Skip to content
This repository was archived by the owner on Feb 17, 2021. It is now read-only.

Commit 1e91330

Browse files
committed
HUED-8605 Test & Fix Potential truncation issue in LabelLayout
- Added LabelLayout unit test to verify that size calculation logic considers line break mode while calculating LabelLayout size. - Added line break mode support in LabelLayout to calculate correct size based on line break mode.
1 parent dc2ff66 commit 1e91330

File tree

7 files changed

+207
-19
lines changed

7 files changed

+207
-19
lines changed

LayoutKitTests/LabelLayoutTests.swift

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ import LayoutKit
1111

1212
class LabelLayoutTests: XCTestCase {
1313

14+
// For the defined `sampleText` and `labelLayoutMaxWidth` combination, `LabelLayout` requires 2 line of
15+
// text for char-wrapping and 3 lines for word-wrapping/truncating-tail.
16+
// So don't change this combination as the tests are based on these values.
17+
private static let sampleText = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean comm"
18+
private static let labelLayoutMaxWidth = 305
19+
1420
func testNeedsView() {
1521
let l = LabelLayout(text: "hi").arrangement().makeViews()
1622
XCTAssertNotNil(l as? UILabel)
@@ -200,10 +206,75 @@ class LabelLayoutTests: XCTestCase {
200206
let maxSize = CGSize(width: 17, height: .max)
201207
XCTAssertEqual(layout.measurement(within: maxSize).size, label.sizeThatFits(maxSize))
202208
}
209+
210+
func testSizeCalculationWithWordWrapping() {
211+
// Use same line break mode for `LabelLayout` and dummy label and then match LabelLayout's size with dummy label's size calculation.
212+
verifyTextSizeCalculation(with: LabelLayoutTests.sampleText, lineBreakMode: .byWordWrapping)
213+
}
214+
215+
func testSizeCalculationWithCharWrapping() {
216+
// Use same line break mode for `LabelLayout` and dummy label and then match LabelLayout's size with dummy label's size calculation.
217+
verifyTextSizeCalculation(with: LabelLayoutTests.sampleText, lineBreakMode: .byCharWrapping)
218+
}
219+
220+
func testSizeCalculationWithDifferentLineBreakMode() {
221+
// Use different line break mode for `LabelLayout` and dummy label.
222+
// So in this case, size calculation should not match with dummy label's size calculation.
223+
let label = UILabel(text: LabelLayoutTests.sampleText, numberOfLines: 0, lineBreakMode: .byCharWrapping)
224+
let layout = LabelLayout(text: LabelLayoutTests.sampleText, config: { label in
225+
label.text = LabelLayoutTests.sampleText
226+
})
227+
let maxSize = CGSize(width: LabelLayoutTests.labelLayoutMaxWidth, height: .max)
228+
XCTAssertNotEqual(layout.measurement(within: maxSize).size, label.sizeThatFits(maxSize))
229+
}
230+
231+
func testAttributedTextSizeCalculationWithWordWrapping() {
232+
// Use same line break mode for `LabelLayout` and dummy label and then match LabelLayout's size with dummy label's size calculation.
233+
let attributedText = NSAttributedString(string: LabelLayoutTests.sampleText)
234+
verifyAttributedTextSizeCalculation(with: attributedText, lineBreakMode: .byWordWrapping)
235+
}
236+
237+
func testAttributedTextSizeCalculationWithCharWrapping() {
238+
// Use same line break mode for `LabelLayout` and dummy label and then match LabelLayout's size with dummy label's size calculation.
239+
let attributedText = NSAttributedString(string: LabelLayoutTests.sampleText)
240+
verifyAttributedTextSizeCalculation(with: attributedText, lineBreakMode: .byCharWrapping)
241+
}
242+
243+
func testAttributedTextSizeCalculationWithDifferentLineBreakMode() {
244+
// Use different line break mode for `LabelLayout` and dummy label.
245+
// So in this case, size calculation should not match with dummy label's size calculation.
246+
let attributedText = NSAttributedString(string: LabelLayoutTests.sampleText)
247+
let label = UILabel(attributedText: attributedText, numberOfLines: 0, lineBreakMode: .byCharWrapping)
248+
let layout = LabelLayout(attributedText: attributedText, config: { label in
249+
label.attributedText = attributedText
250+
})
251+
let maxSize = CGSize(width: LabelLayoutTests.labelLayoutMaxWidth, height: .max)
252+
XCTAssertNotEqual(layout.measurement(within: maxSize).size, label.sizeThatFits(maxSize))
253+
}
254+
255+
// MARK: Private Helpers
256+
257+
private func verifyTextSizeCalculation(with text: String, lineBreakMode: NSLineBreakMode) {
258+
let label = UILabel(text: text, numberOfLines: 0, lineBreakMode: lineBreakMode)
259+
let layout = LabelLayout(text: text, lineBreakMode: lineBreakMode, config: { label in
260+
label.text = text
261+
})
262+
let maxSize = CGSize(width: LabelLayoutTests.labelLayoutMaxWidth, height: .max)
263+
XCTAssertEqual(layout.measurement(within: maxSize).size, label.sizeThatFits(maxSize))
264+
}
265+
266+
private func verifyAttributedTextSizeCalculation(with attributedText: NSAttributedString, lineBreakMode: NSLineBreakMode) {
267+
let label = UILabel(attributedText: attributedText, numberOfLines: 0, lineBreakMode: lineBreakMode)
268+
let layout = LabelLayout(attributedText: attributedText, lineBreakMode: lineBreakMode, config: { label in
269+
label.attributedText = attributedText
270+
})
271+
let maxSize = CGSize(width: LabelLayoutTests.labelLayoutMaxWidth, height: .max)
272+
XCTAssertEqual(layout.measurement(within: maxSize).size, label.sizeThatFits(maxSize))
273+
}
203274
}
204275

205276
extension UILabel {
206-
convenience init(text: String, font: UIFont? = nil, numberOfLines: Int? = nil) {
277+
convenience init(text: String, font: UIFont? = nil, numberOfLines: Int? = nil, lineBreakMode: NSLineBreakMode? = nil) {
207278
self.init()
208279
self.text = text
209280
if let font = font {
@@ -212,16 +283,23 @@ extension UILabel {
212283
if let numberOfLines = numberOfLines {
213284
self.numberOfLines = numberOfLines
214285
}
286+
if let lineBreakMode = lineBreakMode {
287+
self.lineBreakMode = lineBreakMode
288+
}
215289
}
216290

217-
convenience init(attributedText: NSAttributedString, font: UIFont? = nil, numberOfLines: Int? = nil) {
291+
convenience init(attributedText: NSAttributedString, font: UIFont? = nil, numberOfLines: Int? = nil, lineBreakMode: NSLineBreakMode? = nil) {
218292
self.init()
219293
if let font = font {
220294
self.font = font
221295
}
222296
if let numberOfLines = numberOfLines {
223297
self.numberOfLines = numberOfLines
224298
}
299+
if let lineBreakMode = lineBreakMode {
300+
self.lineBreakMode = lineBreakMode
301+
}
302+
225303
// Want to set attributed text AFTER font it set, otherwise the font seems to take precedence.
226304
self.attributedText = attributedText
227305
}

Sources/Internal/NSAttributedStringExtension.swift

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,18 @@ extension NSAttributedString {
1212

1313
/// Returns a new NSAttributedString with a given font and the same attributes.
1414
func with(font: UIFont) -> NSAttributedString {
15-
let fontAttribute = [NSAttributedStringKey.font: font]
16-
let attributedTextWithFont = NSMutableAttributedString(string: string, attributes: fontAttribute)
15+
return with(additionalAttributes: [NSAttributedStringKey.font: font])
16+
}
17+
18+
/// Returns a new NSAttributedString with previous as well as additional attributes.
19+
func with(additionalAttributes: [NSAttributedStringKey : Any]?) -> NSAttributedString {
20+
let attributedTextWithAdditionalAttributes = NSMutableAttributedString(string: string, attributes: additionalAttributes)
1721
let fullRange = NSMakeRange(0, (string as NSString).length)
18-
attributedTextWithFont.beginEditing()
22+
attributedTextWithAdditionalAttributes.beginEditing()
1923
self.enumerateAttributes(in: fullRange, options: .longestEffectiveRangeNotRequired, using: { (attributes, range, _) in
20-
attributedTextWithFont.addAttributes(attributes, range: range)
24+
attributedTextWithAdditionalAttributes.addAttributes(attributes, range: range)
2125
})
22-
attributedTextWithFont.endEditing()
23-
24-
return attributedTextWithFont
26+
attributedTextWithAdditionalAttributes.endEditing()
27+
return attributedTextWithAdditionalAttributes
2528
}
26-
2729
}

Sources/Layouts/LabelLayout.swift

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ open class LabelLayout<Label: UILabel>: BaseLayout<Label>, ConfigurableLayout {
1717
open let font: UIFont
1818
open let numberOfLines: Int
1919
open let lineHeight: CGFloat
20+
open let lineBreakMode: NSLineBreakMode
2021

2122
public init(text: Text,
2223
font: UIFont = LabelLayoutDefaults.defaultFont,
2324
lineHeight: CGFloat? = nil,
2425
numberOfLines: Int = LabelLayoutDefaults.defaultNumberOfLines,
26+
lineBreakMode: NSLineBreakMode = LabelLayoutDefaults.defaultLineBreakMode,
2527
alignment: Alignment = LabelLayoutDefaults.defaultAlignment,
2628
flexibility: Flexibility = LabelLayoutDefaults.defaultFlexibility,
2729
viewReuseId: String? = nil,
@@ -31,13 +33,15 @@ open class LabelLayout<Label: UILabel>: BaseLayout<Label>, ConfigurableLayout {
3133
self.numberOfLines = numberOfLines
3234
self.font = font
3335
self.lineHeight = lineHeight ?? font.lineHeight
36+
self.lineBreakMode = lineBreakMode
3437
super.init(alignment: alignment, flexibility: flexibility, viewReuseId: viewReuseId, config: config)
3538
}
3639

3740
init(attributedString: NSAttributedString,
3841
font: UIFont = LabelLayoutDefaults.defaultFont,
3942
lineHeight: CGFloat? = nil,
4043
numberOfLines: Int = LabelLayoutDefaults.defaultNumberOfLines,
44+
lineBreakMode: NSLineBreakMode = LabelLayoutDefaults.defaultLineBreakMode,
4145
alignment: Alignment = LabelLayoutDefaults.defaultAlignment,
4246
flexibility: Flexibility = LabelLayoutDefaults.defaultFlexibility,
4347
viewReuseId: String? = nil,
@@ -48,13 +52,15 @@ open class LabelLayout<Label: UILabel>: BaseLayout<Label>, ConfigurableLayout {
4852
self.numberOfLines = numberOfLines
4953
self.font = font
5054
self.lineHeight = lineHeight ?? font.lineHeight
55+
self.lineBreakMode = lineBreakMode
5156
super.init(alignment: alignment, flexibility: flexibility, viewReuseId: viewReuseId, viewClass: viewClass ?? Label.self, config: config)
5257
}
5358

5459
init(string: String,
5560
font: UIFont = LabelLayoutDefaults.defaultFont,
5661
lineHeight: CGFloat? = nil,
5762
numberOfLines: Int = LabelLayoutDefaults.defaultNumberOfLines,
63+
lineBreakMode: NSLineBreakMode = LabelLayoutDefaults.defaultLineBreakMode,
5864
alignment: Alignment = LabelLayoutDefaults.defaultAlignment,
5965
flexibility: Flexibility = LabelLayoutDefaults.defaultFlexibility,
6066
viewReuseId: String? = nil,
@@ -65,6 +71,7 @@ open class LabelLayout<Label: UILabel>: BaseLayout<Label>, ConfigurableLayout {
6571
self.numberOfLines = numberOfLines
6672
self.font = font
6773
self.lineHeight = lineHeight ?? font.lineHeight
74+
self.lineBreakMode = lineBreakMode
6875
super.init(alignment: alignment, flexibility: flexibility, viewReuseId: viewReuseId, viewClass: viewClass ?? Label.self, config: config)
6976
}
7077

@@ -74,6 +81,7 @@ open class LabelLayout<Label: UILabel>: BaseLayout<Label>, ConfigurableLayout {
7481
font: UIFont = LabelLayoutDefaults.defaultFont,
7582
lineHeight: CGFloat? = nil,
7683
numberOfLines: Int = LabelLayoutDefaults.defaultNumberOfLines,
84+
lineBreakMode: NSLineBreakMode = LabelLayoutDefaults.defaultLineBreakMode,
7785
alignment: Alignment = LabelLayoutDefaults.defaultAlignment,
7886
flexibility: Flexibility = LabelLayoutDefaults.defaultFlexibility,
7987
viewReuseId: String? = nil,
@@ -83,6 +91,7 @@ open class LabelLayout<Label: UILabel>: BaseLayout<Label>, ConfigurableLayout {
8391
font: font,
8492
lineHeight: lineHeight,
8593
numberOfLines: numberOfLines,
94+
lineBreakMode: lineBreakMode,
8695
alignment: alignment,
8796
flexibility: flexibility,
8897
viewReuseId: viewReuseId,
@@ -93,6 +102,7 @@ open class LabelLayout<Label: UILabel>: BaseLayout<Label>, ConfigurableLayout {
93102
font: UIFont = LabelLayoutDefaults.defaultFont,
94103
lineHeight: CGFloat? = nil,
95104
numberOfLines: Int = LabelLayoutDefaults.defaultNumberOfLines,
105+
lineBreakMode: NSLineBreakMode = LabelLayoutDefaults.defaultLineBreakMode,
96106
alignment: Alignment = LabelLayoutDefaults.defaultAlignment,
97107
flexibility: Flexibility = LabelLayoutDefaults.defaultFlexibility,
98108
viewReuseId: String? = nil,
@@ -102,6 +112,7 @@ open class LabelLayout<Label: UILabel>: BaseLayout<Label>, ConfigurableLayout {
102112
font: font,
103113
lineHeight: lineHeight,
104114
numberOfLines: numberOfLines,
115+
lineBreakMode: lineBreakMode,
105116
alignment: alignment,
106117
flexibility: flexibility,
107118
viewReuseId: viewReuseId,
@@ -116,7 +127,7 @@ open class LabelLayout<Label: UILabel>: BaseLayout<Label>, ConfigurableLayout {
116127
}
117128

118129
private func textSize(within maxSize: CGSize) -> CGSize {
119-
var size = text.textSize(within: maxSize, font: font)
130+
var size = text.textSize(within: maxSize, font: font, lineBreakMode: lineBreakMode)
120131
if numberOfLines > 0 {
121132
let maxHeight = (CGFloat(numberOfLines) * lineHeight).roundedUpToFractionalPoint
122133
if size.height > maxHeight {
@@ -134,6 +145,7 @@ open class LabelLayout<Label: UILabel>: BaseLayout<Label>, ConfigurableLayout {
134145
open override func configure(view label: Label) {
135146
config?(label)
136147
label.numberOfLines = numberOfLines
148+
label.lineBreakMode = lineBreakMode
137149
label.font = font
138150
switch text {
139151
case .unattributed(let text):
@@ -155,6 +167,7 @@ public class LabelLayoutDefaults {
155167
public static let defaultNumberOfLines = 0
156168
public static let defaultFont = UILabel().font ?? UIFont.systemFont(ofSize: 17)
157169
public static let defaultAlignment = Alignment.topLeading
170+
public static let defaultLineBreakMode = NSLineBreakMode.byTruncatingTail
158171
public static let defaultFlexibility = Flexibility.flexible
159172
}
160173

Sources/ObjCSupport/Builders/LOKLabelLayoutBuilder.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
@property (nonatomic, nonnull, readonly) LOKLabelLayoutBuilder * _Nonnull(^font)(UIFont * _Nullable);
2424
@property (nonatomic, nonnull, readonly) LOKLabelLayoutBuilder * _Nonnull(^numberOfLines)(NSInteger);
25+
@property (nonatomic, nonnull, readonly) LOKLabelLayoutBuilder * _Nonnull(^lineBreakMode)(NSLineBreakMode);
2526
@property (nonatomic, nonnull, readonly) LOKLabelLayoutBuilder * _Nonnull(^lineHeight)(CGFloat);
2627

2728
@property (nonatomic, nonnull, readonly) LOKLabelLayoutBuilder * _Nonnull(^alignment)(LOKAlignment * _Nullable);

Sources/ObjCSupport/Builders/LOKLabelLayoutBuilder.m

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ @interface LOKLabelLayoutBuilder ()
2121
@property (nonatomic, nullable) NSAttributedString *privateAttributedString;
2222
@property (nonatomic, nullable) UIFont *privateFont;
2323
@property (nonatomic) NSInteger privateNumberOfLines;
24+
@property (nonatomic) NSLineBreakMode privateLineBreakMode;
2425
@property (nonatomic) CGFloat privateLineHeight;
2526
@property (nonatomic, nullable) void (^ privateConfigure)(UILabel * _Nonnull);
2627

@@ -87,6 +88,13 @@ - (nonnull LOKLabelLayout *)layout {
8788
};
8889
}
8990

91+
- (LOKLabelLayoutBuilder * _Nonnull (^)(NSLineBreakMode))lineBreakMode {
92+
return ^LOKLabelLayoutBuilder *(NSLineBreakMode lineBreakMode){
93+
self.privateLineBreakMode = lineBreakMode;
94+
return self;
95+
};
96+
}
97+
9098
- (LOKLabelLayoutBuilder * _Nonnull (^)(CGFloat))lineHeight {
9199
return ^LOKLabelLayoutBuilder *(CGFloat lineHeight){
92100
self.privateLineHeight = lineHeight;

Sources/ObjCSupport/LOKLabelLayout.swift

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@ import UIKit
1313
@objc public let string: String?
1414
@objc public let lineHeight: CGFloat
1515
@objc public let font: UIFont
16+
@objc public let lineBreakMode: NSLineBreakMode?
1617
@objc public let numberOfLines: Int
1718
@objc public let alignment: LOKAlignment
1819
@objc public let viewClass: UILabel.Type
1920
@objc public let configure: ((UILabel) -> Void)?
2021

22+
// TODO: Remove this init once all consumers are switch to `init(attributedString:font:lineBreakMode:lineHeight:numberOfLines:alignment:flexibility:viewReuseId:viewClass:configure:)`
2123
@objc public init(attributedString: NSAttributedString,
2224
font: UIFont?,
2325
lineHeight: CGFloat,
@@ -29,6 +31,7 @@ import UIKit
2931
configure: ((UILabel) -> Void)?) {
3032
self.attributedString = attributedString
3133
self.font = font ?? UIFont.systemFont(ofSize: UIFont.systemFontSize)
34+
self.lineBreakMode = nil
3235
self.lineHeight = lineHeight
3336
self.numberOfLines = numberOfLines
3437
self.alignment = alignment ?? .topLeading
@@ -49,6 +52,7 @@ import UIKit
4952
super.init(layout: layout)
5053
}
5154

55+
// TODO: Remove this init once all consumers are switch to `init(string:font:lineBreakMode:lineHeight:numberOfLines:alignment:flexibility:viewReuseId:viewClass:configure:)`
5256
@objc public init(string: String,
5357
font: UIFont?,
5458
lineHeight: CGFloat,
@@ -60,6 +64,7 @@ import UIKit
6064
configure: ((UILabel) -> Void)?) {
6165
self.string = string
6266
self.font = font ?? UIFont.systemFont(ofSize: UIFont.systemFontSize)
67+
self.lineBreakMode = nil
6368
self.lineHeight = lineHeight
6469
self.numberOfLines = numberOfLines
6570
self.alignment = alignment ?? .topLeading
@@ -78,4 +83,71 @@ import UIKit
7883
config: self.configure)
7984
super.init(layout: layout)
8085
}
86+
87+
@objc public init(attributedString: NSAttributedString,
88+
font: UIFont?,
89+
lineBreakMode: NSlineBreakMode?,
90+
lineHeight: CGFloat,
91+
numberOfLines: Int,
92+
alignment: LOKAlignment?,
93+
flexibility: LOKFlexibility?,
94+
viewReuseId: String?,
95+
viewClass: UILabel.Type?,
96+
configure: ((UILabel) -> Void)?) {
97+
self.attributedString = attributedString
98+
self.font = font ?? UIFont.systemFont(ofSize: UIFont.systemFontSize)
99+
self.lineBreakMode = lineBreakMode
100+
self.lineHeight = lineHeight
101+
self.numberOfLines = numberOfLines
102+
self.alignment = alignment ?? .topLeading
103+
self.viewClass = viewClass ?? UILabel.self
104+
self.configure = configure
105+
string = nil
106+
let layout = LabelLayout<UILabel>(
107+
attributedString: attributedString,
108+
font: self.font,
109+
lineHeight: lineHeight > 0 && lineHeight.isFinite ? lineHeight : Optional<CGFloat>.none,
110+
numberOfLines: self.numberOfLines,
111+
lineBreakMode: self.lineBreakMode ?? .byTruncatingTail,
112+
alignment: self.alignment.alignment,
113+
flexibility: flexibility?.flexibility ?? .flexible,
114+
viewReuseId: viewReuseId,
115+
viewClass: self.viewClass,
116+
config: self.configure)
117+
118+
super.init(layout: layout)
119+
}
120+
121+
@objc public init(string: String,
122+
font: UIFont?,
123+
lineBreakMode: NSlineBreakMode?,
124+
lineHeight: CGFloat,
125+
numberOfLines: Int,
126+
alignment: LOKAlignment?,
127+
flexibility: LOKFlexibility?,
128+
viewReuseId: String?,
129+
viewClass: UILabel.Type?,
130+
configure: ((UILabel) -> Void)?) {
131+
self.string = string
132+
self.font = font ?? UIFont.systemFont(ofSize: UIFont.systemFontSize)
133+
self.lineBreakMode = lineBreakMode
134+
self.lineHeight = lineHeight
135+
self.numberOfLines = numberOfLines
136+
self.alignment = alignment ?? .topLeading
137+
self.viewClass = viewClass ?? UILabel.self
138+
self.configure = configure
139+
attributedString = nil
140+
let layout = LabelLayout<UILabel>(
141+
string: string,
142+
font: self.font,
143+
lineHeight: lineHeight > 0 && lineHeight.isFinite ? lineHeight : Optional<CGFloat>.none,
144+
numberOfLines: self.numberOfLines,
145+
lineBreakMode: self.lineBreakMode ?? .byTruncatingTail,
146+
alignment: self.alignment.alignment,
147+
flexibility: flexibility?.flexibility ?? .flexible,
148+
viewReuseId: viewReuseId,
149+
viewClass: self.viewClass,
150+
config: self.configure)
151+
super.init(layout: layout)
152+
}
81153
}

0 commit comments

Comments
 (0)