Skip to content

Commit 67610e2

Browse files
Mark Pospeselmpospese
authored andcommitted
[Issue-69] Make buildAttributes public
1 parent 1db75f3 commit 67610e2

File tree

2 files changed

+67
-7
lines changed

2 files changed

+67
-7
lines changed

Sources/YMatterType/Typography/TypographyLayout.swift

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public extension TypographyLayout {
7979
/// Style plain text using this layout
8080
/// - Parameters:
8181
/// - text: the text to style
82-
/// - isSingleLine: `true` for single line text, `false` for potentially multi-line text.
82+
/// - lineMode: line mode of the text.
8383
/// Paragraph styles will not be applied to single line text.
8484
/// - additionalAttributes: any additional attributes to apply
8585
/// (e.g. `UITextView` requires `.foregroundColor`), default = `[:]`
@@ -98,8 +98,8 @@ public extension TypographyLayout {
9898

9999
/// Style attributed text using this layout
100100
/// - Parameters:
101-
/// - text: the text to style
102-
/// - isSingleLine: `true` for single line text, `false` for potentially multi-line text.
101+
/// - attributedText: the attrubuted text to style
102+
/// - lineMode: line mode of the text.
103103
/// Paragraph styles will not be applied to single line text.
104104
/// - additionalAttributes: any additional attributes to apply
105105
/// (e.g. `UITextView` requires `.foregroundColor`), default = `[:]`
@@ -112,12 +112,20 @@ public extension TypographyLayout {
112112
let attributes = buildAttributes(startingWith: additionalAttributes, lineMode: lineMode)
113113
return attributedText.textCase(textCase).attributedString(with: attributes)
114114
}
115-
}
116115

117-
private extension TypographyLayout {
116+
/// Generates the text attributes needed to apply this typographical layout.
117+
///
118+
/// These attributes may change (because the font may change) any time there is a change in
119+
/// content size category (Dynamic Type) or legibility weight (Accessibility Bold Text).
120+
/// - Parameters:
121+
/// - additionalAttributes: any additional attributes to combine with the typographical attributes.
122+
/// Default = `[:]`
123+
/// - lineMode: line mode of the text. Default = `.single`.
124+
/// Paragraph styles will not be applied to single line text.
125+
/// - Returns: the dictionary of attributes needed to style the text.
118126
func buildAttributes(
119-
startingWith additionalAttributes: [NSAttributedString.Key: Any],
120-
lineMode: Typography.LineMode
127+
startingWith additionalAttributes: [NSAttributedString.Key: Any] = [:],
128+
lineMode: Typography.LineMode = .single
121129
) -> [NSAttributedString.Key: Any] {
122130
var attributes = additionalAttributes
123131
if case let .multi(textAlignment, lineBreakMode) = lineMode {

Tests/YMatterTypeTests/Typography/TypographyLayoutTests.swift

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,58 @@ final class TypographyLayoutTests: XCTestCase {
110110
// we expect it to have paragraph style
111111
XCTAssertNotNil(styled.attribute(.paragraphStyle, at: 0, effectiveRange: nil))
112112
}
113+
114+
func test_buildAttributes_defaultDeliversFontAttributeOnly() {
115+
let sut = makeSUT()
116+
let attributes = sut.buildAttributes()
117+
118+
XCTAssertEqual(sut.font, attributes[.font] as? UIFont)
119+
XCTAssertNil(attributes[.paragraphStyle])
120+
XCTAssertNil(attributes[.kern])
121+
XCTAssertNil(attributes[.underlineStyle])
122+
XCTAssertNil(attributes[.strikethroughStyle])
123+
XCTAssertNil(attributes[.baselineOffset])
124+
}
125+
126+
func test_buildAttributes_singleLineDeliversNoParagraphStyles() {
127+
let sut = makeSUT()
128+
let attributes = sut.buildAttributes(lineMode: .single)
129+
130+
XCTAssertNil(attributes[.paragraphStyle])
131+
}
132+
133+
func test_buildAttributes_multiLineDeliversParagraphStyles() throws {
134+
let sut = makeSUT()
135+
let attributes = sut.buildAttributes(lineMode: .multi(alignment: .natural, lineBreakMode: .byWordWrapping))
136+
137+
let paragraphStyle = try XCTUnwrap(attributes[.paragraphStyle] as? NSParagraphStyle)
138+
XCTAssertEqual(paragraphStyle.minimumLineHeight, sut.lineHeight)
139+
XCTAssertEqual(paragraphStyle.maximumLineHeight, sut.lineHeight)
140+
XCTAssertEqual(paragraphStyle.lineBreakMode, .byWordWrapping)
141+
XCTAssertEqual(sut.baselineOffset, attributes[.baselineOffset] as? CGFloat)
142+
}
143+
144+
func test_buildAttributes_deliversKernAttribute() {
145+
let letterSpacing = CGFloat(Int.random(in: 1...24)) / 10.0
146+
let sut = makeSUT(letterSpacing: letterSpacing)
147+
let attributes = sut.buildAttributes()
148+
149+
XCTAssertEqual(letterSpacing, attributes[.kern] as? CGFloat)
150+
}
151+
152+
func test_buildAttributes_deliversUnderlineAttributes() {
153+
let sut = makeSUT(textDecoration: .underline)
154+
let attributes = sut.buildAttributes()
155+
156+
XCTAssertEqual(NSUnderlineStyle.single.rawValue, attributes[.underlineStyle] as? Int)
157+
}
158+
159+
func test_buildAttributes_deliversStrikethroughAttributes() {
160+
let sut = makeSUT(textDecoration: .strikethrough)
161+
let attributes = sut.buildAttributes()
162+
163+
XCTAssertEqual(NSUnderlineStyle.single.rawValue, attributes[.strikethroughStyle] as? Int)
164+
}
113165
}
114166

115167
private extension TypographyLayoutTests {

0 commit comments

Comments
 (0)