I've been looking for something to dynamically adjust my fontSize
to fit the width of my layout box. But all the answers I can find, is either to use this:
label.adjustsFontSizeToFitWidth = true
Which does work. But only if I don't have setTranslatesAutoresizingMaskIntoConstraints
set to false
.
Please note that I don't use storyboards. So to have full control over my other constraints I need this line:
label.setTranslatesAutoresizingMaskIntoConstraints(false)
So how can I adjust the font size to fit the width without using storyboard and when I can't use adjustsFontSizeToFitWidth
.
After I figure out how to adjust the font size to fit the width. I also need to adjust the height of the layout box to fit the font size. There seems to be documentation on that though, but if you happen to know the answer of this as well, it would be greatly appreciated.
Thanks in advance
Try the following commands for your label:
label.adjustsFontSizeToFitWidth = true label.minimumScaleFactor = 0.2
And try to change the lines of the label to 0 and 1 (check both cases):
label.numberOfLines = 0 // or 1
MinimumScaleFactor
range is 0 to 1. So we are setting the MinimumScaleFactor
with respect to our font size as follows
Objective C
[lb setMinimumScaleFactor:10.0/[UIFont labelFontSize]]; lb.adjustsFontSizeToFitWidth = YES;
Swift 3.0
lb.minimumScaleFactor = 10/UIFont.labelFontSize lb.adjustsFontSizeToFitWidth = true
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With