TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

A modifier to help if you using a custom type

Forums > SwiftUI

Enter your custom font name in "Your Font Here".

/// A modifier to change `Font`
/// - Parameters:
///   - size: Size of `Font`
///   - fixedSize: If set to `true` then the font size will not change dynamically. Default is `false`
/// - Returns: A view of size and font
struct SiennaFont: ViewModifier {
    let size: CGFloat
    let fixedSize: Bool
    let font = "Your Font Here"

    func body(content: Content) -> some View {
        content
            .font(fixedSize ? .custom(font, fixedSize: size): .custom(font, size: size))
    }
}
extension View {
    /// A modifier to change `Font`
    /// - Parameters:
    ///   - size: Size of `Font`
    ///   - fixedSize: If set to `true` then the font size will not change dynamically. Default is `false`
    /// - Returns: A view of size and font
    func myCustomFont(size: CGFloat, fixedSize: Bool = false) -> some View {
        modifier(SiennaFont(size: size, fixedSize: fixedSize))
    }
}

Then use it like if you want the text to change dynamically.

Text("Hello World!")
     .myCustomFont(size: 40)

or if you want it fixed size then

Text("Hello World!")
     .myCustomFont(size: 40, fixedSize: true)

2      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.