@buoge
2017-06-28T10:55:53.000000Z
字数 636
阅读 846
iOS
uilable 一定要是 .byWordWrapping 计算才正确
public static func stringSize(calculateString: String, fontSize:CGFloat,width:CGFloat,lineSpacing:CGFloat=0)->CGSize {
let font = FontUtils.getThinFont(fontSize)
let size = CGSize(width: width,height: CGFloat.greatestFiniteMagnitude)
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = lineSpacing
paragraphStyle.lineBreakMode = .byWordWrapping;
let attributes = [NSFontAttributeName:font,
NSParagraphStyleAttributeName:paragraphStyle.copy()]
let text = calculateString as NSString
let rect = text.boundingRect(with: size, options:.usesLineFragmentOrigin, attributes: attributes, context:nil)
return rect.size
}