截圖:

問(wèn)題描述:
UILabel,指定行數(shù),高度通過(guò)sizetofit來(lái)自適應(yīng)高度(自己計(jì)算高度也試過(guò),還是一樣的這個(gè)問(wèn)題),發(fā)現(xiàn)主要是由于這個(gè)UILabel的attributedText引起的,兩個(gè)屬性NSBackgroundColorAttributeName和paragraphStyle.lineSpacing這兩個(gè)屬性同時(shí)設(shè)置導(dǎo)致出現(xiàn)這種情況,僅在iOS 11出現(xiàn)。
代碼片段:
- (void)addLabel {
? ? UILabel*label = [UILabelnew];
????label.backgroundColor= [UIColorblueColor];
????label.numberOfLines=3;
????NSAttributedString*attributedStr = [selfsetupAttributedString:eng_text];
????label.attributedText= attributedStr;
????label.frame=CGRectMake(0,0,300,0);
????[labelsizeToFit];
????label.center=CGPointMake(ScreenWidth/2, (ScreenHeight-20)/2);
????[self.viewaddSubview:label];
}
- (NSMutableAttributedString*)setupAttributedString:(NSString*)text {
????NSMutableDictionary*attrsDic = [NSMutableDictionarydictionary];
????attrsDic[NSFontAttributeName] = [UIFontsystemFontOfSize:12];????????
????attrsDic[NSBackgroundColorAttributeName] = [UIColorredColor];
????NSMutableParagraphStyle*paragraphStyle = [[NSMutableParagraphStylealloc]init];
????paragraphStyle.lineSpacing=10;
????paragraphStyle.lineBreakMode=NSLineBreakByTruncatingTail;
????attrsDic[NSParagraphStyleAttributeName] = paragraphStyle;
????attrsDic[NSForegroundColorAttributeName] = [UIColorwhiteColor];
????NSMutableAttributedString*attributedText = [[NSMutableAttributedStringalloc]initWithString:textattributes:attrsDic];
????returnattributedText.copy;
}