使背景UILabel更大

问题描述 投票:1回答:3

我的应用程序中有一个问题我有一个数字标签(IBOutlet)。当我写self.numberLabel.backgroundColor = [UIColor redColor];显示红色适合数字的高度时,我想要的是让背景更大一些。谢谢

ios label iboutlet
3个回答
0
投票
self.numberLabel.frame = CGRectMake(self.numberLabel.frame.origin.x, self.numberLabel.frame.origin.y, widht, newHeight);

并确保字体在需要时保持相同的大小

[self.numberLabel setFont:[UIFont systemFontOfSize:35]];

0
投票

您必须在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];

0
投票

有很多方法可以给猫皮肤......在我看来,“uilabel”的内容决定了它的大小。我只是希望背景略大 - 垂直5点,水平7点。所以我使用autolayout来解决它。

  1. 添加一个uilabel作为背景的子视图,这是一个uiview
  2. uilabeluiview之间的IB中添加约束
  3. uiview及其superview之间添加IB中的约束

enter image description here

Background uiview

Label

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.