简体   繁体   中英

UILabel set text vertically

I have a UILabel that I want to expand vertically rather than horizontally after some 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

However, I always have a width that is much larger than my maximumWidth.

I tried using preferredMaxLayoutWidth but this did not work.

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.

You need to use NSLineBreakByCharWrapping instead of 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

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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