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

Max font size refactor #38

Merged
merged 2 commits into from
Aug 18, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,14 @@ struct AllComponentsView: View {
ScrollView {
VStack(alignment: .leading, spacing: 5) {
VStack(alignment: .leading, spacing: 2) {
ScreenTitle("Screen Title", style: Config.current.sectionTitleStyle.with(
dynamicTypeMaxSize: .accessibility4
))
ScreenTitle("Screen Title", dynamicMaxSize: 90)
SectionTitle("Section Title")
ItemTitle("Item Title")
ItemTitle("Attributed Item Title") { string in
ItemTitle("Attributed Item Title", dynamicMaxSize: 32) { string in
if let range = string.range(of: "Attributed") {
string[range].foregroundColor = .red
string[range].underlineStyle = .single
string[range].font = .custom(Config.current.detailTextStyle)
string[range].font = .scaledFont(name: Config.current.detailTextStyle.theme.name, size: Config.current.detailTextStyle.theme.size, maxSize: 32)
}
}
}
Expand Down
22 changes: 17 additions & 5 deletions Sources/NiceComponents/Components/NiceText.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ public protocol NiceText: View {

init(_ text: String, style: TypeStyle?)

init(_ text: String, color: Color?, size: CGFloat?, lineLimit: Int?, dynamicMaxSize: DynamicTypeSize?)
init(_ text: String, color: Color?, size: CGFloat?, lineLimit: Int?, dynamicMaxSize: CGFloat?)

init(_ text: String, configure: (inout AttributedString) -> Void)
init(_ text: String, color: Color?, size: CGFloat?, lineLimit: Int?, dynamicMaxSize: CGFloat?, configure: (inout AttributedString) -> Void)
}

public extension NiceText {
Expand All @@ -29,7 +29,7 @@ public extension NiceText {
color: Color? = nil,
size: CGFloat? = nil,
lineLimit: Int? = nil,
dynamicMaxSize: DynamicTypeSize? = nil
dynamicMaxSize: CGFloat? = nil
) {
self.init(
text,
Expand All @@ -46,10 +46,22 @@ public extension NiceText {
self.init(AttributedString(text), style: style)
}

init(_ text: String, configure: (inout AttributedString) -> Void) {
init(
_ text: String,
color: Color? = nil,
size: CGFloat? = nil,
lineLimit: Int? = nil,
dynamicMaxSize: CGFloat? = nil,
configure: (inout AttributedString) -> Void
) {
var attributedString = AttributedString(text)
configure(&attributedString)
self.init(attributedString, style: Self.defaultStyle)
self.init(attributedString, style: Self.defaultStyle.with(
color: color,
size: size,
lineLimit: lineLimit,
dynamicTypeMaxSize: dynamicMaxSize
))
}
}

30 changes: 0 additions & 30 deletions Sources/NiceComponents/Helper/DynamicTypeSize+MaxSize.swift

This file was deleted.

28 changes: 17 additions & 11 deletions Sources/NiceComponents/Helper/ScaledFont.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,31 @@ public struct ScaledFont: ViewModifier {
var name: String?
var weight: Font.Weight
var size: CGFloat
var maxSize: DynamicTypeSize?
var maxSize: CGFloat?

public func body(content: Content) -> some View {
return content.font(.scaledFont(name: name, size: size, weight: weight, maxSize: maxSize))
}
}

@available(iOS 13, macCatalyst 13, tvOS 13, watchOS 6, *)
AZielinsky95 marked this conversation as resolved.
Show resolved Hide resolved
extension View {
public func scaledFont(name: String?, size: CGFloat, weight: Font.Weight?, maxSize: CGFloat? = nil) -> some View {
return self.modifier(ScaledFont(name: name, weight: weight ?? .regular, size: size, maxSize: maxSize))
}
}

public extension Font {
static func scaledFont(name: String?, size: CGFloat, weight: Font.Weight? = nil, maxSize: CGFloat? = nil) -> Font {
var scaledSize = UIFontMetrics.default.scaledValue(for: size)

if let maxFontSize = maxSize?.maxFontSize {
if let maxFontSize = maxSize {
scaledSize = min(maxFontSize, scaledSize)
}
if let name = name {
return content.font(.custom(name, size: scaledSize))
return Self.custom(name, size: scaledSize)
}

return content.font(Font.system(size: scaledSize, weight: weight))
}
}

@available(iOS 13, macCatalyst 13, tvOS 13, watchOS 6, *)
extension View {
public func scaledFont(name: String?, size: CGFloat, weight: Font.Weight?, maxSize: DynamicTypeSize? = nil) -> some View {
return self.modifier(ScaledFont(name: name, weight: weight ?? .medium, size: size, maxSize: maxSize))
return Font.system(size: scaledSize, weight: weight ?? .regular)
}
}
2 changes: 1 addition & 1 deletion Sources/NiceComponents/Theme/TypeStyle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public struct TypeStyle {
}

public extension TypeStyle {
func with(color: Color? = nil, size: CGFloat? = nil, lineLimit: Int? = nil, dynamicTypeMaxSize: DynamicTypeSize? = nil) -> TypeStyle {
func with(color: Color? = nil, size: CGFloat? = nil, lineLimit: Int? = nil, dynamicTypeMaxSize: CGFloat? = nil) -> TypeStyle {
TypeStyle(
color: color ?? self.color,
theme: TypeTheme.TextStyle(
Expand Down
4 changes: 2 additions & 2 deletions Sources/NiceComponents/Theme/TypeTheme.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ public struct TypeTheme {
public let name: String?
public let weight: Font.Weight?
public let size: CGFloat
public var dynamicTypeMaxSize: DynamicTypeSize?
public var dynamicTypeMaxSize: CGFloat?

public init(
_ name: String? = nil,
size: CGFloat,
weight: Font.Weight? = nil,
dynamicTypeMaxSize: DynamicTypeSize? = nil
dynamicTypeMaxSize: CGFloat? = nil
) {
self.name = name
self.size = size
Expand Down