NSString sizeWithAttributes: Inaccuracy

Posted by dave-gennel on Stack Overflow See other posts from Stack Overflow or by dave-gennel
Published on 2010-03-13T18:15:55Z Indexed on 2010/03/13 19:05 UTC
Read the original article Hit count: 269

Filed under:
|
|

I want to know the width of an NSString displayed on the screen in pixels. So I can fit an NSTextField its bounds to be exactly the length of the string itself. So I used IB's "Label" NSTextField and for those who don't know what I mean, I got a label with title "Label", font "Lucida Grande 13px", not selectable, not editable, regular size, no background and according to IB its width is 38px wide.

If I want to get its width programatically I use

[@"Label" sizeWithAttributes: [NSDictionary dictionaryWithObject: [NSFont fontWithName: @"Lucida Grande" size: 13] forKey: NSFontAttributeName]].width

Which will give me 33.293457 . So that's about 5 px of the real width..

© Stack Overflow or respective owner

Related posts about nsstring

Related posts about attributes