Autolayout preventing 3-line label from having more than two lines

扶醉桌前 提交于 2019-12-11 13:29:20

问题


I am laying out a UICollectionViewCell in IB, and one of the UILabels I am adding is not behaving the way I would expect with autolayout. The desired behavior is: the label's width is at most 385px; the label's right edge stays at least 30px away from the edge of it's container; the label takes up as much vertical space as necessary to fit up to three lines of text.

I have working constraints set up to handle the width (and padding). The problem is with the calculated frame height of label. In IB I have the label laid out as 385x80 (the max width, and enough height to hold three lines of text). FWIW, I have three lines of text in the label in IB.

Once the app actually loads, though, the label is never displayed with more than two lines of text. It correctly sizes the label for one or two lines, but three.

I have tried various additional constraints to try and coerce it to be taller, and also messed with the priority to try to and get my constraints to work with the intrinsic height better, but nothing has worked.

From reading various things, I suspect this could be handled by setting up the constraints programmatically, but at this point I'm not sure what I would do differently that how I have things set up in IB.

Edit: A couple other things that could be relevant. I have another label that behaves nearly the same way, and has no problem getting over two lines (though sometimes it will be six lines truncated, where it should be seven). The only difference between the two is the font size. There are also some cases where the displayed label will be one line truncated, rather than two lines, while the next cell with have two lines. I can't figure that out either. Finally, for the label in question, it will have a custom (non-native) font. I suspected that perhaps the autolayout was sizing the label for the native system font (which is smaller), but even taking away the custom font I still see this incorrect behavior.

来源:https://stackoverflow.com/questions/22046350/autolayout-preventing-3-line-label-from-having-more-than-two-lines

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!