Giter Club home page Giter Club logo

swiftcgrectextensions's Introduction

Swift CGRect Extensions

A collection of CGRect, CGPoint and CGSize convenience functions for Swift on OS X and iOS.

The goal is to provide clear functions for the most commonly used rect operations, even if the code savings are minimal. The reason is that function names communicate the programmer's intent more clearly than setting properties with ad-hoc calculations.

Pull requests and suggestions are welcome.

Swift 2.0 OS X ≥ 10.9 iOS ≥ 7.0 CocoaPods compatible Carthage compatible

Features

  • Operators for adding, scaling sizes, applying CGAffineTransform
  • Shortcut properties for corner and center points
  • Offsetting, insetting and extending methods with multiple ways of specifying arguments
  • Aligning and centering sizes in rects
  • Copying rects with some values changed without going through a variable
  • Respects the different coordinate systems on iOS and OS X
  • Unit tested

Examples

These examples are also available as a Playground in the Xcode project.

import Foundation
import CGRectExtensions

let rect = CGRect(1, 2, 100, 200) // shorter constructor

let minY = rect.minY // shortcut properties

let topCenter = rect.topCenter // OS-dependent coordinate system

let oppositeOrigin = rect.origin + rect.size // adding

let corneredRect = rect.with(x: 0, y: 0) // modified copy

let nextPageRect = rect.offsetBy(dx: 100) // offsetting

let paddedRect = rect.insetBy(top: 66, left: 10, right: 10) // insetting

let quarterSize = rect.size * 0.5 // scaling sizes
let corner = rect.align(quarterSize, corner: .MinXEdge, .MinYEdge) // aligning sizes

let halfWidthSize = rect.size * (0.5, 1) // scaling sizes
let centeredRect = rect.center(halfWidthSize) // centering sizes

let scaledRect = rect * CGAffineTransformMakeScale(2.0, 3.0)

Mutating functions are also available. These are especially convenient since Swift allows modifying a struct property directly.

import Cocoa
import CGRectExtensions

let view = NSView()
view.frame.size = CGSize(width: 100, height: 200)

view.frame.bottomLeft = CGPoint(x: 1, y: 2)

view.frame.offsetInPlace(25, 25)

view.frame.insetInPlace(top: 66, left: 10, right: 10)

view.frame.centerInPlace(CGSize(50, 50))

view.frame.centerInPlace(CGSize(50, 50), alignTo: .MinXEdge)

view.frame.alignInPlace(CGSize(50, 50), corner: .MinXEdge, .MinYEdge)

view.frame *= CGAffineTransformMakeScale(2.0, 3.0)

Integration

CocoaPods

Add the following to your Podfile:

pod 'CGRectExtensions'

Carthage

Add the following to your Cartfile:

github 'nschum/SwiftCGRectExtensions'

Changes

2.0

  • Updated for Swift 2
  • Functions renamed to match Swift 2 naming scheme for mutating methods.

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.