diff --git a/Sources/MarkdownUI/Documentation.docc/MarkdownUI.md b/Sources/MarkdownUI/Documentation.docc/MarkdownUI.md index 8cd739dc..385a3eb5 100644 --- a/Sources/MarkdownUI/Documentation.docc/MarkdownUI.md +++ b/Sources/MarkdownUI/Documentation.docc/MarkdownUI.md @@ -38,6 +38,7 @@ You can use the built-in themes, create your own or override specific text and b - ``FontSize`` - ``FontStyle`` - ``FontWeight`` +- ``FontWidth`` - ``StrikethroughStyle`` - ``UnderlineStyle`` - ``FontFamilyVariant`` diff --git a/Sources/MarkdownUI/Theme/TextStyle/Styles/Font+FontProperties.swift b/Sources/MarkdownUI/Theme/TextStyle/Styles/Font+FontProperties.swift index 0dcfc0d5..85301413 100644 --- a/Sources/MarkdownUI/Theme/TextStyle/Styles/Font+FontProperties.swift +++ b/Sources/MarkdownUI/Theme/TextStyle/Styles/Font+FontProperties.swift @@ -41,6 +41,12 @@ extension Font { font = font.weight(fontProperties.weight) } + if #available(iOS 16.0, macOS 13.0, tvOS 16.0, watchOS 9.0, *) { + if fontProperties.width != .standard { + font = font.width(fontProperties.width) + } + } + switch fontProperties.style { case .normal: break // do nothing diff --git a/Sources/MarkdownUI/Theme/TextStyle/Styles/FontProperties.swift b/Sources/MarkdownUI/Theme/TextStyle/Styles/FontProperties.swift index 974817ae..10090cd8 100644 --- a/Sources/MarkdownUI/Theme/TextStyle/Styles/FontProperties.swift +++ b/Sources/MarkdownUI/Theme/TextStyle/Styles/FontProperties.swift @@ -95,6 +95,15 @@ public struct FontProperties: Hashable { /// The font weight. public var weight: Font.Weight = Self.defaultWeight + /// The font width. + @available(iOS 16.0, macOS 13.0, tvOS 16.0, watchOS 9.0, *) + public var width: Font.Width { + get { (self.widthStorage as? Font.Width) ?? .standard } + set { self.widthStorage = newValue } + } + + private var widthStorage: AnyHashable? + /// The font size. public var size: CGFloat = Self.defaultSize @@ -106,6 +115,42 @@ public struct FontProperties: Hashable { round(self.size * self.scale) } + /// Creates a font properties value. + /// - Parameters: + /// - family: The font family. + /// - familyVariant: The font family variant. + /// - capsVariant: The font caps variant. + /// - digitVariant: The font digit variant. + /// - style: The font style. + /// - weight: The font weight. + /// - width: The font width + /// - size: The font size. + /// - scale: The font scale. + @available(iOS 16.0, macOS 13.0, tvOS 16.0, watchOS 9.0, *) + public init( + family: FontProperties.Family = .system(), + familyVariant: FontProperties.FamilyVariant = .normal, + capsVariant: FontProperties.CapsVariant = .normal, + digitVariant: FontProperties.DigitVariant = .normal, + style: FontProperties.Style = .normal, + weight: Font.Weight = Self.defaultWeight, + width: Font.Width, + size: CGFloat = Self.defaultSize, + scale: CGFloat = 1 + ) { + self.init( + family: family, + familyVariant: familyVariant, + capsVariant: capsVariant, + digitVariant: digitVariant, + style: style, + weight: weight, + size: size, + scale: scale + ) + self.width = width + } + /// Creates a font properties value. /// - Parameters: /// - family: The font family. diff --git a/Sources/MarkdownUI/Theme/TextStyle/Styles/FontWidth.swift b/Sources/MarkdownUI/Theme/TextStyle/Styles/FontWidth.swift new file mode 100644 index 00000000..2e38bded --- /dev/null +++ b/Sources/MarkdownUI/Theme/TextStyle/Styles/FontWidth.swift @@ -0,0 +1,17 @@ +import SwiftUI + +/// A text style that adjusts the font width. +@available(iOS 16.0, macOS 13.0, tvOS 16.0, watchOS 9.0, *) +public struct FontWidth: TextStyle { + private let width: Font.Width + + /// Creates a font width text style. + /// - Parameter width: The font width. + public init(_ width: Font.Width) { + self.width = width + } + + public func _collectAttributes(in attributes: inout AttributeContainer) { + attributes.fontProperties?.width = self.width + } +}