我想在不同的iOS设备上制作一个不同的标签,但是我不能根据其高度来更改其字体大小。好吧,但是,只有在不首先设置字体的情况下,我才可以,但是这是一个问题,因为我需要这种字体。函数(我正在使用SnapKit进行布局):
fileprivate func initNameLabel() {
let user = UserManager.shared.getCurrentUser()
self.userNameLabel = UILabel()
self.userNameLabel?.text = user?.fullName
self.userNameLabel?.font = .openSans(size: Sizes.userNameLabelFont)
self.userNameLabel?.numberOfLines = 1
self.userNameLabel?.minimumScaleFactor = 0.5
self.userNameLabel?.adjustsFontSizeToFitWidth = true
self.containerView?.addSubview(self.userNameLabel!)
self.userNameLabel?.snp.makeConstraints({ (make) in
make.top.equalTo(self.avatarImageView!.snp.bottom).offset(Sizes.offset)
make.height.equalTo(Sizes.userNameLabelHeight)
make.centerX.equalTo(self.containerView!.snp.centerX)
})
}
以及字体扩展名:
extension UIFont {
static func openSans(size: CGFloat) -> UIFont {
return UIFont(name: "OpenSans", size: size) ?? UIFont.systemFont(ofSize: size)
}