• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: To know what shows in the first line of a multiline NSTextField
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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

  • Follow-Ups:
    • Re: To know what shows in the first line of a multiline NSTextField
      • From: Graham Cox <email@hidden>
References: 
 >To know what shows in the first line of a multiline NSTextField (From: Nick Rogers <email@hidden>)

  • Prev by Date: Re: Core Data NSExpression Error
  • Next by Date: Re: To know what shows in the first line of a multiline NSTextField
  • Previous by thread: To know what shows in the first line of a multiline NSTextField
  • Next by thread: Re: To know what shows in the first line of a multiline NSTextField
  • Index(es):
    • Date
    • Thread