Skip to content

Commit 9343f1b

Browse files
PanchamiShenoyPanchamiShenoy
authored andcommitted
[CM-914] Added family name mutator method
1 parent 82dc370 commit 9343f1b

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

Sources/YMatterType/Typography/Typography+Mutators.swift

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,33 @@
99
import UIKit
1010

1111
public extension Typography {
12+
/// Returns a copy of the Typography but with the new `familyName` applied.
13+
/// - Parameter value: the family name to use
14+
/// - Returns: an updated copy of the Typography
15+
func familyName(_ value: String) -> Typography {
16+
if fontFamily.familyName == value { return self }
17+
18+
return Typography(
19+
familyName: value,
20+
fontStyle: fontFamily.fontNameSuffix.isEmpty ? .regular : .italic,
21+
fontWeight: fontWeight,
22+
fontSize: fontSize,
23+
lineHeight: lineHeight,
24+
letterSpacing: letterSpacing,
25+
paragraphIndent: paragraphIndent,
26+
paragraphSpacing: paragraphSpacing,
27+
textCase: textCase,
28+
textDecoration: textDecoration,
29+
textStyle: textStyle,
30+
isFixed: isFixed
31+
)
32+
}
33+
1234
/// Returns a copy of the Typography but with `.regular` font weight
1335
var regular: Typography {
1436
fontWeight(.regular)
1537
}
16-
38+
1739
/// Returns a copy of the Typography but with `.bold` font weight
1840
var bold: Typography {
1941
fontWeight(.bold)

Tests/YMatterTypeTests/Typography/Typography+MutatorsTests.swift

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ final class TypographyMutatorsTests: XCTestCase {
2727
return styles
2828
}()
2929

30+
func testFamilyName() {
31+
types.forEach {
32+
let familyName = "AppleSDGothicNeo"
33+
let typography = $0.familyName(familyName)
34+
_test(original: $0, modified: typography, familyName: familyName)
35+
let layout = typography.generateLayout(compatibleWith: UITraitCollection(legibilityWeight: .regular))
36+
XCTAssertEqual(layout.font.familyName.removeSpaces(), familyName)
37+
}
38+
}
39+
3040
func testRegular() {
3141
types.forEach {
3242
_test(original: $0, modified: $0.regular, weight: .regular)
@@ -94,6 +104,7 @@ final class TypographyMutatorsTests: XCTestCase {
94104
private func _test(
95105
original: Typography,
96106
modified: Typography,
107+
familyName: String? = nil,
97108
weight: Typography.FontWeight? = nil,
98109
fontSize: CGFloat? = nil,
99110
lineHeight: CGFloat? = nil,
@@ -102,6 +113,7 @@ final class TypographyMutatorsTests: XCTestCase {
102113
textCase: Typography.TextCase? = nil,
103114
textDecoration: Typography.TextDecoration? = nil
104115
) {
116+
let familyName = familyName ?? original.fontFamily.familyName
105117
let weight = weight ?? original.fontWeight
106118
let fontSize = fontSize ?? original.fontSize
107119
let lineHeight = lineHeight ?? original.lineHeight
@@ -110,7 +122,9 @@ final class TypographyMutatorsTests: XCTestCase {
110122
let textCase = textCase ?? original.textCase
111123
let textDecoration = textDecoration ?? original.textDecoration
112124

113-
// fontWeight, fontSize, lineHeight, isFixed, letterSpacing, textCase, and textDecoration should be as expected
125+
// familyName, fontWeight, fontSize, lineHeight, isFixed,
126+
// letterSpacing, textCase, and textDecoration should be as expected
127+
XCTAssertEqual(modified.fontFamily.familyName, familyName)
114128
XCTAssertEqual(modified.fontWeight, weight)
115129
XCTAssertEqual(modified.fontSize, fontSize)
116130
XCTAssertEqual(modified.lineHeight, lineHeight)
@@ -120,7 +134,6 @@ final class TypographyMutatorsTests: XCTestCase {
120134
XCTAssertEqual(modified.textDecoration, textDecoration)
121135

122136
// the other variables should be the same
123-
XCTAssertEqual(modified.fontFamily.familyName, original.fontFamily.familyName)
124137
XCTAssertEqual(modified.textStyle, original.textStyle)
125138
XCTAssertEqual(modified.paragraphIndent, original.paragraphIndent)
126139
XCTAssertEqual(modified.paragraphSpacing, original.paragraphSpacing)

0 commit comments

Comments
 (0)