美文网首页
Swift计算文本的高度、宽度

Swift计算文本的高度、宽度

作者: 凯司机 | 来源:发表于2021-03-20 18:17 被阅读0次

 /// 计算文本的高度

    static func textHeight(text: String,  fontSize: CGFloat, width: CGFloat) -> CGFloat {

        return text.boundingRect(with:CGSize(width: width, height:CGFloat(MAXFLOAT)), options: .usesLineFragmentOrigin, attributes: [.font:UIFont.systemFont(ofSize: fontSize)], context:nil).size.height+5

    }

    /// 计算文本的宽度

    static func textHieght(text: String,  fontSize: CGFloat, height: CGFloat) -> CGFloat {

        return text.boundingRect(with:CGSize(width:CGFloat(MAXFLOAT), height: height), options: .usesLineFragmentOrigin, attributes: [.font:UIFont.systemFont(ofSize: fontSize)], context:nil).size.height

    }

相关文章

网友评论

      本文标题:Swift计算文本的高度、宽度

      本文链接:https://www.haomeiwen.com/subject/huiwcltx.html