简体   繁体   English

UILabel垂直设置文本

[英]UILabel set text vertically

I have a UILabel that I want to expand vertically rather than horizontally after some maximumWidth. 我有一个UILabel,我想在一些maximumWidth之后垂直扩展而不是水平扩展。 Currently I am trying to do this by doing the following: 目前,我正在尝试通过执行以下操作来做到这一点:

    _caption.numberOfLines = 0;
    _caption.lineBreakMode = NSLineBreakByWordWrapping;

and then when I size I use sizeToFit 然后当我调整尺寸时,我使用sizeToFit

However, I always have a width that is much larger than my maximumWidth. 但是,我的宽度总是比我的maximumWidth大得多。

I tried using preferredMaxLayoutWidth but this did not work. 我尝试使用preferredMaxLayoutWidth但这没有用。

I could do a simple calculation by dividing the width by the maximumWidth that I want and then adjust the height accordingly but I'm wondering if there is any way to do this automatically. 我可以通过将宽度除以所需的maximumWidth来进行简单的计算,然后相应地调整高度,但是我想知道是否有任何方法可以自动执行此操作。

You need to use NSLineBreakByCharWrapping instead of NSLineBreakByWordWrapping 您需要使用NSLineBreakByCharWrapping而不是NSLineBreakByWordWrapping

_caption.lineBreakMode = NSLineBreakByCharWrapping

I had tested this using a simple label and storyboard and setting the constraints to top, left and width and here is the result 我已经使用简单的标签和情节提要进行了测试,并将约束条件设置为顶部,左侧和宽度,这就是结果

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM