3

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): enter image description here

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:

enter image description here

Shanti K
  • 2,873
  • 1
  • 16
  • 31
  • 1
    How do you define size constraints for your cells ? – Antzi Nov 18 '14 at 17:17
  • it seems that bottom space constraint is also been set, if this is the case then jst remove this bottom space constraint, better if yo u share the constraint screeshot – ZAZ Nov 18 '14 at 17:22
  • @ZAZ.. no the bottom constraint is not set.. – Shanti K Nov 19 '14 at 04:58
  • @Antzi: I have not set any size constraints for the cells. – Shanti K Nov 19 '14 at 06:37
  • Your `UILabel` adapt to fill the space available (due to the absence of a label such as `Original food choice`.) You have to decide on non ambiguous rules to set the correct sizing in case one of the label is empty (maybe set a minimum size to both labels ?) – Antzi Nov 19 '14 at 06:44
  • @Antzi.. the original food choice label is present too.. – Shanti K Nov 19 '14 at 06:51
  • @Antzi.. For the same cell, if i increase the character count to beyond a certain point, label height increases.. – Shanti K Nov 19 '14 at 06:52
  • @ShantiK Try setting a minimum height for the food choice label – Antzi Nov 19 '14 at 06:55

3 Answers3

3

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 =)

Sori Alex
  • 61
  • 2
2

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.

CyberInfo
  • 240
  • 2
  • 9
0

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.

Community
  • 1
  • 1
Evan R
  • 875
  • 13
  • 27