iOS/自动布局:使用自动布局使UILabel转到多行 [英] IOS/Autolayout: Make UILabel go to more than one line with Autolayout

查看:75
本文介绍了iOS/自动布局:使用自动布局使UILabel转到多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果文本需要使用自动布局,我需要获得UILabel才能转到多行.在代码和情节提要中,为了达到良好的效果,我将numberOfLines设置为0并进行了自动换行,并且我也称为sizeToFit.

I need to get a UILabel to go to more than one line if text requires with autolayout. In code and also in storyboard for good measure I have set numberOfLines to 0 and wordwrapping on and I've also called sizeToFit.

如果我将标签的高度限制设置为足够大的值(例如= 200),则全部文本将显示在多行上.但是对于短文本,它不会收缩,而会留出很多空白.但是,如果我将高度限制设置为一个较低的值,例如> = 21,则它仅显示一行,并切断其他所有内容.

The full amount of text will display on multiple lines if I set the height constraint for the label to a large enough value such = 200. But with short text, it does not contract, leaving a lot of white space. But if I set the height constraint to a lower value such as >=21 then it shows only one line and cuts off everything else.

视图层次结构为View-Scrollview-Contentview-elements我为最低元素设置了一个底部常量,优先级为250,因此应该对其进行调整.自动版式显示无错误

The view hierarchy is View-Scrollview-Contentview-elements I set a bottom constant for the lowest element with a priority of 250 so it should adjust. Autolayout shows no errors

但是,只要没有为标签创建巨大的高度限制,标签就可以扩展到多行.

Nothing, however, short of creating a huge height constraint for the label lets the label extend to multiple lines.

非常感谢您提供有关解决此问题的建议.

Would appreciate any suggestions on how to solve this issue.

出现在视图中:

_myLabel.lineBreakMode = UILineBreakModeWordWrap;//deprecated but I threw it in too
_myLabel.lineBreakMode = NSLineBreakByWordWrapping;
_myLabel.numberOfLines = 0;
[_myLabel sizeToFit];

// height constraint for label

height>=21//text does not extend more than one line
height=200//text does extend more than one line but leaves lots of whitespace if text is short ie it does not shrink.

推荐答案

为标签和情节提要集numberOfLines = 2提供适当的leading, trailing, top, bottom约束.我指定约束的原因是基于内容的auto-layout will then automatically resize the intrinsic size of the label.希望这是有帮助的.

Give proper leading, trailing, top, bottom constraints to your label and in storyboard set numberOfLines = 2. The reason why i specified constraints is that auto-layout will then automatically resize the intrinsic size of the label based on the content. Hope, this was helpful.

这篇关于iOS/自动布局:使用自动布局使UILabel转到多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆