Skip to content

Commit e141bfd

Browse files
committed
add getHeight, getWidth function
1 parent 7f5a437 commit e141bfd

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

Demo/ViewController.swift

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
import UIKit
1010
import AttributedStringWrapper
1111

12+
let screenW = UIScreen.main.bounds.width
13+
14+
1215
class ViewController: UIViewController {
1316

1417
@IBOutlet weak var label1: UILabel!
@@ -25,19 +28,25 @@ class ViewController: UIViewController {
2528

2629

2730
// 1. shadow: you can set range, default allRange
28-
label1.attributedText = content.toAttributed.shadow {
31+
let attrText1 = content.toAttributed.shadow {
2932
$0.shadowColor = UIColor.red
3033
$0.shadowOffset = CGSize(width: 3, height: 3)
3134
$0.shadowBlurRadius = 2.0
32-
}.rawValue
35+
}.font(UIFont.systemFont(ofSize: 15))
36+
37+
label1.attributedText = attrText1.rawValue
38+
// Add the shadow offset
39+
print(attrText1.getHeight(by: screenW - 20) + 3)
3340

3441

3542

3643
// 2. paragraphStyle: you can set range, default allRange
37-
label2.attributedText = content.toAttributed.paragraph {
44+
let attrText2 = content.toAttributed.paragraph {
3845
$0.alignment = .center
3946
$0.lineSpacing = 8.0
40-
}.rawValue
47+
}.font(UIFont.systemFont(ofSize: 15))
48+
label2.attributedText = attrText2.rawValue
49+
print(attrText2.getHeight(by: screenW - 20))
4150

4251

4352

@@ -54,8 +63,6 @@ class ViewController: UIViewController {
5463
// 5. strokeStyle:
5564
label5.attributedText = content.toAttributed.stroke(color: UIColor.blue, width: 2.5).rawValue
5665

57-
58-
5966
// 6. all:
6067
label6.attributedText = content.toAttributed
6168
.underLine(style: [.styleSingle, .patternDot], color: .red, range: NSMakeRange(0, 5))
@@ -74,5 +81,16 @@ class ViewController: UIViewController {
7481
$0.shadowBlurRadius = 2.0
7582
}.rawValue
7683
}
84+
85+
override func viewDidLayoutSubviews() {
86+
super.viewDidLayoutSubviews()
87+
print("label1 height: \(label1.frame.height)")
88+
print("label2 height: \(label2.frame.height)")
89+
}
90+
7791
}
7892

93+
94+
95+
96+

Sources/AttributedStringWrapper.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,21 @@ public extension AttributedStringWrapper {
163163
rawValue.addAttributes([NSVerticalGlyphFormAttributeName: value], range: range ?? allRange)
164164
return self
165165
}
166+
167+
/// get height
168+
func getHeight(by fixedWidth: CGFloat) -> CGFloat {
169+
let h = rawValue.boundingRect(with: CGSize(width: fixedWidth, height: CGFloat(MAXFLOAT)), options: [.usesFontLeading , .usesLineFragmentOrigin, .usesDeviceMetrics], context: nil).size.height
170+
return ceil(h)
171+
}
172+
/// get width
173+
func getWidth(by fixedHeight: CGFloat) -> CGFloat {
174+
let w = rawValue.boundingRect(with: CGSize(width: CGFloat(MAXFLOAT), height: fixedHeight), options: [.usesFontLeading , .usesLineFragmentOrigin], context: nil).size.width
175+
return ceil(w)
176+
}
166177
}
178+
179+
180+
181+
182+
183+

0 commit comments

Comments
 (0)