swift 计算文字的宽高



   //            计算文字高度
    func getTextRectSize(text:NSString,font:UIFont,size:CGSize) -> CGRect {
        let attributes = [NSFontAttributeName: font]
        let option = NSStringDrawingOptions.UsesLineFragmentOrigin
        let rect:CGRect = text.boundingRectWithSize(size, options: option, attributes: attributes, context: nil)
        //        println("rect:\(rect)");
        return rect;
    }

例:

let nameTextHeight = getTextRectSize("@爱吃柠檬:", font: UIFont.systemFontOfSize(8), size: CGSizeMake(self.chatTVLeft.bounds.width, CGFloat.max)).size.height


self.chatTVLeft.contentInset = UIEdgeInsetsMake(-(fullTextHeight - 85 + nameTextHeight + inputTextHeight), 0, 0, 0)

//                文字过多不绘制

                self.chatTVLeft.layoutManager.allowsNonContiguousLayout = false

你可能感兴趣的:(swift 计算文字的宽高)