【问题标题】:iOS 6 multiline label line spacingiOS 6 多行标签行距
【发布时间】:2014-04-16 02:35:42
【问题描述】:

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

由于未捕获的异常“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;
    }

【问题讨论】:

  • 使用 [yourLabel setAdjustsFontSizeToFitWidth:NO];
  • 解决的问题是您只需将标签的文本大小自动缩小为固定字体,然后它就可以工作了
  • @NitinGohel 有了这个,它的应用程序不会崩溃,但它只显示一行
  • 更改标签。 numberOfLines = 0;
  • 有什么方法可以将它与 sizeToFit 一起使用吗?在其他情况下,它没有设置标签的宽度,我需要这样做。

标签: ios uilabel


【解决方案1】:

试试这个

    [cell.label2 setAdjustsFontSizeToFitWidth:NO];

甚至可能只适用于 iOS6

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

【讨论】:

    【解决方案2】:

    设置属性字符串

      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;
    

    【讨论】:

      猜你喜欢
      • 2013-09-28
      • 1970-01-01
      • 1970-01-01
      • 2014-09-28
      • 2017-09-15
      • 2012-12-13
      • 1970-01-01
      • 2018-06-17
      • 1970-01-01
      相关资源
      最近更新 更多