设置minimumScaleFactor时,UILabel会获得当前比例因子吗?

Sve*_*uer 8 objective-c uilabel ios swift

我有一个UILabel并设置:

let label = UILabel()
label.minimumScaleFactor = 10 / 25
Run Code Online (Sandbox Code Playgroud)

设置标签文本后,我想知道当前的比例因子是什么.我怎样才能做到这一点?

car*_*ine 4

您还需要知道原始字体大小是多少,但我想您可以通过某种方式找到它

也就是说,使用以下函数来发现实际的字体大小:

func getFontSizeForLabel(_ label: UILabel) -> CGFloat {
    let text: NSMutableAttributedString = NSMutableAttributedString(attributedString: label.attributedText!)
    text.setAttributes([NSFontAttributeName: label.font], range: NSMakeRange(0, text.length))
    let context: NSStringDrawingContext = NSStringDrawingContext()
    context.minimumScaleFactor = label.minimumScaleFactor
    text.boundingRect(with: label.frame.size, options: NSStringDrawingOptions.usesLineFragmentOrigin, context: context)
    let adjustedFontSize: CGFloat = label.font.pointSize * context.actualScaleFactor
    return adjustedFontSize
}

//actualFontSize is the size, in points, of your text
let actualFontSize = getFontSizeForLabel(label)

//with a simple calc you'll get the new Scale factor
print(actualFontSize/originalFontSize*100)
Run Code Online (Sandbox Code Playgroud)