Re: String attributes in the iPhone SDK
Re: String attributes in the iPhone SDK
- Subject: Re: String attributes in the iPhone SDK
- From: Greg Parker <email@hidden>
- Date: Thu, 10 Dec 2009 15:49:38 -0800
On Dec 10, 2009, at 3:44 PM, Charlie Dickman wrote:
> I use the following code to determine the height and width of a string in the Snow Leopard version of an app
>
> NSMutableDictionary *textAttributes = [[[NSMutableDictionary alloc] init] autorelease];
> [textAttributes setObject: [NSFont fontWithName: @"Garamond Bold" size: 24]
> forKey: NSFontAttributeName];
> [textAttributes setObject: [NSColor blackColor]
> forKey: NSForegroundColorAttributeName];
>
>
> NSString *text = [NSString stringWithFormat: @"%d", h];
> NSSize textSize = [faceText sizeWithAttributes: faceTextAttributes];
>
> x -= faceTextSize.width / 2.;
> y -= faceTextSize.height / 2.;
>
> I'm tearing my hair out trying to figure out how to accomplish the same thing in the iPhone SDK. Can anybody help me?
First, get a UIFont representing the font you want.
http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIFont_Class/Reference/Reference.html
Then measure the string with -[NSString sizeWithFont:] or some variant thereof.
http://developer.apple.com/iphone/library/documentation/UIKit/Reference/NSString_UIKit_Additions/Reference/Reference.html
--
Greg Parker email@hidden Runtime Wrangler
_______________________________________________
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