Skip to content

Commit 3424e5a

Browse files
Mark Pospeselmpospese
Mark Pospesel
authored andcommitted
Add tvOS support
1 parent b3f1017 commit 3424e5a

23 files changed

+191
-109
lines changed

Diff for: Package.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ let package = Package(
66
name: "YMatterType",
77
defaultLocalization: "en",
88
platforms: [
9-
.iOS(.v14)
9+
.iOS(.v14),
10+
.tvOS(.v14)
1011
],
1112
products: [
1213
.library(

Diff for: README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
![Y-Matter Type](https://mpospese.com/wp-content/uploads/2022/08/YMatterType-hero-compact.jpeg)
22
[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fyml-org%2FYMatterType%2Fbadge%3Ftype%3Dswift-versions)](https://swiftpackageindex.com/yml-org/YMatterType) [![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fyml-org%2FYMatterType%2Fbadge%3Ftype%3Dplatforms)](https://swiftpackageindex.com/yml-org/YMatterType)
3-
_An opinionated take on Design System Typography for iOS._
3+
_An opinionated take on Design System Typography for iOS and tvOS._
44

55
This framework uses Figma's concept of Typography to create text-based UI elements (labels, buttons, text fields, and text views) that render themselves as described in Figma design files (especially sizing themselves according to line height) while also supporting Dynamic Type scaling and the Bold Text accessibility setting.
66

@@ -17,7 +17,7 @@ Documentation is automatically generated from source code comments and rendered
1717

1818
## What is Y—MatterType?
1919

20-
Y—MatterType is a framework that assists in getting typography done right when constructing iOS user interfaces from Figma-based designs.
20+
Y—MatterType is a framework that assists in getting typography done right when constructing iOS and tvOS user interfaces from Figma-based designs.
2121

2222
Y—MatterType aims to achieve the following goals:
2323

Diff for: Sources/YMatterType/Elements/TypographyButton.swift

+12-12
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ import UIKit
1313
open class TypographyButton: UIButton {
1414
/// The current typographical layout
1515
public private(set) var layout: TypographyLayout!
16-
16+
1717
/// Typography to be used for this buttons's title label
1818
public var typography: Typography {
1919
didSet {
2020
adjustFonts()
2121
}
2222
}
23-
23+
2424
/// (Optional) maximum point size when scaling the font.
2525
///
2626
/// Value should be greater than Typography.fontSize.
@@ -61,15 +61,15 @@ open class TypographyButton: UIButton {
6161
super.init(frame: .zero)
6262
build()
6363
}
64-
64+
6565
/// :nodoc:
6666
required public init?(coder: NSCoder) { nil }
6767

6868
private enum TextSetMode {
6969
case text
7070
case attributedText
7171
}
72-
72+
7373
private var textSetMode: TextSetMode = .text
7474

7575
/// :nodoc:
@@ -79,15 +79,15 @@ open class TypographyButton: UIButton {
7979
textSetMode = .text
8080
styleText(title, for: state)
8181
}
82-
82+
8383
/// :nodoc:
8484
override public func setAttributedTitle(_ title: NSAttributedString?, for state: UIControl.State) {
8585
// When text is set, we may need to re-style it as attributedText
8686
// with the correct paragraph style to achieve the desired line height.
8787
textSetMode = .attributedText
8888
styleAttributedText(title, for: state)
8989
}
90-
90+
9191
/// Gets or sets the text alignment of the button's title label.
9292
/// Default value = `.natural`
9393
public var textAlignment: NSTextAlignment = .natural {
@@ -99,7 +99,7 @@ open class TypographyButton: UIButton {
9999
}
100100
}
101101
}
102-
102+
103103
/// Gets or sets the line break mode of the button's title label.
104104
/// Default value = `.byTruncatingTail`
105105
public var lineBreakMode: NSLineBreakMode = .byTruncatingTail {
@@ -119,7 +119,7 @@ open class TypographyButton: UIButton {
119119
if traitCollection.hasDifferentFontAppearance(comparedTo: previousTraitCollection) {
120120
adjustFonts()
121121
}
122-
122+
123123
if traitCollection.hasDifferentColorAppearance(comparedTo: previousTraitCollection) {
124124
adjustColors()
125125
}
@@ -212,16 +212,16 @@ private extension TypographyButton {
212212
adjustColors()
213213
adjustBreakpoint()
214214
}
215-
215+
216216
func configure() {
217217
setTitleColor(.label, for: .normal)
218218
titleLabel?.adjustsFontForContentSizeCategory = true
219219
}
220-
220+
221221
@objc func contentSizeDidChange() {
222222
adjustFonts()
223223
}
224-
224+
225225
func restyleText() {
226226
styleAttributedText(currentAttributedTitle, for: state)
227227
}
@@ -240,7 +240,7 @@ private extension TypographyButton {
240240
let attributedText = layout.styleText(newValue, lineMode: lineMode)
241241
super.setAttributedTitle(attributedText, for: state)
242242
}
243-
243+
244244
func styleAttributedText(_ newValue: NSAttributedString?, for state: UIControl.State) {
245245
defer { invalidateIntrinsicContentSize() }
246246
guard let layout = layout,

Diff for: Sources/YMatterType/Elements/TypographyLabel.swift

+11-11
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ open class TypographyLabel: UILabel {
2020
adjustFonts()
2121
}
2222
}
23-
23+
2424
/// (Optional) maximum point size when scaling the font.
2525
///
2626
/// Value should be greater than Typography.fontSize.
@@ -61,15 +61,15 @@ open class TypographyLabel: UILabel {
6161
super.init(frame: .zero)
6262
build()
6363
}
64-
64+
6565
/// :nodoc:
6666
required public init?(coder: NSCoder) { nil }
6767

6868
private enum TextSetMode {
6969
case text
7070
case attributedText
7171
}
72-
72+
7373
private var textSetMode: TextSetMode = .text
7474

7575
/// :nodoc:
@@ -82,7 +82,7 @@ open class TypographyLabel: UILabel {
8282
styleText(newValue)
8383
}
8484
}
85-
85+
8686
/// :nodoc:
8787
override public var attributedText: NSAttributedString? {
8888
get { super.attributedText }
@@ -104,7 +104,7 @@ open class TypographyLabel: UILabel {
104104
}
105105
}
106106
}
107-
107+
108108
/// :nodoc:
109109
override public var lineBreakMode: NSLineBreakMode {
110110
didSet {
@@ -123,7 +123,7 @@ open class TypographyLabel: UILabel {
123123
if traitCollection.hasDifferentFontAppearance(comparedTo: previousTraitCollection) {
124124
adjustFonts()
125125
}
126-
126+
127127
if traitCollection.hasDifferentColorAppearance(comparedTo: previousTraitCollection) {
128128
adjustColors()
129129
}
@@ -132,7 +132,7 @@ open class TypographyLabel: UILabel {
132132
adjustBreakpoint()
133133
}
134134
}
135-
135+
136136
/// Call this if you've made a change that would require text to be re-styled. (Normally this is not necessary).
137137
/// Override this if you need to do something additional when preferred content size
138138
/// or legibility weight has changed
@@ -145,7 +145,7 @@ open class TypographyLabel: UILabel {
145145
font = layout.font
146146
restyleText()
147147
}
148-
148+
149149
/// Override this if you have colors that will not automatically adjust to
150150
/// Light / Dark mode, etc. This can be the case for CGColor or
151151
/// non-template images (or backgroundImages).
@@ -167,11 +167,11 @@ private extension TypographyLabel {
167167
adjustColors()
168168
adjustBreakpoint()
169169
}
170-
170+
171171
func configure() {
172172
adjustsFontForContentSizeCategory = true
173173
}
174-
174+
175175
func restyleText() {
176176
if textSetMode == .text {
177177
styleText(text)
@@ -193,7 +193,7 @@ private extension TypographyLabel {
193193
// Set attributed text to match typography
194194
super.attributedText = layout.styleText(newValue, lineMode: lineMode)
195195
}
196-
196+
197197
func styleAttributedText(_ newValue: NSAttributedString?) {
198198
defer { invalidateIntrinsicContentSize() }
199199
guard let layout = layout,

Diff for: Sources/YMatterType/Elements/TypographyTextField.swift

+19-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,17 @@ open class TypographyTextField: UITextField {
2525
}
2626
}
2727

28-
/// Insets to apply around the functional area of the `UITextField`. Defaults to `.zero`
29-
public var textInsets: UIEdgeInsets = .zero {
28+
/// Default text insets (values vary by platform)
29+
public static var defaultTextInsets: UIEdgeInsets = {
30+
#if os(tvOS)
31+
UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20)
32+
#else
33+
.zero
34+
#endif
35+
}()
36+
37+
/// Insets to apply around the functional area of the `UITextField`.
38+
public var textInsets: UIEdgeInsets = TypographyTextField.defaultTextInsets {
3039
didSet {
3140
if textInsets != oldValue {
3241
invalidateIntrinsicContentSize()
@@ -162,12 +171,20 @@ open class TypographyTextField: UITextField {
162171

163172
/// :nodoc
164173
open override func textRect(forBounds bounds: CGRect) -> CGRect {
174+
#if os(tvOS)
175+
bounds.inset(by: textInsets)
176+
#else
165177
super.textRect(forBounds: bounds).inset(by: textInsets)
178+
#endif
166179
}
167180

168181
/// :nodoc
169182
open override func editingRect(forBounds bounds: CGRect) -> CGRect {
183+
#if os(tvOS)
184+
bounds.inset(by: textInsets)
185+
#else
170186
super.editingRect(forBounds: bounds).inset(by: textInsets)
187+
#endif
171188
}
172189

173190
/// :nodoc

Diff for: Sources/YMatterType/Extensions/UIKit/NSParagraphStyle+lineSpacing.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ extension NSParagraphStyle {
1717
paragraphStyle.lineSpacing = lineSpacing
1818
return paragraphStyle
1919
}
20-
20+
2121
/// Combines line height multiple with the existing style
2222
/// - Parameter lineHeightMultiple: the line height multiple to use
2323
/// - Returns: Current paragraph style combined with line height multiple
@@ -26,7 +26,7 @@ extension NSParagraphStyle {
2626
paragraphStyle.lineHeightMultiple = lineHeightMultiple
2727
return paragraphStyle
2828
}
29-
29+
3030
/// Combines text alignment with the existing style
3131
/// - Parameter alignment: the text alignment to use
3232
/// - Returns: Current paragraph style combined with text alignment

Diff for: Sources/YMatterType/Typography/FontFamily/DefaultFontFamily.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ import UIKit
1212
public struct DefaultFontFamily: FontFamily {
1313
/// Suffix to use for italic family font names "Italic"
1414
public static let italicSuffix = "Italic"
15-
15+
1616
/// Font family root name, e.g. "AvenirNext"
1717
public let familyName: String
18-
18+
1919
/// Font style, e.g. regular or italic
2020
public let style: Typography.FontStyle
21-
21+
2222
/// Initialize a `DefaultFontFamily` object
2323
/// - Parameters:
2424
/// - familyName: font family name
@@ -27,7 +27,7 @@ public struct DefaultFontFamily: FontFamily {
2727
self.familyName = familyName
2828
self.style = style
2929
}
30-
30+
3131
/// Optional suffix to use for the font name.
3232
///
3333
/// Used by `FontFamily.fontName(for:compatibleWith:)`

Diff for: Sources/YMatterType/Typography/FontFamily/FontFamily.swift

+8-8
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import os
1414
public protocol FontFamily {
1515
/// Font family root name, e.g. "AvenirNext"
1616
var familyName: String { get }
17-
17+
1818
/// Optional suffix to use for the font name.
1919
///
2020
/// Used by `FontFamily.fontName(for:compatibleWith:)`
@@ -31,7 +31,7 @@ public protocol FontFamily {
3131

3232
// The following four methods have default implementations that
3333
// can be overridden in custom implementations of `FontFamily`.
34-
34+
3535
/// Returns a font for the specified `weight` and `pointSize` that is compatible with the `traitCollection`
3636
/// - Parameters:
3737
/// - weight: desired font weight
@@ -50,12 +50,12 @@ public protocol FontFamily {
5050
/// If `nil` then `UIAccessibility.isBoldTextEnabled` will be considered instead
5151
/// - Returns: The font name formulated from `familyName` and `weight`
5252
func fontName(for weight: Typography.FontWeight, compatibleWith traitCollection: UITraitCollection?) -> String
53-
53+
5454
/// Generates a weight name suffix as part of a full font name. Not all fonts support all 9 weights.
5555
/// - Parameter weight: desired font weight
5656
/// - Returns: The weight name to use
5757
func weightName(for weight: Typography.FontWeight) -> String
58-
58+
5959
/// Returns the alternate weight to use if user has requested a bold font. e.g. might convert `.regular`
6060
/// to `.semibold`. Not all fonts support all 9 weights.
6161
/// - Parameter weight: desired font weight
@@ -94,7 +94,7 @@ extension FontFamily {
9494
}
9595
return font
9696
}
97-
97+
9898
public func fontName(
9999
for weight: Typography.FontWeight,
100100
compatibleWith traitCollection: UITraitCollection?
@@ -105,7 +105,7 @@ extension FontFamily {
105105
let weightName = weightName(for: actualWeight)
106106
return "\(familyName)-\(weightName)\(fontNameSuffix)"
107107
}
108-
108+
109109
public func weightName(for weight: Typography.FontWeight) -> String {
110110
// Default font name suffix by weight
111111
switch weight {
@@ -129,7 +129,7 @@ extension FontFamily {
129129
return "Black"
130130
}
131131
}
132-
132+
133133
public func accessibilityBoldWeight(for weight: Typography.FontWeight) -> Typography.FontWeight {
134134
// By default returns the next heavier supported weight (if any), otherwise the heaviest supported weight
135135
let weights = supportedWeights.sorted(by: { $0.rawValue < $1.rawValue })
@@ -147,7 +147,7 @@ extension FontFamily {
147147
guard let traitCollection = traitCollection else {
148148
return UIAccessibility.isBoldTextEnabled
149149
}
150-
150+
151151
return traitCollection.legibilityWeight == .bold
152152
}
153153
}

Diff for: Sources/YMatterType/Typography/FontFamily/SystemFontFamily.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public struct SystemFontFamily: FontFamily {
5252
// The system font has a private font family name (literally ".SFUI"), so
5353
// just return empty string for familyName. The system font can't be retrieved by name anyway.
5454
public var familyName: String { "" }
55-
55+
5656
/// Returns a font for the specified `weight` and `pointSize` that is compatible with the `traitCollection`
5757
/// - Parameters:
5858
/// - weight: desired font weight

0 commit comments

Comments
 (0)