Re: drawing lines in and NSTextView
Re: drawing lines in and NSTextView
On 31 Mar 2012, at 13:08, Koen van der Drift wrote:
> I have an NSTextView to which I want to add some lines that connect
> certain words. When the text changes, either by editing, or scrolling,
> that lines should follow the words. I thought about using
> CoreAnimation, but text in a CATextLayer does not appear to be
> editable like the text in an NSTextView (is that correct?). So, an
> alternative could be to override drawViewBackgroundInRect and use
> NSBezierPaths to draw my lines and I will work on that this weekend.
>
> Any thoughts or suggestions I may have overlooked?
>
You probably get get what you need form the WWDC references already given but the following might still be useful.
http://www.cocoabuilder.com/archive/cocoa/113955-graphics-in-nstextview-ichat-like-bubbles.html?q=nstextview+draw+bubble#114075
Of course, overriding drawViewBackgroundInRect, will draw your lines behind the text, which may be acceptable.
Another approach is to add a subview to the NSTextView instance and do all your drawing there.
I use this approach to draw a simple Quartz 2D animation over an NSTextView.
Obviously you need to make your drawing sub view non opaque.
You also will need to resize your subview as the NSTextView size changes by requesting that the appropriate NSView notifications be sent.
In my case I make my child view modify its behaviour if it is embedded in an NSTextView.
// in NSTextView subview
if ([[self superview] isKindOfClass:[NSTextView class]]) {
// get the text view
_textView = (id)[self superview];
[_textView setAutoresizesSubviews:YES];
// set scrollview background drawing behaviour
[[self enclosingScrollView] setDrawsBackground:NO];
// observe changes to the scrollview content bounds.
// see "How Scroll Views Work" in the docs
[[[self enclosingScrollView] contentView] setPostsBoundsChangedNotifications:YES];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(scrollContentBoundsChanged:) name:NSViewBoundsDidChangeNotification object:[[self enclosingScrollView] contentView]];
[_textView setPostsFrameChangedNotifications:YES];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(scrollContentBoundsChanged:) name:NSViewFrameDidChangeNotification object:_textView];
// turn on layer backed views for this view
// and all subviews.
//
// this works fine but the cpu usage can be very high
// add a filter
if (_useLayers) {
// create layer for textview and all subviews
[_textView setWantsLayer:YES];
// this works but the overhead is high - cpu usage increases from 4 -> 40%
CIFilter *filter = [CIFilter filterWithName:@"CIColorBurnBlendMode"];
[self setCompositingFilter:filter];
}
}
Regards
Jonathan Mitchell
Mugginsoft LLP
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden