Skip to content

Commit e7b49f0

Browse files
authored
Add FontWidth text style (gonzalezreal#266)
1 parent 5df8a4a commit e7b49f0

File tree

4 files changed

+69
-0
lines changed

4 files changed

+69
-0
lines changed

Sources/MarkdownUI/Documentation.docc/MarkdownUI.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ You can use the built-in themes, create your own or override specific text and b
3838
- ``FontSize``
3939
- ``FontStyle``
4040
- ``FontWeight``
41+
- ``FontWidth``
4142
- ``StrikethroughStyle``
4243
- ``UnderlineStyle``
4344
- ``FontFamilyVariant``

Sources/MarkdownUI/Theme/TextStyle/Styles/Font+FontProperties.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ extension Font {
4141
font = font.weight(fontProperties.weight)
4242
}
4343

44+
if #available(iOS 16.0, macOS 13.0, tvOS 16.0, watchOS 9.0, *) {
45+
if fontProperties.width != .standard {
46+
font = font.width(fontProperties.width)
47+
}
48+
}
49+
4450
switch fontProperties.style {
4551
case .normal:
4652
break // do nothing

Sources/MarkdownUI/Theme/TextStyle/Styles/FontProperties.swift

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,15 @@ public struct FontProperties: Hashable {
9595
/// The font weight.
9696
public var weight: Font.Weight = Self.defaultWeight
9797

98+
/// The font width.
99+
@available(iOS 16.0, macOS 13.0, tvOS 16.0, watchOS 9.0, *)
100+
public var width: Font.Width {
101+
get { (self.widthStorage as? Font.Width) ?? .standard }
102+
set { self.widthStorage = newValue }
103+
}
104+
105+
private var widthStorage: AnyHashable?
106+
98107
/// The font size.
99108
public var size: CGFloat = Self.defaultSize
100109

@@ -106,6 +115,42 @@ public struct FontProperties: Hashable {
106115
round(self.size * self.scale)
107116
}
108117

118+
/// Creates a font properties value.
119+
/// - Parameters:
120+
/// - family: The font family.
121+
/// - familyVariant: The font family variant.
122+
/// - capsVariant: The font caps variant.
123+
/// - digitVariant: The font digit variant.
124+
/// - style: The font style.
125+
/// - weight: The font weight.
126+
/// - width: The font width
127+
/// - size: The font size.
128+
/// - scale: The font scale.
129+
@available(iOS 16.0, macOS 13.0, tvOS 16.0, watchOS 9.0, *)
130+
public init(
131+
family: FontProperties.Family = .system(),
132+
familyVariant: FontProperties.FamilyVariant = .normal,
133+
capsVariant: FontProperties.CapsVariant = .normal,
134+
digitVariant: FontProperties.DigitVariant = .normal,
135+
style: FontProperties.Style = .normal,
136+
weight: Font.Weight = Self.defaultWeight,
137+
width: Font.Width,
138+
size: CGFloat = Self.defaultSize,
139+
scale: CGFloat = 1
140+
) {
141+
self.init(
142+
family: family,
143+
familyVariant: familyVariant,
144+
capsVariant: capsVariant,
145+
digitVariant: digitVariant,
146+
style: style,
147+
weight: weight,
148+
size: size,
149+
scale: scale
150+
)
151+
self.width = width
152+
}
153+
109154
/// Creates a font properties value.
110155
/// - Parameters:
111156
/// - family: The font family.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import SwiftUI
2+
3+
/// A text style that adjusts the font width.
4+
@available(iOS 16.0, macOS 13.0, tvOS 16.0, watchOS 9.0, *)
5+
public struct FontWidth: TextStyle {
6+
private let width: Font.Width
7+
8+
/// Creates a font width text style.
9+
/// - Parameter width: The font width.
10+
public init(_ width: Font.Width) {
11+
self.width = width
12+
}
13+
14+
public func _collectAttributes(in attributes: inout AttributeContainer) {
15+
attributes.fontProperties?.width = self.width
16+
}
17+
}

0 commit comments

Comments
 (0)