【问题标题】:Label doesn't start from top left标签不是从左上角开始
【发布时间】:2017-02-05 21:19:14
【问题描述】:

我上传了两张带有“取货地点”和“送货地点”标签的图片。此标签在 tableview 单元格中设置。

在这张图片中:

这两个标签设置为 numberoflines = 3 但文本从中间开始

我想要这个:

我已经使用 set property numberofLines = 0 和 label height >= of label 做了这件事。 但是设置这个属性我必须找到带有文本的标签实际高度,然后根据标签高度设置其他控件。

Sizetofit 在这种情况下不起作用

我想要:标签高度是固定的。 NumberofLines = 3,标签从左上角开始

感谢您的帮助

【问题讨论】:

    标签: objective-c swift


    【解决方案1】:

    创建一个 UILabel 的子类并将其命名为 MyTopAlignedLabel。

    然后将其添加到您的 MyTopAlignedLabel.m 文件中:

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

    将您在 Interface Builder 中的标签设置为此类。

    【讨论】:

    • 它非常适合我。您确定在 Interface Builder 中将标签的自定义类设置为MyTopAlignedLabel
    猜你喜欢
    • 2011-06-30
    • 2020-08-26
    • 2016-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多