GBA-8-19/CGame/Extensions/UILabel+FontSize.swift
bluesea 66c5202be8 s
2024-06-14 18:12:40 +08:00

29 lines
837 B
Swift

//
// UILabel+FontSize.swift
// Hthik
//
// Created by Hthik on 12/25/15.
// Copyright © 2015 Hthik. All rights reserved.
//
import UIKit
internal extension UILabel
{
var currentScaleFactor: CGFloat
{
guard let text = self.text else { return 1.0 }
let context = NSStringDrawingContext()
context.minimumScaleFactor = self.minimumScaleFactor
// Using self.attributedString returns incorrect calculations, so we create our own attributed string
let attributedString = NSAttributedString(string: text, attributes: [.font: self.font!])
attributedString.boundingRect(with: self.bounds.size, options: [.usesLineFragmentOrigin, .usesFontLeading], context: context)
let scaleFactor = context.actualScaleFactor
return scaleFactor
}
}