10

I ran my app under iOS 7 and discovered that multiline labels (non-attributed, plain text) render with a small line spacing. Anyone knows what to do it with iOS 5 compatibility?

iOS 5/6

iOS 5/6

iOS 7

iOS 7

efpies
  • 3,625
  • 6
  • 34
  • 45

2 Answers2

18
if(NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1)
{
    NSFont *font = /* set font */;

    NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
    [paragraphStyle setLineSpacing: /* required line spacing */];

    NSDictionary *attributes = @{ NSFontAttributeName: font, NSParagraphStyleAttributeName: paragraphStyle };
    NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:@"strigil" attributes:attributes];

    [label setAttributedText: attributedString];
}
else
{
    /* old method */
}
Ben Packard
  • 26,102
  • 25
  • 102
  • 183
Léo Natan
  • 56,823
  • 9
  • 150
  • 195
  • if reducing linespacing to 0 'zero' is not sufficient, you should have a look to the "maximumLineHeight" property of the paragraphStyle. – Riad Krim Jun 09 '14 at 13:31
  • @TwiterZX Reducing light height can create issue where some character colliding. – Léo Natan Jun 09 '14 at 13:55
  • @Leo Natan I agree, this can produce an issue in text drawing if the maximumLineHeight is lower then the font lineheight. – Riad Krim Jun 09 '14 at 14:55
  • @Leo Natan I used this to remove the font descender value as it is in uppercase, so for me in this case, it is safe. "maximumLineHeight = font.ascender". – Riad Krim Jun 09 '14 at 15:00
  • @TwiterZX A letter like "Q" still has a descender, as well as other Unicode characters. But yeah, your comment should be considered as well. – Léo Natan Jun 09 '14 at 15:45
  • Why `NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];` not showing any effect? – Saheb Singh Aug 01 '14 at 09:51
  • 1
    @SahebSingh Because I assume `alloc] init]` doesn't return the default paragraph style. – Léo Natan Aug 01 '14 at 09:52
1

I used MSLabel in iOS5/6. After iOS7 released, MSLabel is still working normally.

Nothing different in labels between iOS5/6 and iOS7. You can try MSLabel at https://github.com/LemonCake/MSLabel

Tuyen Nguyen
  • 1,286
  • 1
  • 9
  • 9