Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Height of text in wrapping NSTextField?

Height of text in wrapping NSTextField?
Thread Tools
Dedicated MacNNer
Join Date: Nov 2000
Status: Offline
Reply With Quote
Mar 3, 2002, 01:31 PM
 
Hello ...

How can I determine the total height of the text in a wrapping NSTextField?

I want to create a generic alert/information sheet that adjusts its height according to the height of the informational text. (Similar to NSBeginAlertSheet() function, except that I need a "Don't Ask Again" checkbox.)

I've tried tinkering with "sizeToFit", but this stretches the field horizontally to keep the text from wrapping. (I notice that NSText has methods "setHorizontallyResizable:" (default NO) and "setVerticallyResizable:" (default YES), but I don't understand how --or if-- these methods can help me out at all.)

Any help?


Thanks and regards,

DayLateDon
     
Dedicated MacNNer
Join Date: Jan 2001
Location: Virginia, US
Status: Offline
Reply With Quote
Mar 7, 2002, 08:08 PM
 
There are a couple methods in NSStringDrawing.h in the AppKit
for getting the size for an NS[Attributed]String, but these don't
deal with text that needs to be wrapped based on a max width.

Are you sure using an NSTextView (as opposed to an NSTextField) and calling sizeToFit doesn't work? You should be able to use NSHeight([textView frame]) after that call... but maybe there is a problem with it.

Otherwise... ick. You'll probably have to get down and dirty with NSTextStorage, NSLayoutManager, and NSTextContainer. I think you have
to create those three objects and point them all to each other, set the text container's size to what you want (i.e. fixed width and big big height), set the text into the text storage (may have to bracket with beginEditing ... endEditing), call a method on NSLayoutManager to force the layout (glyphRangeForTextContainer or textContainerForGlyphAtIndex:effectiveRange: maybe?) then call usedRectForTextContainer to get the rect you want.

Looks like there is a private _sizeWithSize:attributes: method on NSString that probably does exactly what you want (_sizeWithSize: on NSAttributedString), but darn it it's private. That would be a rather useful one to make public... or at least a sizeWithMaxWidth or something.
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 12:13 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2