1

I have been using this code for quite a long time in my iOS 5/6 apps. It's under viewWillAppear: and manipulates a UILabel height do that it can fit more text in as needed (and pushes other labels below it down). The problem is, with iOS 7, it doesn't appear to be working anymore. The text simply doesn't wrap to the second line. Here is the code:

self.titleLabel.text = titleString;
self.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
self.titleLabel.numberOfLines = 0;

// Determine what the size of the title label should be
CGSize maximumLabelSize = CGSizeMake(163,66);
CGSize expectedLabelSize = [titleString sizeWithFont:self.titleLabel.font
                                           constrainedToSize:maximumLabelSize
                                            lineBreakMode:self.titleLabel.lineBreakMode];

// Adjust the the new height of the title label.
CGRect newFrame = self.titleLabel.frame;
newFrame.size.height = expectedLabelSize.height;
self.titleLabel.frame = newFrame;

NSLog(@"Title Height: %f", self.titleLabel.frame.size.height);

Here's a bit more information. This UILabel is on a header view in a UITableView. It's designed with Interface Builder and an IBOutlet to the label. The properties of the UILabel are identical between both the iOS 6 and iOS 7 project.

Here is what is looks like with Xcode 4 running against iOS 6:

enter image description here

And here is Xcode 5 with iOS 7 (as you can see, the text doesn't wrap down):

enter image description here

Anyone have any ideas?

enter image description here

Ethan Allen
  • 14,425
  • 24
  • 101
  • 194
  • Have you set the number of lines on the UILabel to a high enough value? – Linuxios Feb 11 '14 at 17:31
  • The text rendering in iOS 7 has quirks that people have notice, try giving the label a few pixels more height (+5 maybe), and see if it wraps properly – Jack Feb 11 '14 at 17:32
  • Even with a + 10 added to the height, still get nothing. It's like the label gets bigger but the text doesn't wrap or appear in the area. – Ethan Allen Feb 11 '14 at 17:49
  • possible duplicate: http://stackoverflow.com/questions/9059631/autoshrink-on-a-uilabel-with-multiple-lines – NeverHopeless Feb 11 '14 at 18:21

3 Answers3

1

Like @Jack Wu said, try giving the label a few pixels. Also is iOS 7 sizeWithFont has been replaced by boundingRectWithSize:

GSize maximumLabelSize = CGSizeMake(163,66);
NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:titleString attributes:@{NSFontAttributeName: self.titleLabel.font}];
CGRect rect = [attributedText boundingRectWithSize:maximumLabelSize
                                           options:NSStringDrawingUsesLineFragmentOrigin
                                           context:nil];
CGSize expectedLabelSize = rect.size;
jbouaziz
  • 1,484
  • 1
  • 12
  • 24
0

Ensure that your UILabel is the correct height needed to display the text.

Then make sure that you are not changing the numberOfLines on the UILabel.

If you want the UILabel to use as many lines at it needs then set the number of lines to 0

myLabel.numberOfLines = 0;
AppHandwerker
  • 1,758
  • 12
  • 22
0

Although you're printing the size, and I guess if it were different you would have said that. What might be happening is the size being changed at display time because of some constraint or resizing option. One thing you can do to test if that is the issue is to set a backgroundColor on the label, to see if it actually is the correct size vertically. Also set a backgroundColor for the header view. That is usually what I do to make sure things have the correct size.

Also, the sizeWithFont: method is deprecated on iOS7.

You can use this drop in replacement I got on this answer here:

UITableViewCell with UITextView height in iOS 7?

Community
  • 1
  • 1
manecosta
  • 8,682
  • 3
  • 26
  • 39