Giter VIP home page Giter VIP logo

scaledtofit's Introduction

Swift Package Manager macOS iOS tvOS watchOS

ScaledToFit

ScaledToFit is a small library that provides funtions to scale up or down a rectangle size so that it fits into another rectangle size. The scaling is done so that the aspect ratio is preserved.

The spm package contains an example SwiftUI project which also contains visual tests to see how the scaling works and can be used to verify that it is correct.

The following screenshots are showing the scaling in action:

Usage

The scale logic itself is available in the generic Size type, which works with (width, height) tuples of any FloatingPoint type. So this can be used by any code, no matter the actual type of the size or rectangle:

let scaledSize = Size.scaledToFit(orinigalSize: (10, 15), size: (20, 30))

There is also a convenience extension for the CGSize type, which is available for all of Apple's platform SDKs:

let originalSize = CGSize(width: 10, height: 15)
let containerSize = CGSize(width: 20, height: 30)
let scaledSize = originalSize.scaledToFit(size: containerSize)

If you want to only scale down, use scaledDownToFit:

let scaledSize = originalSize.scaledDownToFit(maxSize: containerSize)

The new size will be a smaller version of the original size. If the original size completely fits into the provided container size, the function will return nil, indicating that no scaling is necessary. You can fall back to the original size in that case:

let scaledSize = originalSize.scaledDownToFit(maxSize: containerSize) ?? originalSize

Similarly, if you want to only scale up, use scaledUpToFit. The new size will be a larger version of the original size, or nil if no scaling is needed.

Installation with Swift Package Manager

Use this URL as a dependency: https://github.com/WilhelmOks/ScaledToFit.git

scaledtofit's People

Contributors

wilhelmoks avatar

Stargazers

 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.