๐Ÿ‘”view.resizableFont()

swiftโŸฉ custom โŸฉ extension โŸฉ Text+ โŸฉ .resizableFont()

This custom modifier allows your Text view's font size to fit the size of the view.

import SwiftUI

// โญ๏ธ custom modifier
struct FlexibleFontModifier: ViewModifier {

    var maxFontSize: Double
    var minScaleFactor : Double
    
    func body(content: Content) -> some View {
        content
            .font(.system(size: maxFontSize))
            .minimumScaleFactor(minScaleFactor)
    }
}

// view extension
extension View {
    // โญ๏ธ view.resizableFont()
    func resizableFont(maxFontSize: Double = 125, minScaleFactor: Double = 0.01) -> some View {
        self.modifier(FlexibleFontModifier(maxFontSize: maxFontSize, minScaleFactor: minScaleFactor))
    }
}

๐Ÿ“ ็ฏ„ไพ‹

Text("Hello")
    .resizableFont()        // โญ๏ธ custom modifier
    .frame(minWidth: 125)

Last updated