【问题标题】:How to remove insets in UILabel in iOS如何在 iOS 中删除 UILabel 中的插图
【发布时间】:2016-05-31 14:55:25
【问题描述】:

我有带有文本 D 的简单标签,我使用 sizeToFit 我得到高度 21.5

但我看到大约 2px 的填充

如何去除?

我尝试 layoutMargins = UIEdgeInsetsZero - 但没有效果

【问题讨论】:

  • 尝试将文本设置为 Dj,看看底部是否发生了魔法?
  • 无变化。仅移除底部
  • 那是因为字体高度。我 j 删除了底部填充,这意味着可能有一些比 D 高的字符会到达实验室的顶部

标签: ios iphone user-interface autolayout


【解决方案1】:

您可能想尝试将UILabel 子类化并覆盖drawTextInRect:

- (void)drawTextInRect:(CGRect)rect
{
    [super drawTextInRect:UIEdgeInsetsInsetRect(rect, UIEdgeInsetsMake(0., 0., 0., 0.))];
}

【讨论】:

    【解决方案2】:
    CGRect frame = CGRectMake(0, 0, 120, 30);
    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    CGRect labelFrame = UIEdgeInsetsInsetRect(frame, contentInsets);
    UILabel *label = [[UILabel alloc] initWithFrame:labelFrame];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多