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 (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to