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);



把賦值放在自適應的前面就可以了

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容