Swiftpack.co - 0x0001SUI/ScaledValues as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by 0x0001SUI.
0x0001SUI/ScaledValues v1.3.0
An alternative to the @ScaledMetric dynamic property.
⭐️ 1
🕓 3 weeks ago
iOS macOS watchOS tvOS macCatalyst
.package(url: "https://github.com/0x0001SUI/ScaledValues.git", from: "v1.3.0")

ScaledValues

The library provides you with an alternative to the @ScaledMetric dynamic property.

It has several dynamic property wrappers:

  • @ScaledValue
  • @ScaledPointSize
  • @ScaledFont
  • @ScaledInsets

All dynamic properties scale values according to the current Dynamic Type size and provide you with the ability to define minimum and maximum values.

@ScaledValue

With @ScaledValue you can scale an arbitrary layout value. It is the same as the standard @ScaledMetric, but it provides you with the ability to define minimum and maximum values.

import SwiftUI
import ScaledValues

struct ContentView: View {
    @ScaledValue(min: 30, max: 60, relativeTo: .body)
    private var shapeSize = 32.0
    
    @ScaledValue(min: 8, max: 15, relativeTo: .body)
    private var shapeRadius = 10.0

    var body: some View {
        RoundedRectangle(
            cornerRadius: shapeRadius, 
            style: .continuous
        )
        .fill(.green)
        .frame(width: shapeSize, height: shapeSize)
    }
}

@ScaledPointSize

With @ScaledPointSize you can scale a custom font size in points.

import SwiftUI
import ScaledValues

struct ContentView: View {
    
    <...>

    @ScaledPointSize(min: 15, max: 30, relativeTo: .body)
    private var fontSize = 16

    var body: some View {
        RoundedRectangle(
            cornerRadius: shapeRadius, 
            style: .continuous
        )
        .fill(.green)
        .frame(width: shapeSize, height: shapeSize)
        .overlay {
            Image(systemName: "star.fill")
                .foregroundColor(.yellow)
                .font(.system(size: fontSize))
        }
    }
}

@ScaledFont

As an alternative to @ScaledPointSize, you can define the font itself using @ScaledFont. This makes your code neater, as you can identify an accessible font in one place.

import SwiftUI
import ScaledValues

struct ArticleRow: View {
    @ScaledFont(size: 18, leading: .tight, relativeTo: .headline)
    private var titleFont: Font

    @ScaledFont(size: 14, design: .serif, relativeTo: .caption)
    private var summaryFont: Font

    @ObservedObject var article: Article

    var body: some View {
        HStack {
            VStack(alignment: .leading) {
                Text(article.title)
                    .font(titleFont)

                Text(article.summary)
                    .font(summaryFont)
            }

            Spacer()
        }
    }
}

@ScaledInsets

With @ScaledInsets you can scale the inset distances.

import SwiftUI
import ScaledValues

struct ArticlesView: View {
    @ScaledInsets(max: maxRowInsets, relativeTo: .title)
    private var rowInsets = defaultRowInsets
    
    var articles: [Article]

    var body: some View {
        List(articles) {
            ArticleRow($0)
                .listRowInsets(rowInsets)
        }
    }

    private static var defaultRowInsets: EdgeInsets {
        .init(
            top: 5, 
            leading: 10, 
            bottom: 5, 
            trailing: 15)
    }

    private static var maxRowInsets: EdgeInsets {
        .init(
            top: 10, 
            leading: 20, 
            bottom: 10, 
            trailing: 20)
    }
}

GitHub

link
Stars: 1
Last commit: 3 weeks ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Release Notes

v1.3.0
3 weeks ago

Added @ScaledInsets.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics