ios - 在 UILabel 中垂直对齐文本(注 : Using AutoLayout)

标签 ios objective-c cocoa-touch autolayout uilabel

我正在复制 Question 之前提出的相同问题. 我已经尝试了给出的解决方案,但由于 sizetofit 在我使用 Autolayout 时无效,因此无法解决。

first screenshot

预期的显示如下。

second screenshot

最佳答案

编辑

在我原来的答案中,我使用了标签的段落样式。事实证明,对于多行标签,这实际上可以防止标签是多行的。结果我把它从计算中删除了。在 Github 中查看更多信息

对于那些更习惯于使用开源的人,请务必查看 TTTAttributedLabel您可以在其中将标签的文本对齐方式设置为 TTTAttributedLabelVerticalAlignmentTop


诀窍是继承 UILabel 并覆盖 drawTextInRect。然后强制在标签边界的原点绘制文本。

这是一个您现在可以使用的简单实现:

swift

@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
    }
}

swift 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
    }
}

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

由于我使用了 IBDesignable,您可以将此标签添加到 Storyboard 中并观看它的运行,这对我来说就是这样

enter image description here

关于ios - 在 UILabel 中垂直对齐文本(注 : Using AutoLayout),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28605341/

相关文章:

ios - ScrollView 中的分页和重用 View

ios - 如何在 iOS 中停止 JSContext 评估?

ios - 在 iOS 7 中撰写邮件 : Assertion failure in -[MFComposeSubjectView layoutSublayersOfLayer:]

cocoa-touch - 搜索 nsdictionary 的 nsarray

ios - 如何制作一个在 hh :mm:ss:ms 中计算的 Swift 3.0 计时器

ios - 无法在iOS模拟器中选择图片

ios - 将 UIView backgroundColor 设置为透明的更好方法是什么?

iphone - NSOperations 或 NSThread 用于突发不断相互取消的较小任务?

android - 我可以构建一个应用程序与另一个我不拥有的应用程序交互吗?

ios - 如何在 UIwebview IOS 中切换 html source View 和 web view