【问题标题】:Clipping of UILabel when adjustsFontSizeToFitWidthadjustsFontSizeToFitWidth 时 UILabel 的裁剪
【发布时间】:2012-10-06 10:25:42
【问题描述】:

当我将文本添加到 adjustsFontSizeToFitWidth 设置为 YES 的标签时,文本不再垂直居中,并最终将文本剪切到标签框架的底部。对于大量文本,它最终会从标签底部消失。

如果您添加较少的文本,会发生这种情况:

这被剪裁了,正如我所期望的那样(即字体大小没有减小,文本在标签中垂直居中并被剪裁在顶部和底部。

这里是重现的代码:

- (void)loadView {
    [super loadView];

    self.view.backgroundColor = [UIColor blueColor];
    testLabel = [[UILabel alloc] init];
    testLabel.font = [UIFont boldSystemFontOfSize:172];
    testLabel.textColor = [UIColor blackColor];
    testLabel.adjustsFontSizeToFitWidth = YES;
    testLabel.numberOfLines = 1;
    testLabel.frame = CGRectMake(50, 50, 300, 100);
    testLabel.text = @"123";

    [self.view addSubview:testLabel];
}

这应该发生吗?以及如何让我的标签垂直居中而不考虑标签中的字符数。

【问题讨论】:

    标签: ios uilabel clipping


    【解决方案1】:

    添加

    testLabel.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
    

    到您的代码中以垂直居中字体比例的文本。

    【讨论】:

    • 太棒了,克鲁姆鲁尔!这工作得很好,我不知道这个属性。我会帮助 adjustFontSizeToFitWidth 属性提到这是类参考。再次感谢。
    • 与 UILabel 合作了这么多年,我从来没有遇到过这个属性))
    【解决方案2】:

    还想补充一点,adjustsFontSizeToFitWidth 不适用于属性文本,所以如果可以的话,将属性添加到标签而不是属性文本。这对我有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-03
      • 1970-01-01
      • 2018-08-30
      • 2017-01-08
      • 2012-08-03
      • 2018-10-02
      • 1970-01-01
      • 2016-01-04
      相关资源
      最近更新 更多