Coder Social home page Coder Social logo

fit's Introduction

Twitter: @OKorchytskyi

Fit allows you to lay out your views into lines without ever bothering to group or measure them, all thanks to the implementation of the Layout protocol.

Usage

Add your views just like you would do it with all other SwiftUI stacks:

import Fit

Fit {
    Text("Tags:")

    ForEach(tags) { tag in
        TagView(tag: tag)
    }

    TextField("New tag", text: $input)
}

tag_list_example

Note

Full code for this example: Tag List Example.

Customisation

Fit provides multiple ways to customise your layout:

Line Alignment and Spacing

// .leading (default), .center or .trailing
Fit(lineAlignment: .center, lineSpacing: 12) {
    // views
}

Item Alignment and Spacing

You can align items in the same way you would do in HStack:

// fixed item spacing
Fit(itemAlignment: .firstTextBaseline, itemSpacing: .fixed(8)) {
    // views
}

// view's preferred spacing
Fit(itemAlignment: .firstTextBaseline, itemSpacing: .viewSpacing(minimum: 8)) {
    // views
}

Line-Break

Use view modifier to add line-break before or after a particular view:

Fit {
    Text("Title:")
        .fit(lineBreak: .after)
    // next views
}

Per-line styling

You can define secific style for each line with LineStyle:

let conveyorBeltStyle: LineStyle = .lineSpecific { style, line in
    // reverse every second line
    style.reversed = (line.index + 1).isMultiple(of: 2)
    // if the line is reversed, it should start from the trailing edge
    style.alignment = style.reversed ? .trailing : .leading
}

Fit(lineStyle: conveyorBeltStyle) {
    // views
}

conveyor_belt_example

Note

Full code for this example: Conveyor Belt Example.

It is also possible to create a variable style:

var fancyAlignJustified: LineStyle {
    .lineSpecific { style, line in
        if stretch {
            style.stretched = line.percentageFilled >= threshold
        }
    }
}

Fit(lineStyle: fancyAlignJustified) {
    // views
}

stretching_example

Note

Full code for this example: Long Text Example.

Installing

Use Swift Package Manager to get Fit:

.package(url: "https://github.com/OlehKorchytskyi/Fit", from: "1.0.0")

Import Fit into your Swift code:

import Fit

License

MIT License.

Copyright (c) 2024 Oleh Korchytskyi.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

fit's People

Contributors

olehkorchytskyi avatar

Stargazers

Aron Cedercrantz avatar Karl avatar Joakim Hassila avatar  avatar Skydoomo avatar Ladd3r avatar Karmjit Singh avatar An Tran avatar Muhammad Umer avatar pranav-two avatar Stephan Deumier avatar Igor Tarasenko avatar Kwangmin Bae avatar  avatar Vatsal Manot avatar Pat Nakajima avatar Felix Green avatar Wayne Dahlberg avatar  avatar Sam Dawes avatar Tim Kersey avatar  avatar Vladyslav Chabaniuk avatar  avatar Sebastian avatar Daniel Capra avatar Peter Forward avatar Danilo Campos avatar  avatar Malte Ruff avatar Benjamin Vela avatar Alexander avatar Jaume Corbi avatar M.Z. avatar Nick Yang avatar Andrew Olechtchouk avatar Isaac Paschall avatar Htain Lin Shwe avatar  avatar  avatar Stef Kors avatar Lukas Pistrol avatar

Watchers

 avatar  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.