Rya*_*yan 13 macos xcode cocoa interface-builder nsbutton
我有一个NSButton(按钮),在Interface Builder/Xcode中内置了一些临时标题文本.在其他地方,按钮内的标题文本以编程方式更改为未知长度的字符串(实际上,多次到许多不同的长度).
我希望按钮能够自动调整大小(具有固定的右侧位置 - 因此它向左延伸)以适合以编程方式插入的任何长度的字符串作为按钮文本.但我无法弄明白.有什么建议?提前致谢!
Rob*_*ger 15
如果您不能使用@jtbandes建议的自动布局(它仅在Lion中可用),那么您可以[button sizeToFit]
在设置其字符串值后调用,这将使按钮调整大小以适合其字符串.然后,您需要根据新宽度调整其框架.
你不能自动执行此操作,但在子类中很容易做到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
Run Code Online (Sandbox Code Playgroud)
这样您就可以RKSizeToFitButton
在Interface Builder中设置按钮的类,然后调用setStringValue:
按钮来更改其标签将"正常工作"而无需其他代码.
归档时间: |
|
查看次数: |
11392 次 |
最近记录: |