【问题标题】:Single line UILabel hugging not working with autoshrink单行 UILabel 拥抱不适用于自动收缩
【发布时间】:2015-07-09 21:26:06
【问题描述】:

我有一个具有以下属性的 UILabel:

  • 一个很大的字体
  • 修复了超级视图的左边距
  • 修复了超级视图的右边距
  • 垂直居中
  • Autoshrinksminimum font size 尽可能避免截断文本。
  • 单行
  • 垂直拥抱优先级设置为Required (1000)

我遇到的问题是标签没有垂直拥抱文本,正如您在下图中看到的那样,顶部和文本下方有很多空间。

我上传了示例项目here

谢谢!

【问题讨论】:

  • 我认为自动缩小使文本保持在同一基线上。由于您想要的字体太大,它会为该大小的字体分配空间,然后将文本缩小到基线。我说我认为这就是正在发生的事情。尝试将字体大小设置得更小,看看是否是导致它的原因。我意识到,这不是一个答案,但可能有助于理解它为什么会这样。
  • 是的,这就是原因,迈克,似乎拥抱不适用于标签的自动收缩功能

标签: ios objective-c autolayout uilabel nslayoutconstraint


【解决方案1】:

试试下面的代码:

[self.myLabel setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleWidth]; 

【讨论】:

  • 感谢 Piyush 的建议,但没用 :(
【解决方案2】:

我能够通过一种解决方法达到预期的效果。这不是完美的解决方案,也不适用于所有情况,但至少解决了我的问题。

摘要

解决方法如下:

  1. 找到actual font(标签实际使用的字体)。
  2. 使用实际字体计算文本高度。
  3. 使用UIViews- (UIEdgeInsets)alignmentRectInsets (reference) 修改Autolayout 使用的对齐矩形,以根据实际文本高度定位视图。

有了这一切,我才得以摆脱:

到这里:

问题

  • 需要将基线设置为中心
  • 仅适用于超出高度(不适用于左右边距)
  • 仅适用于单行标签

代码

如下声明了一个UILabel子类

@implementation HuggingLabel

- (UIEdgeInsets)alignmentRectInsets
{
    if (self.numberOfLines != 1)
    {
        if ([super respondsToSelector:@selector(alignmentRectInsets)])
            return [super alignmentRectInsets];
        else
            return UIEdgeInsetsZero;
    }

    UIFont *fontThatFitsBounds = [self fontThatFitsBounds];
    CGSize textSize = [self.text sizeWithAttributes:@{
                                                      NSFontAttributeName : fontThatFitsBounds
                                                      }];
    CGSize actualSize = self.bounds.size;

    CGFloat exceedingWidth = actualSize.width = textSize.width;
    CGFloat exceedingHeight = actualSize.height - textSize.height;
    CGFloat exceedingHeightTop = exceedingHeight / 2.0f;
    CGFloat exceedingHeightBottom = MAX(0.0f, exceedingHeight / 2.0f + fontThatFitsBounds.descender);

    UIEdgeInsets insets = UIEdgeInsetsMake(
                                           exceedingHeightTop,
                                           0.0f,
                                           exceedingHeightBottom,
                                           0.0f
                                           );

    return insets;
}

- (UIFont *)fontThatFitsBounds
{
    CGSize currentSize = CGSizeZero;
    CGFloat fontSizeThatFits = self.font.pointSize + 1.0f;

    do {

        fontSizeThatFits = fontSizeThatFits - 1.0f; //try with one point smaller size

        NSDictionary *attributes = [self stringAttributesWithFontOfSize:fontSizeThatFits];
        currentSize = [self.text sizeWithAttributes:attributes];

    } while (currentSize.width > self.bounds.size.width ||
             currentSize.height > self.bounds.size.height);

    return [self.font fontWithSize:fontSizeThatFits];
}

- (NSDictionary *)stringAttributesWithFontOfSize:(CGFloat)fontSize
{
    NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineBreakMode = self.lineBreakMode;
    paragraphStyle.alignment = self.textAlignment;

    NSDictionary *attributes = @{
                                 NSFontAttributeName : [self.font fontWithSize:fontSize],
                                 NSParagraphStyleAttributeName : paragraphStyle
                                 };

    return attributes;
}

@end

可以下载here的演示项目

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多