Haris
Haris

Reputation: 267

iOS 6 multiline label line spacing

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

Upvotes: 0

Views: 1235

Answers (2)

Flori
Flori

Reputation: 2493

try this

    [cell.label2 setAdjustsFontSizeToFitWidth:NO];

maybe even only for iOS6

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

Upvotes: 1

codercat
codercat

Reputation: 23271

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;

Upvotes: 0

Related Questions