Swift计算文本宽高

时间:2022-12-16 21:05:35

iOS 8 开始可以配合 AutoLayout 自动估算文本的高度,但是当 Cell 比较复杂的时候,还会需要手动去计算。首先声明一个样式

var TextStyle : [String : NSObject] {
get { let paraStyle = NSMutableParagraphStyle() paraStyle.minimumLineHeight = 17.3 paraStyle.lineSpacing = paraStyle.lineBreakMode = NSLineBreakMode.ByWordWrapping paraStyle.paragraphSpacing = paraStyle.paragraphSpacingBefore = return [NSForegroundColorAttributeName: UIColor.blackColor(),
NSKernAttributeName: CGFloat(0.5),
NSFontAttributeName: UIFont.systemFontOfSize(15.0),
NSParagraphStyleAttributeName: paraStyle
]
}
}

如果你的文本里含有很多特殊字符,例如颜文字,通过 NSKernAttributeName 设置字间距可以算出更宽松的结果。

以 UILabel 为例,设置其 attributedText

label.attributedText = NSAttributedString(string: "Hello World", attributes: TextStyle)) 

再利用 boundingRectWithSize 这个方法来计算其约束后的高度,传入一个 width 为宽度约束,然后利用我们声明的样式来计算高度。

func sizeHeightWithText(attrString: NSString, width: CGFloat, textAttributes: [NSObject : AnyObject]) -> CGSize {

    var rect = attrString.boundingRectWithSize(CGSizeMake(width, CGFloat.max), options: .UsesLineFragmentOrigin | .UsesFontLeading, attributes: textAttributes, context: nil)

    return CGSize(width: rect.width, height: rect.height)
}

如果你使用的是 UITextView,那么还需要对 UITextView 做一些处理来去掉他的边距。

textView.textContainer.lineFragmentPadding =
textView.textContainerInset = UIEdgeInsetsZero