You are the one saying:
lbl.minimumScaleFactor = 0.1
That line means, "Hey, iOS, if you feel like shrinking the font, go ahead and shrink it." If you don't want that, then don't say that.
Instead, pick a font size and stick to it. What size? Well, NSString has the ability to tell you the size of a drawn string in a given font/size. So just take your longest string and cycle through different sizes until you find one that fits in the desired space.
Plug your own values into this:
func fontSizeToFit(s:String, fontName:String, intoWidth w:CGFloat) -> CGFloat? {
let desiredMaxWidth = w
for i in (8...20).reverse() {
let d = [NSFontAttributeName:UIFont(name:fontName, size:CGFloat(i))!]
let sz = (s as NSString).sizeWithAttributes(d)
if sz.width <= desiredMaxWidth {
return(CGFloat(i))
}
}
return nil
}
print(fontSizeToFit("Transferation", fontName:"GillSans", intoWidth:50)) // 9.0
(There used to be an elegant string drawing feature where you could let the font shrink and NSString would tell you how much it shrank to fit; but that feature is broken, alas, so I can't advise you to use it.)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…