You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
JustOneThing/WidgetShared/InterfaceExtensions.swift

56 lines
1.8 KiB

//
// InterfaceExtensions.swift
// JustOneThing
//
// Created by Alan Francis on 06/07/2023.
//
import SwiftUI
extension Color {
static func randomForegroundColor(_ colorScheme:ColorScheme = .light) -> Color {
if( colorScheme == .light ) {
return Color(red: .random(in: 0...0.5),
green: .random(in: 0...0.5),
blue: .random(in: 0...0.5))
} else {
return Color(red: .random(in: 0.5...1.0),
green: .random(in: 0.5...1.0),
blue: .random(in: 0.5...1.0))
}
}
}
public struct FitSystemFont: ViewModifier {
public var lineLimit: Int?
public var fontSize: CGFloat?
public var minimumScaleFactor: CGFloat
public var percentage: CGFloat
func rightSize(geometry:GeometryProxy) -> CGFloat {
min(
min(geometry.size.width, geometry.size.height) * percentage,
fontSize ?? CGFloat.greatestFiniteMagnitude
)
}
public func body(content: Content) -> some View {
GeometryReader { geometry in
content
.font(.system(size: rightSize(geometry: geometry),
weight: .bold,
design: .rounded))
.bold()
.lineLimit(self.lineLimit)
.minimumScaleFactor(self.minimumScaleFactor)
.position(x: geometry.frame(in: .local).midX, y: geometry.frame(in: .local).midY)
}
}
}
public extension View {
func fitSystemFont(lineLimit: Int? = nil, fontSize: CGFloat? = nil, minimumScaleFactor: CGFloat = 0.01, percentage: CGFloat = 1) -> ModifiedContent<Self, FitSystemFont> {
return modifier(FitSystemFont(lineLimit: lineLimit, fontSize: fontSize, minimumScaleFactor: minimumScaleFactor, percentage: percentage))
}
}