Skip to content

Commit 9bc66c2

Browse files
virginiapujols-ymlmpospese
authored andcommitted
Create TextStyleLabel for SwiftUI
1 parent be9767c commit 9bc66c2

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,27 @@ label.typography = .headline
9292

9393
What you should _not_ do, however, is set the `font` property directly. Just set the typography and the class will take care of the font.
9494

95+
### Using Typography in SwiftUI
96+
To make the Typography elements work within the SwiftUI framework, we used `UIViewRepresentable`, a wrapper for a UIKit that integrates the view into your SwiftUI view hierarchy.
97+
98+
*Rendering Single Line Label:* `TextStyleLabel` is the first wrapper component we created, it renders a single line `TypographyLabel`, and as such, has the property `numberOfLines = 1`. Setting this property to any other value is undefined behavior.
99+
```
100+
// using `TextStyleLabel` in SwiftUI
101+
TextStyleLabel("Another sample headline", typography: .systemLabel)
102+
```
103+
104+
To add specific TypographyLabel configurations, use the `configuration` closure:
105+
```
106+
// using custom typography, the configuration closure and a View background modifier
107+
TextStyleLabel(
108+
"This is a long text string that will not fit",
109+
typography: .ParagraphUber.small.fontSize(28).lineHeight(45),
110+
configuration: { label in
111+
label.lineBreakMode = .byTruncatingMiddle
112+
})
113+
.background(Color.yellow.opacity(0.5))
114+
```
115+
95116
## Registering Custom Fonts
96117

97118
Any custom fonts need to be included as assets in your application and registered with the system. If you're building a simple app then you can just add them to your project and list them in your app's Info.plist file as you normally would. If, however, you want to build them into a separate Swift package, then that's fine too, and Y—MatterType has an extension on `UIFont` that makes it easier to register (and unregister) your font files. It throws an error if the font cannot be registered (and also if it has already been registered), so you'll know when you have a problem. Note that you will need to specify `subpath` if you use `.copy` for the resources in your Swift package file (and probably won't need to specify it if you use `.process`).
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
//
2+
// TextStyleLabel.swift
3+
// YMatterType
4+
//
5+
// Created by Virginia Pujols on 1/11/23.
6+
// Copyright © 2023 Y Media Labs. All rights reserved.
7+
//
8+
9+
import SwiftUI
10+
11+
public struct TextStyleLabel: View {
12+
private let text: String
13+
private let typography: Typography
14+
private let configureTypographyText: ((TypographyLabel) -> Void)?
15+
16+
public init(_ text: String,
17+
typography: Typography,
18+
configuration: ((TypographyLabel) -> Void)? = nil) {
19+
self.typography = typography
20+
self.text = text
21+
self.configureTypographyText = configuration
22+
}
23+
24+
public var body: some View {
25+
TypographyLabelRepresentable(text,
26+
typography: typography,
27+
configuration: configureTypographyText)
28+
.fixedSize(horizontal: false, vertical: true)
29+
}
30+
}
31+
32+
public extension TextStyleLabel {
33+
struct TypographyLabelRepresentable: UIViewRepresentable {
34+
public typealias UIViewType = TypographyLabel
35+
36+
private let typography: Typography
37+
private let text: String
38+
private let configureText: ((TypographyLabel) -> Void)?
39+
40+
public init(_ text: String,
41+
typography: Typography,
42+
configuration: ((TypographyLabel) -> Void)? = nil) {
43+
self.typography = typography
44+
self.text = text
45+
self.configureText = configuration
46+
}
47+
48+
public func makeUIView(context: Context) -> TypographyLabel {
49+
let label = TypographyLabel(typography: typography)
50+
label.text = text
51+
52+
label.numberOfLines = 1
53+
54+
label.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
55+
label.setContentCompressionResistancePriority(.defaultLow, for: .vertical)
56+
57+
configureText?(label)
58+
return label
59+
}
60+
61+
public func updateUIView(_ uiView: TypographyLabel, context: Context) {
62+
uiView.typography = typography
63+
uiView.text = text
64+
configureText?(uiView)
65+
}
66+
}
67+
}

0 commit comments

Comments
 (0)