UILabel圆角、边框、宽度自适应


//设置圆角
    _userCompanyLabel.layer.masksToBounds = YES;//必须设置
    _userCompanyLabel.layer.cornerRadius = labelHight/2;//设置圆角的弧度,等于label的高的一半的时候,是个圆
//设置边框
    _userCompanyLabel.layer.borderColor = [SNColorFromRGB(backgroundColor1) CGColor];//边框颜色
    _userCompanyLabel.layer.borderWidth = 4;//边框的宽度

宽度自适应

_userCompanyLabel.text = text;
    CGFloat width = 78;
//根据text来找到宽度,size为最大宽度和最大高度,当为单行时options为NSStringDrawingUsesLineFragmentOrigin即可,为多行时NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading,attributes中设置front,
    CGRect rect = [text boundingRectWithSize:CGSizeMake(320, 40) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:28]} context:nil];
    if (rect.size.width > 78) {
        width = rect.size.width;
    }
    [_userCompanyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_userNameLabel.mas_bottom).offset(6*bili);
        make.centerX.equalTo(_userNameLabel.mas_centerX);
        make.size.mas_equalTo(CGSizeMake(width, 28));
    }];


你可能感兴趣的:(ios开发笔记)