NSParagraphStyle line spacing ignored

41,137

Solution 1

This is a bug in NSHTMLWriter which is the private class which UITextView uses to convert attributedText into HTML. Internally it displays this HTML via a UIWebDocumentView. Read more on the inner workings of UITextView in my writeup here: http://www.cocoanetics.com/2012/12/uitextview-caught-with-trousers-down/

The problem comes from an easy to miss speciality in the font CSS shorthand. If you specify a pixel size with the font shorthand then this sets BOTH the font-size as well as the line-height. Since NSHTMLWriter puts the font AFTER the line-height this causes the line-height to be cancelled out by the font size.

See here for my Radar which includes the full analysis of the bug: http://www.cocoanetics.com/2012/12/radar-uitextview-ignores-minimummaximum-line-height-in-attributed-string/

I suggest you file a bug report as well and mention my Radar #12863734.

Solution 2

I don't know if this is enough for your purposes but I could adjust the line spacing by setting the minimum and maximum line height. Furthermore to use a font I put it into the font property of the text view rather than passing it as the value of NSFontAttributeName in the attributes dictionary. (Maybe this part is not (well) documented?)

About your attributes

lineSpacing is calculated from the bottom of the line to the bottom of the upper line and that space is constrained to values between minimumLineHeight and miximumLineHeight. What I am trying to say is that maybe some values in your attributes are cancelling or overriding others.

Also if you need to just adjust the spacing between line you probably don't need to use paragraphStyle.lineHeightMultiple :)

The code

This worked for me:

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.minimumLineHeight = 35.f;
paragraphStyle.maximumLineHeight = 35.f;

UIFont *font = [UIFont fontWithName:@"AmericanTypewriter" size:18.f];
NSString *string = @"This is a test.\nWill I pass?\n日本語のもじもあるEnglish\nEnglish y Español";
NSDictionary *attributtes = @{
    NSParagraphStyleAttributeName : paragraphStyle,
};
self.textView.font = font;
self.textView.attributedText = [[NSAttributedString alloc] initWithString:string
                                 attributes:attributtes];

Additional Notes

There seems to be a situation with Japanese/Chinesse and maybe other characters mixed with alphabet characters in the same line. It will make that line to have a bigger leading to solve that you need to set up the minimum and maximum line height as I did. You can see the problem when rendering my example without attributes.

Solution 3

Setting maximumLineHeight seems to resolve this issue for me;

CGFloat fontSize = 22.f;
titleLabel.font = [UIFont boldSystemFontOfSize:fontSize];
NSMutableParagraphStyle *paragraphStyle = [[[NSMutableParagraphStyle  alloc] init] autorelease];
paragraphStyle.maximumLineHeight = fontSize/2;
titleLabel.attributedText = [[[NSAttributedString alloc]
                              initWithString:@"This is a test.\nWill I pass?"
                              attributes: @{ NSParagraphStyleAttributeName : paragraphStyle,
                                             NSFontAttributeName : titleLabel.font}]
                             autorelease];

overlapping lines

Solution 4

For this particular string you need to set paragraphSpacing instead. What's about lineSpacing, I believe it's just not supported yet on iOS.

Share:
41,137
borrrden
Author by

borrrden

Maintainer of the Couchbase Lite .NET repo

Updated on July 21, 2022

Comments

  • borrrden
    borrrden almost 2 years

    A simple test that is failed: Make a new project with just one subview (UITextView) and put the following in:

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        paragraphStyle.lineHeightMultiple = 50.f;
        paragraphStyle.lineSpacing = 100.f;
        paragraphStyle.minimumLineHeight = 200.f;
        paragraphStyle.maximumLineHeight = 500.f;
    
        UIFont *font = [UIFont fontWithName:@"AmericanTypewriter" size:24.f];
    
        self.textView.attributedText = [[NSAttributedString alloc] initWithString:
        @"This is a test.\n Will I pass?" attributes:
        @{NSParagraphStyleAttributeName : paragraphStyle, NSFontAttributeName : font}];
    }
    

    Line spacing is the same as if the attribute were not there. Has anything got this to work successfully? I put in ridiculous numbers just to show that it won't change...