iOS13上,NSAttributedString設置折行方式NSLineBreakByTruncatingTail,計算高度出錯,只返回一行的高度。
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc]init];
style.hyphenationFactor = 1;??//?設置每行的最后單詞是否截斷,在0.0-1.0之間,默認為0.0,越接近1.0單詞被截斷的可能性越大,?
設置hyphenationFactor=1,在計算就返回正常高度了。
如果設置不起效。換下面的方法
找到了解決方案。只需將?NSMutableParagraphStyle
?上的?setAllowsDefaultTighteningForTruncation
?設置為 YES。
參考?cocoa - Making NSTextField not shrink when NSLineBreakByTruncatingTail is set - Stack Overflow
? ? NSString *highlightTitle = title;//轉換參數NSDictionary *options = @{ NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute :@(NSUTF8StringEncoding) };//將html文本轉換為正常格式的文本NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithData:[highlightTitle dataUsingEncoding:NSUnicodeStringEncoding] options:options documentAttributes:nil error:nil];[attributedString removeAttribute:NSParagraphStyleAttributeName range: NSMakeRange(0, attributedString.length)];[attributedString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, attributedString.length)];NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];//? ? paragraphStyle.hyphenationFactor = 1;[paragraphStyle setLineSpacing:space];paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;[paragraphStyle setAllowsDefaultTighteningForTruncation:YES];[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [attributedString length])];return attributedString;