iOS bug----label自适应宽度

self.commentNumber=[[UILabel alloc]init];

self.commentNumber.numberOfLines=0;

self.commentNumber.text=[NSString stringWithFormat:@"%@",self.model.comment_num];

CGRect rectcomment = [self.commentNumber.text boundingRectWithSize:size options:NSStringDrawingUsesFontLeading|NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : Font11} context:nil];

[self.commentNumber setFont:[UIFont systemFontOfSize:11]];

[self.commentNumber setTextColor:[UIColor grayColor]];

[self.commentNumber sizeToFit];

self.commentNumber.textAlignment=NSTextAlignmentCenter;

[self.contentView addSubview:self.commentNumber];

self.commentNumber.sd_layout

.rightSpaceToView(self.likeBtn,15)

.topSpaceToView(self.contentLa,11)

.widthIs(rectcomment.size.width)

.heightIs(rectcomment.size.height);



把赋值放在自适应的前面就可以了

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容