iOS 11之前限制宽高计算字符串的size用的是UILabel
的textRect(forBounds bounds: CGRect, limitedToNumberOfLines numberOfLines: Int) -> CGRect
方法,当时也没考虑线程安全问题(low爆了),Xcode也没提示,用了好几个版本,所幸一直都没问题。
贴下方法(当时为什么选这个方法就不解释了):
func textSize(font: UIFont, constrainedSize: CGSize, lineSpacing: CGFloat?, lines: Int) -> CGSize {
if self.isEmpty || lines < 0 {
return CGSize.zero
}
let attributedString = NSMutableAttributedString(string: self)
let range = NSRange(location: 0, length: attributedString.length)
attributedString.addAttributes([NSFontAttributeName: font], range: range)
if lineSpacing != nil {
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineBreakMode = .byTruncatingTail
paragraphStyle.lineSpacing = lineSpacing!
attributedString.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range: range)
}
let calculatedLabel = UILabel()
calculatedLabel.font = font
calculatedLabel.attributedText = attributedString
calculatedLabel.numberOfLines = lines
let rect = calculatedLabel.textRect(forBounds: CGRect(x: 0, y: 0, width: constrainedSize.width, height: constrainedSize.height), limitedToNumberOfLines: lines)
return rect.size
}
最近升级了Xcode 9,运行时警告我let calculatedLabel = UILabel()
要在主线程执行,这时才意识到问题的严重性,马上进行了修改:
extension String {
func boundingRect(with constrainedSize: CGSize, font: UIFont, lineSpacing: CGFloat? = nil) -> CGSize {
let attritube = NSMutableAttributedString(string: self)
let range = NSRange(location: 0, length: attritube.length)
attritube.addAttributes([NSAttributedStringKey.font: font], range: range)
if lineSpacing != nil {
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = lineSpacing!
attritube.addAttribute(NSAttributedStringKey.paragraphStyle, value: paragraphStyle, range: range)
}
let rect = attritube.boundingRect(with: constrainedSize, options: [.usesLineFragmentOrigin, .usesFontLeading], context: nil)
var size = rect.size
if let currentLineSpacing = lineSpacing {
// 文本的高度减去字体高度小于等于行间距,判断为当前只有1行
let spacing = size.height - font.lineHeight
if spacing <= currentLineSpacing && spacing > 0 {
size = CGSize(width: size.width, height: font.lineHeight)
}
}
return size
}
func boundingRect(with constrainedSize: CGSize, font: UIFont, lineSpacing: CGFloat? = nil, lines: Int) -> CGSize {
if lines < 0 {
return .zero
}
let size = boundingRect(with: constrainedSize, font: font, lineSpacing: lineSpacing)
if lines == 0 {
return size
}
let currentLineSpacing = (lineSpacing == nil) ? (font.lineHeight - font.pointSize) : lineSpacing!
let maximumHeight = font.lineHeight*CGFloat(lines) + currentLineSpacing*CGFloat(lines - 1)
if size.height >= maximumHeight {
return CGSize(width: size.width, height: maximumHeight)
}
return size
}
}
参数解释
constrainedSize:限制的size
font:字号
lineSpacing:默认为nil,使用系统默认的行间距
lines:限制的行数
注:代码版本为Swift 4.0
上面的两个方法分别取代名:方法1和方法2。
方法1:限制宽高,可设置行间距,计算准确
方法2:比方法1多了限制行数功能。
配合使用UILabel
的扩展方法:
extension UILabel {
// 设置`numberOfLines = 0`的原因:
// 配合方法`func boundingRect(with constrainedSize: CGSize, font: UIFont, lineSpacing: CGFloat? = nil, lines: Int) -> CGSize`使用,可以很好的解决不能正常显示限制行数的问题;
// 如果为label设置了限制行数(大于0的前提),使用上面的计算方法(带行间距),同时字符串的实际行数大于限制行数,这时候的高度会使label不能正常显示。
func setText(with normalString: String, lineSpacing: CGFloat?, frame: CGRect) {
self.frame = frame
self.numberOfLines = 0
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineBreakMode = .byTruncatingTail
if lineSpacing != nil {
if (frame.height - font.lineHeight) <= lineSpacing! {
paragraphStyle.lineSpacing = 0
} else {
paragraphStyle.lineSpacing = lineSpacing!
}
}
let attributedString = NSMutableAttributedString(string: normalString)
let range = NSRange(location: 0, length: attributedString.length)
attributedString.addAttributes([NSAttributedStringKey.font: font], range: range)
attributedString.addAttribute(NSAttributedStringKey.paragraphStyle, value: paragraphStyle, range: range)
self.attributedText = attributedString
}
}
网友评论