• Stars
    star
    120
  • Rank 295,983 (Top 6 %)
  • Language
    Swift
  • License
    MIT License
  • Created about 10 years ago
  • Updated about 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A collection of CGRect, CGPoint and CGSize convenience functions for Swift

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.

More Repositories

1

FontAwesomeIconFactory

A factory for turning Font Awesome icons into images for user interface controls. Works for iOS and OS X.
Objective-C
365
star
2

Gitifier

Git commit notifier for MacOSX
Objective-C
314
star
3

highlight-symbol.el

Emacs: automatic and manual symbol highlighting
Emacs Lisp
276
star
4

SwiftHamcrest

Hamcrest test assertions for Swift
Swift
260
star
5

window-numbering.el

Emacs: Numbered window shortcuts
Emacs Lisp
162
star
6

full-ack

An Emacs front-end for ack
Emacs Lisp
66
star
7

AutoLayoutMacros

Macros for making Auto Layout more readable (and writable) without using strings.
Objective-C
54
star
8

fringe-helper.el

Emacs: helper functions for fringe bitmaps
Emacs Lisp
47
star
9

homebridge-twinkly

JavaScript
37
star
10

guess-style

Emacs: automatic setting of code style variables
Emacs Lisp
32
star
11

auto-dictionary-mode

Emacs: automatic dictionary switcher for flyspell
Emacs Lisp
27
star
12

rotate-text.el

Emacs: cycle through words, symbols and patterns
Emacs Lisp
19
star
13

kill-ring-search.el

Emacs: incremental search for the kill ring
Emacs Lisp
10
star
14

idle-require.el

Emacs: load elisp libraries while Emacs is idle
Emacs Lisp
9
star
15

macro-math.el

Emacs: in-buffer mathematical operations
Emacs Lisp
7
star
16

talks

talk slides
Swift
6
star
17

doc-mode

Emacs: convenient editing of in-code documentation
Emacs Lisp
6
star
18

echo-pick.el

Emacs: filter for echo area status messages
Emacs Lisp
3
star
19

tempo-snippets.el

visual insertion of templates for Emacs
Emacs Lisp
2
star
20

async-eval.el

Emacs: execute Emacs lisp in a separate process
Emacs Lisp
2
star
21

recent-addresses.el

Emacs: store and recall recently used email addresses
Emacs Lisp
1
star
22

elk-test

Emacs Lisp testing framework
Emacs Lisp
1
star
23

compile-bookmarks.el

Emacs: bookmarks for compilation commands
Emacs Lisp
1
star