前言
对于swift 还处于摸索阶段很多语法还不熟悉,本文主要给大家介绍的是关于利用Swift计算文本size的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。
iOS 11之前限制宽高计算字符串的size用的是UILabel的textRect(forBounds bounds: CGRect, limitedToNumberOfLines numberOfLines: Int) -> CGRect
方法,当时也没考虑线程安全问题(low爆了),Xcode也没提示,用了好几个版本,所幸一直都没问题。
贴下方法(当时为什么选这个方法就不解释了):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
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()
要在主线程执行,这时才意识到问题的严重性,马上进行了修改:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
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的扩展方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
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
}
}
|
在此感谢仓鼠:iOS 行距全攻略 和https://github.com/zhengwenming/WeChat
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对服务器之家的支持。
原文链接:http://www.jianshu.com/p/0dd6141c46db