Skip to content

Commit

Permalink
Merge pull request #581 from kiwicom/578-text-font-weight
Browse files Browse the repository at this point in the history
Add `textFontWeight` modifier
  • Loading branch information
sjavora authored May 31, 2023
2 parents 8a9c328 + 15db221 commit 9f9ef39
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
6 changes: 3 additions & 3 deletions Sources/Orbit/Components/Text.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
29 changes: 29 additions & 0 deletions Sources/Orbit/Support/Environment Keys/TextFontWeightKey.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit 9f9ef39

Please sign in to comment.