美文网首页
UILabel的设置最大宽度:preferredMaxLayou

UILabel的设置最大宽度:preferredMaxLayou

作者: 阳光下的叶子呵 | 来源:发表于2021-12-28 18:14 被阅读0次
设置最大宽度的前提是:内容多行,设置numberOfLines > 1,如果需要显示一行的话,设置UILabel的height,高度只能放置一行文字

self.initiatorL.numberOfLines = 2;
self.initiatorL.lineBreakMode = NSLineBreakByTruncatingMiddle; // 省略号 在中间
self.initiatorL.preferredMaxLayoutWidth = [UIScreen mainScreen].bounds.size.width/375*85;

    _initiatorL = [[UILabel alloc] init];
    _initiatorL.backgroundColor = RGBA(248, 249, 250, 1);
    _initiatorL.text = @" 叶子啦啦啦啦溜了溜了发起 ";
    _initiatorL.textColor = RGBA(138, 149, 181, 1);
    _initiatorL.textAlignment = NSTextAlignmentLeft;
    _initiatorL.font = [UIFont systemFontOfSize:[UIScreen mainScreen].bounds.size.width/375*10 weight:UIFontWeightRegular];
    self.initiatorL.layer.masksToBounds = YES;
    self.initiatorL.layer.cornerRadius = [UIScreen mainScreen].bounds.size.width/375*4;
    self.initiatorL.numberOfLines = 2;
    _initiatorL.lineBreakMode = NSLineBreakByTruncatingMiddle; // 省略号 在中间
    self.initiatorL.preferredMaxLayoutWidth = [UIScreen mainScreen].bounds.size.width/375*85; // 设置最大宽度的前提是:多行,设置numberOfLines > 1,如果需要显示一行的话,设置UILabel的height,只能放置一行文字
    [self.backView addSubview:self.initiatorL];
    [self.initiatorL makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.matchTypeL);
        make.left.mas_equalTo(self.numPeopleL.mas_right).offset([UIScreen mainScreen].bounds.size.width/375*6);
        make.height.mas_equalTo([UIScreen mainScreen].bounds.size.width/375*20);
    }];

相关文章

网友评论

      本文标题:UILabel的设置最大宽度:preferredMaxLayou

      本文链接:https://www.haomeiwen.com/subject/awopqrtx.html