How to check current font size of UILabel?

后端 未结 3 1749
天涯浪人
天涯浪人 2021-01-25 06:05

I have 3 UILabels and I want to set same (the smallest one from all of 3 possibilities) font for all of them. What is the problem? I don\'t know how to check the cu

3条回答
  •  感情败类
    2021-01-25 06:51

    use this extention

    extension String {
    
           func height(withConstrainedWidth width: CGFloat, font: UIFont) -> CGFloat {
                let constraintRect = CGSize(width: width, height: .greatestFiniteMagnitude)
                let boundingBox = self.boundingRect(with: constraintRect, options: .usesLineFragmentOrigin, attributes: [NSFontAttributeName: font], context: nil)
    
                return ceil(boundingBox.height)
            }
    }
    

    get height

    height = strDesc.height(withConstrainedWidth: UIScreen.main.bounds.size.width - 160, font: UIFont.systemFont(ofSize: 14.0))
    

提交回复
热议问题