Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UILabel with NSAttributedString is clipping content

I've got a UILabel set up with auto layout in such a way that its height is based on its intrinsic content size, such that it gets taller when there are more lines in it. I need it to be centered alongside other elements in the same view. With everything default, it works just fine.

However, I'm using a custom font that has a bit too much space in it. I've set up an NSMutableParagraphStyle, like so:

NSMutableParagraphStyle *headlineParagraphStyle = [NSMutableParagraphStyle new];
headlineParagraphStyle.lineSpacing = 0.0f;
headlineParagraphStyle.maximumLineHeight = 20.0f;
headlineParagraphStyle.hyphenationFactor = 0.0f;
headlineParagraphStyle.alignment = NSTextAlignmentLeft;

I'm then creating and setting an NSAttributedString as the UILabel's -attributedText:

NSString *uppercaseHeadline = self.currentStory.headline.uppercaseString;
NSAttributedString *attributedHeadline = [[NSAttributedString alloc] initWithString:uppercaseHeadline attributes:@{NSParagraphStyleAttributeName: headlineParagraphStyle}];
self.headlineLabel.attributedText = attributedHeadline;

The result is that the text looks okay, but it's shoved up above the top of the UILabel and clipped off at the top, while there's still extra space at the bottom of the label:UILabel Clipping

This also throws off the centering of other items against the text in this label, since you can see that the space between the two lines does not line up with the center of the label's frame.

How can I tell UILabel to recenter this text, so that the top doesn't clip and there isn't any space at the bottom?

like image 882
Cocoatype Avatar asked Sep 08 '13 20:09

Cocoatype


1 Answers

I've realized I never came back and answered this question after iOS 7 was released and the NDA on that platform lifted. As of iOS SDK 7.0, it is possible to use the NSAttributedString attribute NSBaselineOffsetAttributeName, which did exactly what I needed to do.

It was available but "no longer supported" in iOS 6. However, when building with the iOS 7 SDK, it appeared to do the right thing.

Edit: In case it's unclear, I don't recommend doing this. If Apple says it's no longer supported, it's probably not a good idea to rely on it. It worked for me, but it's definitely not a long-term solution.

like image 106
Cocoatype Avatar answered Oct 11 '22 20:10

Cocoatype