diff --git a/Sources/Orbit/Components/Text.swift b/Sources/Orbit/Components/Text.swift index 796ec4a3d87..3ceb633fe63 100644 --- a/Sources/Orbit/Components/Text.swift +++ b/Sources/Orbit/Components/Text.swift @@ -13,6 +13,7 @@ public struct Text: View, FormattedTextBuildable { @Environment(\.lineSpacing) private var lineSpacing @Environment(\.textAccentColor) private var textAccentColor @Environment(\.textColor) private var textColor + @Environment(\.textFontWeight) private var textFontWeight @Environment(\.sizeCategory) private var sizeCategory private let content: String @@ -106,8 +107,7 @@ public struct Text: View, FormattedTextBuildable { if #available(iOS 16.0, *), let isBold { return text .bold(isBold) - } - else if let isBold, isBold { + } else if let isBold, isBold { return text .bold() } else { @@ -244,7 +244,7 @@ public struct Text: View, FormattedTextBuildable { } private var resolvedFontWeight: Font.Weight? { - isBold == true ? .bold : fontWeight + isBold == true ? .bold : fontWeight ?? textFontWeight } private func designatedLineHeight(sizeCategory: ContentSizeCategory) -> CGFloat { diff --git a/Sources/Orbit/Support/Environment Keys/TextFontWeightKey.swift b/Sources/Orbit/Support/Environment Keys/TextFontWeightKey.swift new file mode 100644 index 00000000000..2382814c839 --- /dev/null +++ b/Sources/Orbit/Support/Environment Keys/TextFontWeightKey.swift @@ -0,0 +1,29 @@ +import SwiftUI + +struct TextFontWeightKey: EnvironmentKey { + static let defaultValue: Font.Weight? = nil +} + +public extension EnvironmentValues { + + /// A font weight stored in a view’s environment, used for Orbit text based components. + var textFontWeight: Font.Weight? { + get { self[TextFontWeightKey.self] } + set { self[TextFontWeightKey.self] = newValue } + } +} + +public extension View { + + /// Set the font weight for any text based Orbit component within the view hierarchy. + /// + /// - Parameters: + /// - fontWeight: A font weight that will be used in text based Orbit components. + /// Pass `nil` to ignore environment font weight and to allow the system + /// or the container to provide its own font weight. + /// If a container-specific override doesn’t exist, the `regular` will be used. + @available(iOS, introduced: 13, obsoleted: 16, renamed: "fontWeight") + func textFontWeight(_ fontWeight: Font.Weight?) -> some View { + environment(\.textFontWeight, fontWeight) + } +}