Question

My view controller has a UILabel. It displays a dollar amount to the user. I have a default size that I like the dollar amount to be displayed as, shown below

good

When the amount is too large, some text is replaced with "..."

bad

I want to adjust the text of the UILabel (make it smaller) such that the point size of the text is just enough to not cause the dots to appear. Is there some quick math I can do to accomplish this?

Était-ce utile?

La solution

label.adjustsFontSizeToFitWidth = YES;
label.numberOfLines = 1;

Autres conseils

No need for you to make the math, the system-provided API can do it for you. In Interface Builder, set the label's autoshrink to minimum scale factor of 0.7 (or whatever value fits your need). Text will now shrink as necessary.

enter image description here

Use this:

UILabel * label = // your label

// update from @rmaddy and @z s
label.adjustsFontSizeToFitWidth = YES;
label.minimumScaleFactor = 0.5;

This means that you'll allow your text to shrink to up to half its size before truncating. Adjust accordingly.

If you're working in a pre iOS 7 environment, you can also use:

UILabel * label;
label.minimumFontSize = 14;

To set a specific minimum font size; however, this has been deprecated since iOS 7

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top