2014-03-14 81 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;