我有一个非常简单的UILabel示例,它将调整字体大小以适应宽度。但是,当它缩小文本时,它不会像提供的图像那样拥抱内容高度。使用adjustFontSizeToFitWidth时这是不可能的?
示例代码:
import Foundation
import UIKit
class SignUpViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.white
let label = UILabel()
view.addSubview(label)
label.backgroundColor = UIColor.red
label.adjustsFontSizeToFitWidth = true
label.numberOfLines = 1
label.minimumScaleFactor = 0.25
label.font = UIFont.systemFont(ofSize: 80.0)
label.text = "A really long bit of text that will need to shrink"
label.translatesAutoresizingMaskIntoConstraints = false
label.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 10.0).isActive = true
label.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -10.0 ).isActive = true
label.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
}
}
截图: