2014-03-14 57 views
0

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

终止应用程序由于未捕获的异常“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; 
    } 
+0

使用[yourLabel setAdjustsFontSizeToFitWidth:NO]; –

+0

问题的解决方法是你只需将标签的文本大小自动缩小为固定字体,然后它就可以工作 – codercat

+0

@NitinGohel与此,它的应用程序不会崩溃,但它只显示一行 – Haris

回答

0

建立归属字符串

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;