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: Keary Suska <email@hidden>
- Date: Sun, 06 Nov 2011 10:46:39 -0700
On Nov 6, 2011, at 8:39 AM, Nick wrote:
> Keary, Douglas
> Could you please give me a hint what methods to search for?
> Basically I need an NSRange of the current line (or that part of it, whereb the cursor is in, if the line is word-wrapped). Then I'll simply change this substring's attributes.
> Should i find the current text cursor's position (location) in text, and keep invoking locationForGlyphAtIndex for every previous symbol to this cursor location, up till I get the smallest value?
> What's the right way here?
This is all theory, and I have not had to do this, but if it were me I would look at the methods with the words "lineFragment" in them will help isolate the line. I imagine that once you get the glyph nearest the insertion point, you should then be able to use lineFragmentUsedRectForGlyphAtIndex:effectiveRange: and passing its return value to -setSelectedRange:. That should at least get you close if it doesn't do the trick. Also, if you didn't notice from another responder, you can get the appropriate NSLayoutManager from the NSTextView itself.
HTH,
> 2011/11/4 Keary Suska <email@hidden>
> On Nov 4, 2011, at 7:19 AM, Nick 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)?
>
> Cocoa's text handling system is a bit complex (you should read the text-handling guide) but it shouldn't be too difficult. The object you probably need to talk to is the appropriate NSLayoutManager (probably just one) which belongs to the NSTextStorage of the NSTextView (whew!). That should get you started.
Keary Suska
Esoteritech, Inc.
"Demystifying technology for your home or business"
_______________________________________________
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