Question

When dragging a Label into .xib file in Xcode, I noticed two "sizes": Font size of the label text and the size of the NSTextField instance itself.

If I want to display the text in different font size (for example, from "Lucida Grande 13" to "Lucida Grande 40"), in what way could I know by what size of NSTextField rectangle can JUST fit the text in specified font configuration?

Furthermore, are the ways different between iOS and OS X programming?

Was it helpful?

Solution

You might find this useful:

NSString *longName = @"Subrahmanyan Chandrasekhar";
CGSize size = [longName sizeWithFont:label.font];

That gives the size of rectangle that just fits the given font with the given text. What you want is to solve for a different variable: given the rectangle size and text, find the maximum font size that fits. You could do it by a trial and error process in your code, e.g. binary search.

That doesn't really help you if you're setting the frame size in Interface Builder, though. Best suggestion there, uncheck "Adjust to fit", type the longest text you expect to see in the box, and increase the font size until it looks broken.

Yes, it's different for OSX. Apparently sizeWithFont: doesn't exist on OSX.

OTHER TIPS

NSTextField is a control which is inherited from NSControl.

And if you check here, you will find

- (void)sizeToFit.

Resizes the receiver’s frame so that it is the minimum size needed to contain its cell.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top