Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically resize an NSButton to fit programmatically changed text (Xcode)

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!

like image 631
Ryan Avatar asked Aug 27 '11 04:08

Ryan


People also ask

What does the value of the button indicate in NSButton?

For pressure-sensitive buttons, the value of the button indicates pressure level instead. NSButton and NSMatrix both provide a control view, which displays an NSButtonCell object.

What is the difference between NSButton and nsmatrix?

For pressure-sensitive buttons, the value of the button indicates pressure level instead. NSButton and NSMatrix both provide a control view, which displays an NSButtonCell object. However, while a matrix requires you to access the button cell objects directly, most button class methods act as “covers” for identically declared button cell methods.

Is it possible to change the size of a button?

Button size is static, which means the size of a button cannot be changed once it is defined by the user. The problem here is while resizing the window size, it can affect the button size problem.

What are buttons in Android?

Buttons are a standard control for initiating actions within your app. You can configure buttons with many different visual styles, but the behavior is the same. When a user clicks it, a button calls the action method of its associated target object.


1 Answers

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.

@implementation RKSizeToFitButton
- (void)setStringValue:(NSString*)aString
{
    //get the current frame
    NSRect frame = [self frame];

    //button label
    [super setStringValue:aString];

    //resize to fit the new string
    [self sizeToFit];

    //calculate the difference between the two frame widths
    NSSize newSize = self.frame.size;
    CGFloat widthDelta = newSize.width - NSWidth(frame);
    //set the frame origin
    [self setFrameOrigin:NSMakePoint(NSMinX(self.frame) - widthDelta, NSMinY(self.frame))];
}
@end

This way you can just set your button's class to RKSizeToFitButton in Interface Builder and then calling setStringValue: on the button to change its label will "just work" with no additional code.

like image 196
Rob Keniger Avatar answered Oct 16 '22 22:10

Rob Keniger