Search code examples
cocoansviewnsbuttonnsattributedstring

How to get the preferred size of NSButton?


I have a custom view for an NSMenuItem that contains an NSButton. The title of the button is determined at runtime and I'd like to adjust the frame of the button so that the content (button title) always fits.

I'm aware of [NSAttributedString boundingRectWithSize:options:], but I'm wondering if there's a built-in way to do this for NSButtons or even on the NSView level.


Solution

  • NSButton inherits NSControl's -sizeToFit method, which may provide what you're looking for.