Multiline UILabel height increases even when the text is one line long - ios

I am using a UILabel within a tableViewCell. Some of the cells have texts that are one line, and some 2 lines. I have set the trailing space, leading space and top space from superview constraints to the label. Following are the settings:
lines = 0
Line Breaks: Word Wrap (tried other options as well)
If the text crosses a particular length, for some reason, the label height seems to have increased to accommodate 2 lines, though the text is only one line. As show below (background is red for reference):
For example, In the first row, the actual text is one line, but the height is increased when compared to the other rows.
Is there any reason why it might be happening so? I am not setting the height anywhere from the code.
EDIT
Also, I have set a custom font for UILabel throughout out the App using the following:
[[UILabel appearance] setFont:myFont];
I am not sure if that should cause any issue..
Also, the screen shot of the constraints set are:

Got the same issue.
After checking content hugging priorities got the same results.
Finally found that it was caused by Label-Preferred Width set to Explicit in Metrics Tab, in label configuration.
Unchecked and solved =)

I know its weird, I did face the same issue before and it was because of the content hugging priorities which i set without much knowledge on it. I solved it by removing the label and adding it again.try that.

I was experiencing this issue with an NSAttributedString, but in conjunction with using the lineSpacing property of NSMutableParagraphStyle. I'm using Auto Layout entirely programmatically and with numberOfLines set to 0. Multiline text was working fine until I wanted to add line spacing, at which time I encountered this issue. Adjusting the content hugging and compression resistance didn't do anything for me, nor did using UILabel's preferredMaxLayoutWidth property (I'm not currently).
What fixed it was applying an NSRange to the exact amount of characters in my text string:
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] init];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = spacing;
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, label.text.length)];
label.attributedText = attributedString;
Taken from this answer: https://stackoverflow.com/a/16057159/482557.

Related

Attributed label with custom line height cropping text on top

I need to decrease line spacing on interface builder using attributed text, and changing the line height multiplier it works as desired, but the text is being cropped on top.
I already tried to set the lineSpacing with NSMutableParagraphStyle by code but did not worked or happened the same thing.
There is a way to fix the alignment or setting the correctly the line spacing?
So it isn't a matter of spacing, but more likely constraints. Perhaps your baseline setting is set to none, in which case try changing it to 'Align Centers'.

IOS NSBaselineOffsetAttributeName causing strange truncation

I have some UILabel that contains a couple of different fonts, which I apply via attributedText.
I have set the lineBreakMode to be UILineBreakModeWordWrap and I have set the size of the UILabel to be the sizeThatFits
And it looks just fine, until I add NSBaselineOffsetAttributeName to it's attributedText
Then I see weird truncation, regardless of how I change the height or width of the UILabel
How do I alter the baseline without affecting truncation?
I took the advice from the post, iOS Why do NSTextAttachment disappear when setting NSBaselineOffsetAttributeName?
and added a new NSMutableParagraphStyle to the the UILabel's attributedText with it's minimumLineHeight set to be the font's size plus the added baseline, and I got the results I was looking for.

Does an NSAttributedString in a UILabel ignore the label’s numberOfLines property?

I need to increase the line spacing in a UILabel, so I’m using its attributedString to do so. Works fine, but it ignores the fact that I had also set the label’s numberOfLines to 2 – it only displays a single line, and truncates.
Any idea why this might be, what I can do about it, to still use two lines, but increase the spacing between them?
It sounds like the frame of your UILabel isn't big enough. Try increasing the height and see if the problem persists.
You can force the label to the correct size for your number of lines by calling sizeToFit after setting the attributedText. To avoid the problem of the shrinking label in your table view cells, reset the frame of the label to some default size before setting the attributedText and calling sizeToFit. This will prevent the label from continuously shrinking.

What is the most concise and semantic way to lay out a paragraph of text of variable length for IOS7?

As far as I know, laying out paragraphs can be done with:
a) UITextView
Resizing the textview to fit the content in didlayoutsubviews
Example here: Weird thing occurs when using sizetofit of a UITextView in IOS7
b) UILabel
Setting the Lines to 0, using sizetofit
Example here: Multiple lines of text in UILabel
However, I am unable to get either of these to work in IOS7 (having previously used them in ios6.x). There must be a definitive and clear way to just lay out a paragraph, its such a seemingly simple task.To be specific, this is just a paragraph of text that is:
non-editable
variable length
Works consistently whether using storyboards or code only
So please, what is the way to do this?
UITextView works fine on iOS 7. If you don't use Auto Layout, then calling sizeToFit on UITextView object should be enough. If you do use Auto Layout, then make a height constraint on UITextView object and set its constant in code in the following way:
CGSize sizeThatFits = [self.textView sizeThatFits:CGSizeMake(yourAvailableWidth, MAXFLOAT)];
self.textViewHeightConstraint.constant = ceilf(sizeThatFits.height);
I've seen some problems with UILabel recently, e.g. Lines missing from tall UILabel when embedding NSTextAttachment
With the UILabel, I was able to to get this working by:
setting the lines to 0
setting my line break mode to word wrap
ensuring that the height constraint is set to a "greater than or equal to"

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:
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?
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.
I just had a wild ride with this. For whatever reason, using lineHeightMultiple, maximumLineHeight, and/or minimumLineHeight blows the offset like this.
However, using lineSpacing (which is not an absolute value but a relative value) will change the spacing between lines without messing up the offset.
In iOS 10, my solution was to only set maximumLineHeight and to NOT set minimumLineHeight (otherwise, the top of the label gets clipped). Changing lineSpacing or lineHeightMultiple did not help.
I feel it's important to link to the 'bizarre interview': http://i.imgur.com/pFeqPHd.gif.
You may need to edit the font's ascender property, see here: UIButton custom font vertical alignment
in my case numberOfLines = 0 was needed
otherwise, counterintuitively, line after the newline were clipped

Resources