Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add textFontWeight modifier #581

Merged
merged 1 commit into from
May 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
username0x0a marked this conversation as resolved.
Show resolved Hide resolved
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)
PavelHolec marked this conversation as resolved.
Show resolved Hide resolved
}
}