截图:
相同的代码,分别运行在iPhone 8(iOS 11)和iPhone 7(iOS 10.3.1)问题描述:
UILabel,指定行数,高度通过sizetofit来自适应高度(自己计算高度也试过,还是一样的这个问题),发现主要是由于这个UILabel的attributedText引起的,两个属性NSBackgroundColorAttributeName和paragraphStyle.lineSpacing这两个属性同时设置导致出现这种情况,仅在iOS 11出现。
代码片段:
- (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;
}
网友评论