UILabel with NSAttributedString is clipping content

I have a UILabel configured with auto-layout so that its height is based on its internal size of the content, so that it gets taller if it has more lines. I need this to be concentrated along with other elements in the same view. With all the defaults, it works fine.

However, I use my own font, which has too much space. I installed NSMutableParagraphStyle, for example:

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

Then I create and set the NSAttributedString as UILabel -attributedText :

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

As a result, the text looks fine, but it grabbed the top of the UILabel and cut off at the top, while there is still extra space at the bottom of the label: UILabel Clipping

This also discards the centering of other elements on the text in this label, as you can see that the space between the two lines does not coincide with the center of the label frame.

How can I tell UILabel to re-enter this text so that the top does not crop and there is no space below?

+7
ios cocoa-touch uilabel
source share
4 answers

I realized that I never returned and answered this question after the release of iOS 7, and the NDA on this platform rose. Starting with iOS SDK 7.0, you can 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 creating with the iOS 7 SDK, it turned out to be right.

Edit: If this is unclear, I do not recommend doing this. If Apple claims that it is no longer supported, you probably should not rely on it. It worked for me, but it is definitely not a long term solution.

+3
source share

I consider it important to relate myself to a “strange interview”: http://i.imgur.com/pFeqPHd.gif .

You may need to edit the ascender property of the font, see here: UIButton custom vertical font alignment

+2
source share

I just had a wild ride with this. For some reason, using lineHeightMultiple , maximumLineHeight and / or minimumLineHeight performs an offset like this.

However, using lineSpacing (which is not an absolute value, but a relative value) changes the distance between the lines without spoiling the offset.

+2
source share

In iOS 10, my solution was to set the maximum value of MaximumLineHeight and NOT set the minimum line (otherwise, the top of the label is truncated). Changing lineSpacing or lineHeightMultiple did not help.

+1
source share

All Articles