我的应用程序中有一个问题我有一个数字标签(IBOutlet)。当我写self.numberLabel.backgroundColor = [UIColor redColor];
显示红色适合数字的高度时,我想要的是让背景更大一些。谢谢
self.numberLabel.frame = CGRectMake(self.numberLabel.frame.origin.x, self.numberLabel.frame.origin.y, widht, newHeight);
并确保字体在需要时保持相同的大小
[self.numberLabel setFont:[UIFont systemFontOfSize:35]];
您必须在Interface Builder / Storyboard中设置约束以修复标签高度/宽度。 如果标签的内容在宽度上发生变化,您可以使用它来计算剩余空间的新宽度:
float labelWidth =
[self.myLabel.text
boundingRectWithSize:self.myLabel.frame.size
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{ NSFontAttributeName:self.myLabel.font }
context:nil]
.size.width;
CGRect rect = self.myLabel.bounds;
rect.size.width = labelWidth + 15.0f; //replace 15.0F with the value you want
[self.myLabel setBounds:rect];