Mike2012
Mike2012

Reputation: 7725

Determining the pixel length of a string in Cocoa (MAC OSX)

I need to figure out the pixel length of an NSString in Cocoa on OSX and I have found a couple of links that describe how to do this using the IPhone sdk but I didn't see anything about how to do this when not targeting the IPhone. Can anyone link me to some docs that would be relevant. I did find anything in the NSString docs.

Upvotes: 6

Views: 2469

Answers (2)

AlBeebe
AlBeebe

Reputation: 8121

Here is what i use to get the size of a string...

NSSize size = [@"Some text" sizeWithAttributes:[NSDictionary dictionaryWithObject:[NSFont fontWithName:@"Helvetica Neue Bold" size:24.0f] forKey:NSFontAttributeName]];

NOTE: If you are adding the string to a textfield, i have found that you need to add about 10 to size.width for it to fit.

Upvotes: 4

Chuck
Chuck

Reputation: 237040

-[NSString sizeWithAttributes:]

Upvotes: 7

Related Questions