There is an NSString method, -sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode: which, apparently, exists with iOS 2.0, but, unfortunately, it is not recommended in iOS 7 without the proposed alternative, since automatic font size reduction is not recommended . I really don’t understand Apple’s position on this, since they use it in the key record, etc., And I think that if the font sizes are within a small range, everything is in order. Here is an implementation in Swift using this method.
var newFontSize: CGFloat = 30 let font = UIFont.systemFontOfSize(newFontSize) (self.label.text as NSString).sizeWithFont(font, minFontSize: 20, actualFontSize: &newFontSize, forWidth: self.label.frame.size.width, lineBreakMode: NSLineBreakMode.ByWordWrapping) self.label.font = font.fontWithSize(newFontSize)
I do not know how this can be achieved without using obsolete methods.
Nick Jul 07 '14 at 11:34 2014-07-07 11:34
source share