Re: How to highlight the current line in NSTextView?
Re: How to highlight the current line in NSTextView?
- Subject: Re: How to highlight the current line in NSTextView?
- From: Martin Hewitson <email@hidden>
- Date: Fri, 04 Nov 2011 17:00:24 +0100
Here's some code that I've been using in an NSTextView subclass. It may be of some use.
Martin
- (void) drawViewBackgroundInRect:(NSRect)rect
{
[super drawViewBackgroundInRect:rect];
NSRange sel = [self selectedRange];
NSString *str = [self string];
if (sel.location <= [str length]) {
NSRange lineRange = [str lineRangeForRange:NSMakeRange(sel.location,0)];
NSRect lineRect = [self highlightRectForRange:lineRange];
NSColor *highlightColor = [NSColor grayColor];
[highlightColor set];
[NSBezierPath fillRect:lineRect];
}
}
// Returns a rectangle suitable for highlighting a background rectangle for the given text range.
- (NSRect) highlightRectForRange:(NSRange)aRange
{
NSRange r = aRange;
NSRange startLineRange = [[self string] lineRangeForRange:NSMakeRange(r.location, 0)];
NSInteger er = NSMaxRange(r)-1;
NSString *text = [self string];
if (er >= [text length]) {
return NSZeroRect;
}
if (er < r.location) {
er = r.location;
}
NSRange endLineRange = [[self string] lineRangeForRange:NSMakeRange(er, 0)];
NSRange gr = [[self layoutManager] glyphRangeForCharacterRange:NSMakeRange(startLineRange.location, NSMaxRange(endLineRange)-startLineRange.location-1)
actualCharacterRange:NULL];
NSRect br = [[self layoutManager] boundingRectForGlyphRange:gr inTextContainer:[self textContainer]];
NSRect b = [self bounds];
CGFloat h = br.size.height;
CGFloat w = b.size.width;
CGFloat y = br.origin.y;
NSPoint containerOrigin = [self textContainerOrigin];
NSRect aRect = NSMakeRect(0, y, w, h);
// Convert from view coordinates to container coordinates
aRect = NSOffsetRect(aRect, containerOrigin.x, containerOrigin.y);
return aRect;
}
On 4, Nov, 2011, at 04:33 PM, Douglas Davidson wrote:
> The NSLayoutManager will tell you. Try taking a look at the conceptual documentation and some of the sample code for NSLayoutManager, and let me know if you have any problems.
>
> Douglas Davidson
>
> On Nov 4, 2011, at 6:19 AM, Nick <email@hidden> wrote:
>
>> Hello
>> Basically the question is about getting the NSRange of the line of
>> NSTextView where the text input cursor is currently in.
>> Word wraps are considered as legal 'new lines' - only one line should
>> actually be highlighted.
>>
>> How could I do this (get an NSRange of a single line)?
>> Thank you
>> _______________________________________________
>>
>> 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
> _______________________________________________
>
> 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
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Martin Hewitson
Albert-Einstein-Institut
Max-Planck-Institut fuer
Gravitationsphysik und Universitaet Hannover
Callinstr. 38, 30167 Hannover, Germany
Tel: +49-511-762-17121, Fax: +49-511-762-5861
E-Mail: email@hidden
WWW: http://www.aei.mpg.de/~hewitson
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
_______________________________________________
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