iOS 6多行标签行距

UILabel中的行距有问题,我使用自定义字体,当我使用表情符号时,两行之间没有空格。 这显然看起来不太好。 所以我用这个代码行间距,但应用程序崩溃给出的错误

终止应用程序,由于未捕获exception“NSInternalInconsistencyException”,原因:'NSAttributedString无效的自动调整,它必须有一个单一的跨越段落风格(或没有)与一个无包装lineBreakMode。

if ([cell.label2 respondsToSelector:@selector(setAttributedText:)]) { UIFont *font =btMyriadProRegularWithSize14Pt; NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy]; [paragraphStyle setLineSpacing: 22]; NSDictionary *attributes = @{ NSFontAttributeName: font, NSParagraphStyleAttributeName: paragraphStyle }; NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:chatMessage.msgString attributes:attributes]; [cell.label2 setAttributedText: attributedString]; } else { NSString * msg = [NSString stringWithFormat:@"%@: %@",chatMessage.from,chatMessage.msgString]; cell.label2.text = msg; } 

尝试这个

  [cell.label2 setAdjustsFontSizeToFitWidth:NO]; 

甚至可能只适用于iOS6

 if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) { //* [cell.label2 setAdjustsFontSizeToFitWidth:NO]; } 
  • 如以下所述检测iOS 6: https : //developer.apple.com/library/ios/documentation/userexperience/conceptual/transitionguide/SupportingEarlieriOS.html

设置Attributstring

  NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init]; [paragraph setLineBreakMode:NSLineBreakByWordWrapping]; [paragraph setLineBreakMode:NSLineBreakByTruncatingTail]; self.attrText = [[NSMutableAttributedString alloc] initWithString:text]; [self.attrText addAttribute:NSParagraphStyleAttributeName value:paragraph range:NSMakeRange(0, text.length)]; self.Text = text;