创建 UIButton 其标题的宽度

Create UIButton the width of its title

我试图通过这样做使 UIButton 与其标题的宽度相同:

CGSize stringSize = [[tagArray lastObject] sizeWithAttributes:@{NSFontAttributeName:@"System 14.0"}];
tagButton.frame = CGRectMake(x, y, stringSize.width, 30);

但是它在 CGSize 行崩溃了。任何指针?数组中的 object 只是一个普通的 NSString.

将第一行改为:

CGSize stringSize = [[tagArray lastObject] sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14.0]}];