【发布时间】:2015-08-15 15:48:29
【问题描述】:
我有一个 UIButton,我在设置其标题后调用 sizeToFit。在标题文本很短(可能 sizeToFit 时遵守的内部最小宽度。有谁知道如何防止这种填充?
【问题讨论】:
-
请附上代码的 sn-p。否则,这只是一个猜谜游戏。
标签: ios objective-c swift uibutton uikit
我有一个 UIButton,我在设置其标题后调用 sizeToFit。在标题文本很短(可能 sizeToFit 时遵守的内部最小宽度。有谁知道如何防止这种填充?
【问题讨论】:
标签: ios objective-c swift uibutton uikit
这行得通,只需将字符串更改为任何字符串,然后享受调整大小的奇迹。
NSString * helloKitty = @"I love cats";
UIButton * ss = [UIButton buttonWithType:UIButtonTypeCustom];
[ss setTitle:helloKitty forState:UIControlStateNormal];
[[ss titleLabel] setFont:[UIFont systemFontOfSize:14]];
[ss setBackgroundColor:[UIColor pink] forState:UIControlStateNormal];
CGSize strSizer = [helloKitty sizeWithAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor],
NSFontAttributeName:[UIFont systemFontOfSize:14]}];
[ss setFrame:CGRectMake(100,200,strSizer.width, strSizer.height)];
[self.view addSubview:ss];
【讨论】: