I have an NSButton (Push Button) with some temporary title text built in Interface Builder / Xcode. Elsewhere, the title text inside the button is changed programmatically to a string of unknown length (actually, many times to many different lengths).
I’d like the button to automatically be resized (with a fixed right position–so it grows out to the left) to fit whatever length of string is programmatically inserted as button text. But I can’t figure it out. Any suggestions? Thanks in advance!
If you can’t use Auto Layout as suggested by @jtbandes (it’s only available in Lion), then you can call
[button sizeToFit]after setting its string value, which will make the button resize to fit its string. You would then need to adjust its frame based on the new width.You can’t do this automatically, but it would be easy to do in a subclass of
NSButton.This way you can just set your button’s class to
RKSizeToFitButtonin Interface Builder and then callingsetStringValue:on the button to change its label will “just work” with no additional code.