iOS 6多线标签行间距

时间:2014-03-14 06:39:27

标签: ios uilabel

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

  
    

由于未捕获的异常终止应用程序' NSInternalInconsistencyException',原因:' NSAttributedString对于自动调整大小无效,它必须具有单个跨越段落样式(或无)以及非换行lineBreakMode。&# 39;

  
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;
    }

2 个答案:

答案 0 :(得分:1)

试试这个

    [cell.label2 setAdjustsFontSizeToFitWidth:NO];

甚至可能仅适用于iOS 6

if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) { //*
       [cell.label2 setAdjustsFontSizeToFitWidth:NO];
}

答案 1 :(得分:0)

设置Attributable String

  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;