You need to install Introspect
library.
How to use
It should be used on root of navigationView. If you use
NavigationView {
VStack { ... }
// ↓ set custom font
.navigationTitleFont(font: UIFont.custom.extraBold(ofSize: 20))
}
3 Steps
- Create ViewModifier
- Add extension method to UIFont
- Add extension method to View
Step1: Create ViewModifier
import SwiftUI
import Introspect
public struct NavigationTitleFontModifier: ViewModifier {
public var font: UIFont
public func body(content: Content) -> some View {
content
.introspectNavigationController {
$0.navigationBar.titleTextAttributes = [.font:font]
}
}
}
Step2: Add extension method to UIFont
This is unnecessary. It just for make call custom font easily.
extension UIFont {
public struct custom {
static func extraBold(ofSize size: CGFloat) -> UIFont {
return UIFont(name: "BoldCustomFontName", size: size) ?? UIFont.systemFont(ofSize: size, weight: .bold)
}
}
}
Step3: Add extension method to View
public extension View {
func navigationTitleFont(font: UIFont) -> some View {
self.modifier(NavigationTitleFontModifier(font: font))
}
}
Top comments (0)