swift_动态获取文字高度

   //根据文字获取高度
    func caculateHeight(commemt : String, fontSize : CGFloat, showWidth : CGFloat) -> CGFloat {
        let font = UIFont.systemFont(ofSize: fontSize)
        let  size = commemt.boundingRect(with: CGSize(), options: .usesFontLeading, attributes: [NSAttributedString.Key.font : font], context: nil)
        
        return (size.height - 2 * size.origin.y) * (size.width / showWidth)
    }

猜你喜欢

转载自blog.csdn.net/NoPolun_iOS/article/details/84658001