swift - 动态计算文本高度

时间:2023-03-10 00:16:40
swift - 动态计算文本高度
    func heightOfCell(text : String) -> CGFloat {
        let attributes = [NSFontAttributeName:UIFont.systemFontOfSize(14)]
        let option = NSStringDrawingOptions.UsesLineFragmentOrigin
        let rect:CGRect = text.boundingRectWithSize(CGSize(width: self.frame.size.width - 20, height: 0), options: option, attributes: attributes, context: nil)
        print("height is \(rect.size.height)")
        return rect.size.height + 50
    }