Re: To know what shows in the first line of a multiline NSTextField
Re: To know what shows in the first line of a multiline NSTextField
- Subject: Re: To know what shows in the first line of a multiline NSTextField
- From: Graham Cox <email@hidden>
- Date: Wed, 12 May 2010 21:23:16 +1000
On 12/05/2010, at 5:53 PM, Nick Rogers wrote:
> Hi,
> I have resized a multiline text field to show two lines of text (in IB).
> Now I want to know what it is displaying in the first line (or the length of it) even if the first inputted line is longer than a visible line and wraps around to the next visible line.
>
> Is it possible?
> Searched the docs and internet a lot but couldn't find.
Probably because it (seems to be) is a weird thing to want to know.
You can do it by getting down into the NSLayoutManager used by the text view and asking it for the glyph range for the first line fragment, then converting that to character indexes and then finally extracting that from the string in the view's text storage.
Roughly (untested, typed in mail):
NSLayoutManager* lm = [myTextView layoutManager];
NSRange glyphRange;
NSRect lineFragmentRect = [lm lineFragmentRectForGlyphAtIndex:0 effectiveRange:&glyphRange];
NSRange characterRange = [lm characterRangeForGlyphRange:glyphRange actualGlyphRange:NULL];
NSString* text = [[lm textStorage] string];
NSString* firstLineOfText = [text substringWithRange:characterRange];
(might need some finessing to sanity check ranges, not found and so on)
--Graham_______________________________________________
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