2

I have a label that is supposed to wrap around. There are 4 constraints on it (top/bottom and leading/trailing - no other constraints). The label wraps around correctly on iPhone 6 i.e. 4.7" screen and iPhone 6 Plus i.e. 5.5" screen, But gets cut off on the right edge on 4" inch or smaller screen devices. Please see the screenshot below.

enter image description here

It's as if it thinks available width is more than actual device width. any ideas? (sorry can't post image inline because it requires at least 10 rep)

FWIW: Here are the constraints on the label (I am writing these in pseudo code because these are set in IB).

label.top = top margin
label.bottom = bottom margin
label.leading = leading margin
label.trailing = trailing margin
ishaq
  • 1,781
  • 1
  • 11
  • 19
  • 1
    a.What is the earliest iOS you support? b.Is your label lines number in the Storyboard is 0? c.Did you try to do label.sizeToFit() after assigning the text? – MatanGold Jan 25 '16 at 14:27
  • I am not supporting anything lower than iOS 9.0. yes number of lines is set to 0. I don't think it's a sizeToFit issue because it's not displaying one long line of text. it's wrapping, but at wrong point. – ishaq Jan 25 '16 at 14:30
  • 1
    @Quentin thanks for editing the image to be inline. – ishaq Jan 25 '16 at 14:31
  • Try to delete the constraints and add them again but with the "Related to margin" option UN-checked – MatanGold Jan 25 '16 at 14:38
  • @MatanGold thanks, actually i have already tried that. doesn't help. – ishaq Jan 25 '16 at 14:39
  • Try to justify your lable text. label.textAlignment = NSTextAlignment.Justified. If this will not work can you post the constraints code or image for better understanding. – Ramesh_T Jan 25 '16 at 14:43

2 Answers2

0

In your main_storyboard go to the bottom panel and click the Compact sizes inspector(or whatever how its called) and chose this one

enter image description here

try to remove there all the constrains and add them again

0

I found out what the issue was, This label was in a cell of type TextCell, and here's what I was doing in TextCell's layoutSubviews method:

override func layoutSubviews() {
    super.layoutSubviews()

    // ... snip ...

    self.text.preferredMaxLayoutWidth = 375 // This is the offending line
}

Change preferredMaxLayoutWidth to a sane value (e.g. CGRectGetWidth(self.label.frame)) fixed it.

Please note that the above is a simplification of what the issue was, the point being when you have issues with a self sizing label in Cocoa Touch, always check that preferredMaxLayoutWidth is being set to correct value

Here's a related question (pointed me in the correct direction): UILabel not wrapping text correctly sometimes (auto layout)

Community
  • 1
  • 1
ishaq
  • 1,781
  • 1
  • 11
  • 19