sizeToFit在非常短的字符串周围放置填充



我有一个UIButton,在设置了它的标题后我叫它sizeToFit。如果标题文字很短(可能是<20pt),按钮在左右两边都有一些填充点。在更长时间内,填充物消失。这就好像按钮有一个内部最小宽度,当sizeToFit被调用时。有人知道如何防止这种填充物吗?

这是有效的,只需将字符串更改为任意值,并享受调整大小的奇迹。

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

最新更新