Giter VIP home page Giter VIP logo

scaledvalues's Introduction

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)
    }
}

scaledvalues's People

Contributors

dmkskn avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.