Skip to content

Commit

Permalink
Add FontWidth text style (#266)
Browse files Browse the repository at this point in the history
  • Loading branch information
kirkbig authored Oct 10, 2023
1 parent 5df8a4a commit e7b49f0
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
1 change: 1 addition & 0 deletions Sources/MarkdownUI/Documentation.docc/MarkdownUI.md
Original file line number Diff line number Diff line change
Expand Up @@ -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``
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
45 changes: 45 additions & 0 deletions Sources/MarkdownUI/Theme/TextStyle/Styles/FontProperties.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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.
Expand Down
17 changes: 17 additions & 0 deletions Sources/MarkdownUI/Theme/TextStyle/Styles/FontWidth.swift
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit e7b49f0

Please sign in to comment.