简体   繁体   中英

String Fit to Fix UILabel Height

I followed a that told to use "TruncatingTail" not "WordWrapping" so I used this. But still not able to find the way to get the best optimal output as attached in image.

lbl.text = "1 Year\n=\n365 Opportunities"
lbl.numberOfLines = 0;
lbl.lineBreakMode = .byTruncatingTail;
lbl.adjustsFontSizeToFitWidth = true;
lbl.minimumScaleFactor = 0.1;
lbl.textAlignment = .center;
lbl.backgroundColor = .red
lbl.font = UIFont.init(name:"SourceCodePro-Regular", size: 80)

在此处输入图片说明

lbl.numberOfLines = 0;
lbl.lineBreakMode = .byWordWrapping;

在此处输入图片说明

I am building an App for Quotes, but I am stuck at this point. I have tried all various ways but not able to succeed. Please anyone can help so it will be a great help to me.

Thank you.

From the UILabel documentation for lineBreakMode :

Specifies what happens when a line is too long for the label's bounds. Character wrap and word wrap are most commonly applied to multiline labels and determine the position of line breaks between consecutive lines. Select word wrap to place line breaks at word boundaries, or character wrap to insert line breaks in words. Truncate head, middle, and tail are usually applied for single-line labels, and describe the placement of an inserted ellipsis to represent the truncated text. Access this value at runtime with the lineBreakMode property.

Truncating the tail doesn't apply when you allow as many lines as required ( numberOfLines = 0 ). Switch back to using byWordWrapping and your auto-shrink settings should do the rest.

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