Sergio del Amo
Sergio del Amo

Reputation: 78096

How to set correctly the line height of UILabel with an AttributedString?

I have a UILabel which uses an Attributed String. I want to have its line height to be exactly the size of the font size, not a pixel bigger. However, a top and bottom padding are being added. See image below:

enter image description here

This is my code to create the label.

NSDictionary *basicAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor], NSBackgroundColorAttributeName: [UIColor blueColor]};

NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithDictionary:basicAttributes];

UIFont *font = [UIFont fontWithName:@"AvenirNext-Regular" size:20.0];
[attributes setObject:font forKey:NSFontAttributeName];

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineHeightMultiple = 1.0f;
[attributes setObject:paragraphStyle forKey:NSParagraphStyleAttributeName];

self.helloWorldLabel.attributedText = [[NSMutableAttributedString alloc] initWithString:@"Hola" attributes:attributes];

Attempts

I have tried to call sizeToFit after setting the attributedText without success.

[self.helloWorldLabel sizeToFit];

I have played with other attributes of NSMutableParagraphStyle such as lineSpacing without success.

paragraphStyle.lineSpacing = 0.0f;

What am I missing?

Thanks in advanced

Upvotes: 2

Views: 4661

Answers (2)

propstm
propstm

Reputation: 3629

It sounds like you want to use boundingRectWithSize: Here's a short example of how to use it. This code will dynamically determine your label size based on the text in your label. Setting the constraining sizes allows a max size if content needs to overflow to multiple lines.

    NSString *text = [NSString stringWithFormat:@"Title Header:%@", value];
    NSRange boldRange = [text rangeOfString:@"Title Header:"];
    NSRange normalRange = [text rangeOfString:value];
    NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:text];

    //Add attributes for appropriate ranges
    [attributedText setAttributes:@{ NSFontAttributeName:[UIFont fontWithName:@"HelveticaNeue-Bold" size:13.0f]} range:boldRange];
    [attributedText setAttributes:@{ NSFontAttributeName:[UIFont fontWithName:@"HelveticaNeue" size:13.0f]} range:normalRange];

    //Determine rect for attributed text constrained within max values
    CGRect textAttributedSize = [attributedText boundingRectWithSize:CGSizeMake(CellTitleWidth, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:NULL];
   [self.myLabel setText:attributedText]; 
   [self.myLabel setFrame:textAttributedSize];

Upvotes: 2

George Petrov
George Petrov

Reputation: 163

Check this link. There is no 100% way to correctly set the frame to fit the letters. You can calculate the attributed string with CoreText like using CTFramesetterRef CTFramesetterSuggestFrameSizeWithConstraints but it is more additional work.

Upvotes: 1

Related Questions