如何在iOS 7.0或更高版本中获得自动调整的字体大小?

我想在UILabel或UITextField中缩小一些文本的字体大小。 这是可能的iOS 7.0之前: 如何获得UILabel(UITextView)自动调整字体大小? 。 但是,sizeWithFont已经在iOS 7.0中被弃用了。 我试过使用它的replace,sizeWithAttributes,但没有成功。 有没有办法在iOS 7.0中做到这一点?

我真的很喜欢@ WaltersGE1的答案,所以我把它做成了UILabel的扩展,使它更便于使用:

extension UILabel { /// The receiver's font size, including any adjustment made to fit to width. (read-only) /// /// If `adjustsFontSizeToFitWidth` is not `true`, this is just an alias for /// `.font.pointSize`. If it is `true`, it returns the adjusted font size. /// /// Derived from: [http://stackoverflow.com/a/28285447/5191100](http://stackoverflow.com/a/28285447/5191100) var fontSize: CGFloat { get { if adjustsFontSizeToFitWidth { var currentFont: UIFont = font let originalFontSize = currentFont.pointSize var currentSize: CGSize = (text! as NSString).sizeWithAttributes([NSFontAttributeName: currentFont]) while currentSize.width > frame.size.width && currentFont.pointSize > (originalFontSize * minimumScaleFactor) { currentFont = currentFont.fontWithSize(currentFont.pointSize - 1) currentSize = (text! as NSString).sizeWithAttributes([NSFontAttributeName: currentFont]) } return currentFont.pointSize } return font.pointSize } } } 

这里是我得到一个UILabel的调整字体大小的函数:

迅速

 func getApproximateAdjustedFontSizeWithLabel(label: UILabel) -> CGFloat { if label.adjustsFontSizeToFitWidth == true { var currentFont: UIFont = label.font let originalFontSize = currentFont.pointSize var currentSize: CGSize = (label.text! as NSString).sizeWithAttributes([NSFontAttributeName: currentFont]) while currentSize.width > label.frame.size.width && currentFont.pointSize > (originalFontSize * label.minimumScaleFactor) { currentFont = currentFont.fontWithSize(currentFont.pointSize - 1) currentSize = (label.text! as NSString).sizeWithAttributes([NSFontAttributeName: currentFont]) } return currentFont.pointSize } else { return label.font.pointSize } } 

Objective-C的

 - (CGFloat)getApproximateAdjustedFontSizeWithLabel:(UILabel *)label { if (label.adjustsFontSizeToFitWidth) { UIFont *currentFont = label.font; CGFloat originalFontSize = currentFont.pointSize; CGSize currentSize = [label.text sizeWithAttributes:@{NSFontAttributeName : currentFont}]; while (currentSize.width > label.frame.size.width && currentFont.pointSize > (originalFontSize * label.minimumScaleFactor)) { currentFont = [currentFont fontWithSize:currentFont.pointSize - 1]; currentSize = [label.text sizeWithAttributes:@{NSFontAttributeName : currentFont}]; } return currentFont.pointSize; } else { return label.font.pointSize; } } 

文本应用后检查(也许强制视图重新加载第一),你可以抓住字体大小。 例如,使用UITextField,可以实现textDidChange的委托方法,然后从textField的font属性中找出字体大小。 对于标签,您可以更新文本,然后检查字体大小。

例子我构build了一个从UITextField更新UILabel的项目

 -(void)textFieldDidEndEditing:(UITextField *)textField{ self.label.text = textField.text; CGSize initialSize = [_label.text sizeWithAttributes:@{NSFontAttributeName:_label.font}]; while ( initialSize.width > _label.frame.size.width ) { [_label setFont:[_label.font fontWithSize:_label.font.pointSize - 1]]; initialSize = [_label.text sizeWithAttributes:@{NSFontAttributeName:_label.font}]; } CGFloat actualSize = _label.font.pointSize; NSLog(@"acutal size %f",actualSize ); } 

actualSize似乎是正确的。 ( 尺寸计算来源 )

到目前为止,所有的解决scheme都采用整数字体大小,并且不考虑UILabelminimumScaleFactor属性。

像这样的东西应该工作:

 + (CGFloat)fontSizeForLabel:(UILabel *)label { if (label.adjustsFontSizeToFitWidth == NO || label.minimumScaleFactor >= 1.f) { // font adjustment is disabled return label.font.pointSize; } CGSize unadjustedSize = [_label.text sizeWithAttributes:@{NSFontAttributeName:_label.font}]; CGFloat scaleFactor = label.frame.size.width / unadjustedSize.width; if (scaleFactor >= 1.f) { // the text already fits at full font size return label.font.pointSize; } // Respect minimumScaleFactor scaleFactor = MAX(scaleFactor, minimumScaleFactor); CGFloat newFontSize = label.font.pointSize * scaleFactor; // Uncomment this if you insist on integer font sizes //newFontSize = floor(newFontSize); return newFontSize; }