2

iOS 8.4:

Multi-line label works in iOS8.4

Exact same code on iOS 9:

Multi-line label doesn't work in iOS9

As you can see, exact same code being run on iOS 8.4 yields different results to iOS 9.

On iOS 9, the second line shows for a split second when the view is first loaded and then shrinks back to its single line state. This view is a Header Collection Reusable View.

I tried removing all other views and unrelated constraints in the header view whilst leaving only the label in question (with 3 constraints) but still yields similar results, so it couldn't be caused by other constraints in the view. All other views removed, still same results

Was wondering if anyone else is experiencing this and know of a solution?

EDIT 1:

Configurations for the UILabel:

Attributes Layouts

EDIT 2:

I created a demo project here: https://github.com/v-ken/TestMultilineUILabeliOS9. I located the source of the problem to be reloadData. When it's called, the UILabel shrinks back to a single line.

EDIT 3:

Demo GIT repository above is now updated with the fix.

Community
  • 1
  • 1
ken
  • 3,897
  • 3
  • 22
  • 28
  • Add the label setup code as well as exact string from above example. I'm not having your problem. – David H Oct 19 '15 at 10:53
  • Thanks for giving that a go David. Did you try it inside a collection reusable header view? – ken Oct 19 '15 at 11:09
  • Please, how is the label configured, and the exact string. Edit your question. – David H Oct 19 '15 at 14:24
  • Just added the configuration for the UILabel. The string doesn't matter as long as it's long enough that should span 2 lines. – ken Oct 20 '15 at 00:44
  • iOS 9 uses the San Francisco font, which is different than the iOS 8 font, Helvetica Neue. Most likely the difference in font is causing the difference (i.e. it won't fit). – saagarjha Oct 20 '15 at 01:08
  • The other thing that bothers me is that your cell shows other views - in particular the right view with the two icons. Yet you tell your label it has full range to some offset from the right side? In my experience, if you want to use Autolayout with Cells, you need to provide all views and all constraints between those views. Just my experience. – David H Oct 20 '15 at 01:57
  • @DavidH, the image above with the constraints is only an example of me trying to remove all other views and all other constraints (not related to the UILabel) for testing to ensure that this behaviour isn't caused by some other misconfigured constraint. The original layout do have constraints between the label and the icons. – ken Oct 20 '15 at 02:22

1 Answers1

3

Finally managed to get it working for iOS 9 by setting the UILabel's preferredMaxLayoutWidth in the UIView.

- (void)layoutSubviews {
    [super layoutSubviews];
    myLabel.preferredMaxLayoutWidth = myLabel.frame.size.width;
}

Or, if you're using a UIViewController:

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    myLabel.preferredMaxLayoutWidth = myLabel.frame.size.width;
    [self.view layoutIfNeeded];
}

Reference:

https://stackoverflow.com/a/20287811/2491738

Community
  • 1
  • 1
ken
  • 3,897
  • 3
  • 22
  • 28