iOS 6 multiline label line spacing

耗尽温柔 提交于 2019-12-12 02:18:01

问题


There is a problem with line spacing in UILabel, I am using custom font and when I use smilies there is no space between two lines. which obviously looks not so good. So I used this code for line spacing but app crashes giving the error

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'NSAttributedString invalid for autoresizing, it must have a single spanning paragraph style (or none) with a non-wrapping 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;
    }

回答1:


try this

    [cell.label2 setAdjustsFontSizeToFitWidth:NO];

maybe even only for iOS6

if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) { //*
       [cell.label2 setAdjustsFontSizeToFitWidth:NO];
}
  • detect iOS 6 as decribed here: https://developer.apple.com/library/ios/documentation/userexperience/conceptual/transitionguide/SupportingEarlieriOS.html



回答2:


Set up 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;


来源:https://stackoverflow.com/questions/22397869/ios-6-multiline-label-line-spacing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!