垂直对齐UILabel中的文本(注意:使用AutoLayout)

MEL*_*WIN 62 cocoa-touch objective-c uilabel ios autolayout

我正在复制问题之前的相同问题.我曾试图给出的解决方案,并没有能够解决它,因为sizetofit当我使用自动布局是无效的.

第一个截图

预期的显示如下.

第二个截图

Dan*_*sko 131

编辑

在我原来的回答中,我使用的是标签的段落样式.事实证明,对于多行标签而言,这实际上会阻止标签成为多行标签.结果我把它从计算中删除了.在Github中查看更多相关信息

对于那些使用Open Source更加舒服的人,请务必查看TTTAttributedLabel,您可以在其中设置标签的文本对齐方式TTTAttributedLabelVerticalAlignmentTop


诀窍是子类化UILabel和覆盖drawTextInRect.然后强制在标签边界的原点绘制文本.

这是一个你现在可以使用的天真实现:

迅速

@IBDesignable class TopAlignedLabel: UILabel {
    override func drawTextInRect(rect: CGRect) {
        if let stringText = text {
            let stringTextAsNSString = stringText as NSString
            var labelStringSize = stringTextAsNSString.boundingRectWithSize(CGSizeMake(CGRectGetWidth(self.frame), CGFloat.max),
                options: NSStringDrawingOptions.UsesLineFragmentOrigin,
                attributes: [NSFontAttributeName: font],
                context: nil).size
            super.drawTextInRect(CGRectMake(0, 0, CGRectGetWidth(self.frame), ceil(labelStringSize.height)))
        } else {
            super.drawTextInRect(rect)
        }
    }
    override func prepareForInterfaceBuilder() {
        super.prepareForInterfaceBuilder()
        layer.borderWidth = 1
        layer.borderColor = UIColor.blackColor().CGColor
    }
}
Run Code Online (Sandbox Code Playgroud)

斯威夫特3

  @IBDesignable class TopAlignedLabel: UILabel {
    override func drawText(in rect: CGRect) {
        if let stringText = text {
            let stringTextAsNSString = stringText as NSString
            let labelStringSize = stringTextAsNSString.boundingRect(with: CGSize(width: self.frame.width,height: CGFloat.greatestFiniteMagnitude),
                                                                            options: NSStringDrawingOptions.usesLineFragmentOrigin,
                                                                            attributes: [NSFontAttributeName: font],
                                                                            context: nil).size
            super.drawText(in: CGRect(x:0,y: 0,width: self.frame.width, height:ceil(labelStringSize.height)))
        } else {
            super.drawText(in: rect)
        }
    }
    override func prepareForInterfaceBuilder() {
        super.prepareForInterfaceBuilder()
        layer.borderWidth = 1
        layer.borderColor = UIColor.black.cgColor
    }
}
Run Code Online (Sandbox Code Playgroud)

Objective-C的

IB_DESIGNABLE
@interface TopAlignedLabel : UILabel

@end

@implementation TopAlignedLabel

- (void)drawTextInRect:(CGRect)rect {
    if (self.text) {
        CGSize labelStringSize = [self.text boundingRectWithSize:CGSizeMake(CGRectGetWidth(self.frame), CGFLOAT_MAX)
                                                         options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                                                      attributes:@{NSFontAttributeName:self.font}
                                                         context:nil].size;
        [super drawTextInRect:CGRectMake(0, 0, ceilf(CGRectGetWidth(self.frame)),ceilf(labelStringSize.height))];
    } else {
        [super drawTextInRect:rect];
    }
}

- (void)prepareForInterfaceBuilder {
        [super prepareForInterfaceBuilder];
        self.layer.borderWidth = 1;
        self.layer.borderColor = [UIColor blackColor].CGColor;
}

@end
Run Code Online (Sandbox Code Playgroud)

由于我使用了IBDesignable,您可以将此标签添加到故事板并观看它,这就是我的样子

在此输入图像描述

  • @DanielGalasko我发现使用这个代码的文字太大而不适合标签的框架会导致顶部出现偏移.用`CGFloat height = MIN(labelStringSize.height,CGRectGetHeight(self.frame))修复; [super drawTextInRect:CGRectMake(0,0,ceilf(CGRectGetWidth(self.frame)),height)];` (2认同)

Nik*_*vic 40

如果您不限制UILabel的固定大小,而不是在UILabel中对齐文本,只需在给定标签上使用≥约束来更改它的大小.

在此输入图像描述

这是使用自动布局的最优雅的解决方案.不要忘记将numberOfLines设置为零.

  • 是的!最好,最简单,最优雅的解决方案在这里。 (3认同)
  • 类似地,你可以设置高度<=某物 (2认同)

art*_*dev 33

您可以使用UITextView而不是UILabel:
取消选中"Scrolling enabled"
取消选中"Editable"
取消选中"Selectable"
将背景颜色设置为ClearColor

  • 这对我来说很好...没有覆盖一个类或uitextbox做的任何事情...非常感谢! (3认同)

小智 8

我遇到了同样的问题,这就是我解决它的方法.我刚刚在Attribute Inspector下为Label编辑了Baseline.将其设置为"对齐中心".

将基线设置为对齐中心


Fel*_*ile 7

相反,我将底部空间常数更改为优先级@ 250并解决了我的问题.我的标签高度不变,<=常数