gitmyhub

Cartography

Swift ★ 7.3k updated 9mo ago

A declarative Auto Layout DSL for Swift :iphone::triangular_ruler:

A Swift library for iOS and macOS that replaces Apple's verbose Auto Layout API with short, math-like expressions so you can position UI elements on screen in one line instead of eight.

SwiftiOSmacOSCocoaPodssetup: easycomplexity 2/5

Cartography is a Swift library for iOS and macOS developers that makes positioning user interface elements on screen much less verbose. On Apple platforms, views (buttons, images, text labels, and so on) are sized and placed using a system called Auto Layout, where you define rules describing how elements should relate to each other in size and position. The native Auto Layout API requires a long, repetitive call for each rule. Cartography replaces this with a shorter, more readable style where constraints are written as familiar math-like expressions inside a small function call.

For example, saying that one button's right edge should sit 12 points to the left of another button's left edge takes about 8 lines with the native API. With Cartography the same rule fits on one line.

The library supports all standard layout attributes built into iOS and macOS: width, height, top, bottom, left, right, center, baseline, and the various margin and alignment variants. Compound shortcuts let you set multiple attributes at once, such as matching an element's full size to another view or placing it at its parent's center. Aligning a group of views along a shared edge, or distributing several views at even spacing, each require a single call.

Constraints can be captured into named groups and then replaced later with new constraints, which makes animated layout transitions straightforward. A shorthand operator lets you attach a priority value to any rule, which the system uses to decide which constraint to honor when two rules conflict.

Installation is via CocoaPods. Version compatibility follows Swift releases: versions 1.x target Swift 3, versions 2.x target Swift 4, and versions 4.x and above target Swift 5.

Where it fits