resizableFont()
๐ ๅ่๏ผSwift Playgrounds for iPad (Keep Going with Apps)
import SwiftUI
// ๐ view.resizableFont()
extension View {
func resizableFont(
maxSize : Double = 125,
minScale: Double = 0.01
) -> some View {
self.modifier(FlexibleFontModifier(
maxSize: maxSize,
minScale: minScale
))
}
}
// ๐ FlexibleFontModifier
private struct FlexibleFontModifier: ViewModifier {
var maxSize : Double
var minScale: Double
func body(content: Content) -> some View {
content
.font(.system(size: maxSize))
.minimumScaleFactor(minScale)
}
}
// โญ๏ธ ObservableObject (with @Published properties)
class CreatureZoo : ObservableObject {
@Published var creatures = [
Creature(name: "Gorilla", emoji: "๐ฆ"),
Creature(name: "Peacock", emoji: "๐ฆ"),
Creature(name: "Squid", emoji: "๐ฆ"),
Creature(name: "Chick", emoji: "๐ฃ"),
Creature(name: "Dog", emoji: "๐ถ"),
]
}
// โญ๏ธ content view (details omitted)
// โญ๏ธ @StateObject
@StateObject private var data = CreatureZoo()
// embeded in a `List` ...
ForEach(data.creatures) { creature in
HStack {
Text(creature.name).font(.title)
Spacer()
Text(creature.emoji)
// โญ๏ธ custom view modifier
.resizableFont() // ๐ view.resizableFont()
.frame(minWidth: 125)
}
}
SwiftUI โฉ
SwiftOnTap โฉ
Last updated