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

Wal*_*GE1 11 uikit ios

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

Wal*_*GE1 11

这是我为获取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

    }

}
Run Code Online (Sandbox Code Playgroud)

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;
    }
}
Run Code Online (Sandbox Code Playgroud)


dbb*_*ess 9

我真的很喜欢@ 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
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


jab*_*jab 3

雨燕4

上面的答案效果很好,但使用了一些已弃用的值/函数。这是 2018 年运行的修复版本。

func approximateAdjustedFontSizeWithLabel(_ label: UILabel) -> CGFloat {
    var currentFont: UIFont = label.font
    let originalFontSize = currentFont.pointSize
    var currentSize: CGSize = (label.text! as NSString).size(withAttributes: [NSAttributedStringKey.font: currentFont])

    while currentSize.width > label.frame.size.width && currentFont.pointSize > (originalFontSize * label.minimumScaleFactor) {
        currentFont = currentFont.withSize(currentFont.pointSize - 1.0)
        currentSize = (label.text! as NSString).size(withAttributes: [NSAttributedStringKey.font: currentFont])
    }

    return currentFont.pointSize
}
Run Code Online (Sandbox Code Playgroud)