Swift中关于计算文本的宽度

        /// 计算文本大小
        if titleLabel!.text != nil {
            var attributes = [NSFontAttributeName: titleLabel!.font]
            var option = NSStringDrawingOptions.UsesLineFragmentOrigin
            var text: NSString = NSString(CString: titleLabel!.text!.cStringUsingEncoding(NSUTF8StringEncoding)!,
                encoding: NSUTF8StringEncoding)
            rect = text.boundingRectWithSize(CGSizeMake(200, 300), options: option, attributes: attributes, context: nil)
            rect.origin.x = 12
            rect.origin.y = 66
            
            width = rect.size.width + 24
            height = rect.size.height + 80
            
            if width < 100 {
                width = 100
                rect.origin.x = 0
                rect.size.width = 100
            }
        }

你可能感兴趣的:(Swift中关于计算文本的宽度)