【问题标题】:sizeToFit is placing padding around very short stringssizeToFit 在非常短的字符串周围放置填充
【发布时间】:2015-08-15 15:48:29
【问题描述】:

我有一个 UIButton,我在设置其标题后调用 sizeToFit。在标题文本很短(可能 sizeToFit 时遵守的内部最小宽度。有谁知道如何防止这种填充?

【问题讨论】:

  • 请附上代码的 sn-p。否则,这只是一个猜谜游戏。

标签: ios objective-c swift uibutton uikit


【解决方案1】:

这行得通,只需将字符串更改为任何字符串,然后享受调整大小的奇迹。

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];

【讨论】:

    猜你喜欢
    • 2016-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-06
    • 1970-01-01
    • 2017-10-31
    • 1970-01-01
    • 2017-07-29
    相关资源
    最近更新 更多