To create a custom font property wrapper in SwiftUI, you can follow these steps:
Font+.swift
(or any name you prefer)main.swift28 chars3 lines
@propertyWrapper
:main.swift423 chars18 lines
In the above code, we created a @propertyWrapper
called AppFont
. The wrappedValue
property returns the font object, and the init(wrappedValue:)
initializer sets the font. Inside the set
method, we can customize the font as desired.
Now, you can use it like so:
main.swift164 chars9 lines
In the above code, we created an instance of AppFont
called titleFont
, which has a default font of .headline
. We then use this titleFont
in the font()
modifier of the Text
view.
Now you can easily apply your custom font in any SwiftUI view and it automatically gets updated everywhere.
gistlibby LogSnag