美文网首页iOS
Masonry中UILabel设置多行展示

Masonry中UILabel设置多行展示

作者: 蜡笔小强 | 来源:发表于2017-07-06 16:00 被阅读372次

问题描述

xib中或者代码加载UILabel时候,一般设置label.numberOfLines=0;即可完成自适应。但是当执行如下代码的时候:

        make.top.mas_equalTo(weightOK.mas_bottom).offset(5);
        make.left.mas_equalTo(iconImage.mas_left).offset(0);
    }];```
会发现并不能正常换行。期初怀疑是,漏加numberOfLines。加上`locationOK.numberOfLines=0;`问题依旧。
##解决办法:
创建时候,加上如下代码即可
```  locationOK.text=@“test”;
    locationOK.numberOfLines=0;
    locationOK.font=[UIFont systemFontOfSize:14];
    locationOK.preferredMaxLayoutWidth=400;
    [locationOK setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];```
##总结:
Masonry和UILabel一起使用的时候,需要设置多行和换行,除了`numberOfLines`需要设置,还要注意`preferredMaxLayoutWidth`和`setContentHuggingPriority`的设置,只有正确设置了这2个才能换行。也算踩了一个小坑,之前一直xib写的。

相关文章

网友评论

    本文标题:Masonry中UILabel设置多行展示

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