On Aug 5, 2009, at 3:46 PM, Frederick C. Lee wrote:

Greetings:    I need to adjust a UILabel's width per with of its text.
What I did was to get the text's length via [NSString length]. Of course,
the displayed UILabel width is too narrow to fully display the actual
string.
So I believe I need to compute the true width based on the number of
font-sized characters.

The "width of a font" doesn't really make sense. It might make sense to look for the width of the widest glyph in a font, but unless it's a fixed-width font, that metric is not the same as "the width of the attributed string, as rendered with the given attributes, including font, paragraph settings, etc." I'd recommend reading all font-related documentation.


Is there a simple, quick way to do this?


A quick look at the UILabel class reference shows that's a subclass of UIView. UIView has -sizeToFit, which, "Resizes and moves the receiver view so it just encloses its subviews." In the case of a UILabel, I would expect this (as on the Mac platform) to size to fit its attributed string keeping all its attributes in mind.

Disclaimer: I'm not an iPhone developer, but I am a Mac desktop developer. Much of Cocoa works the same across both platforms. The UI- related stuff differs in some key areas, but apparently not this one.

--
I.S.


_______________________________________________

Cocoa-dev mailing list ([email protected])

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 [email protected]

Reply via email to