CGRectAlignmentExtensions is a library that makes it easy to align rectangle to each other. It contains function to align, aspect fit and aspect fill rectangles based a container rectangle.
- Create a file name 'Podfile'
target '<PROJECT_NAME>' do
use_frameworks!
pod 'CGRectAlignmentExtensions', '>= 2.0'
end
- In the terminal in your project folder Run 'pod install' from
pod install
For more information about installing cocoapods see: cocoapos
- align
- aligned
- aspectFill
- aspectFilled
- aspectFit
- aspectFitted
- aspectFittedSquare
- center
- centered
- horizontalAlign
- horizontalAligned
- inset
- insetted
- verticalAlign
- verticaAligned
Align a rectangle horizontally to the right outside and vertically to the center of another rectangle
let rect1 = CGRect(10, 10, 100, 100)
var rect2 = CGRect(90, 90, 50, 80)
rect2.align(to: rect1, with: .leftOutside, and: .center)
Create an new aligned rectangle horizontally stretched and vertically aligned to the bottom
let rect1 = CGRect(10, 10, 100, 100)
let rect2 = CGRect(90, 90, 50, 80)
let rect3 = rect2.aligned(to: rect1, with: .stretchInside, and: .bottomInside)
Aspect fit a rectangle into a container
let rect1 = CGRect(10, 10, 100, 100)
var rect2 = CGRect(90, 90, 50, 80)
rect2.aspectFill(in: rect1)
Create a new rectangle of a rectangle that is aspect filled into a container
let rect1 = CGRect(10, 10, 100, 100)
let rect2 = CGRect(90, 90, 50, 80)
let rect3 = rect2.aspectFilled(in: rect1)
Aspect fit a rectangle into a container
let rect1 = CGRect(10, 10, 100, 100)
var rect2 = CGRect(90, 90, 50, 80)
rect2.aspectFit(in: rect1)
Create a new rectangle of a rectangle that is aspect fitted into a container
let rect1 = CGRect(10, 10, 100, 100)
let rect2 = CGRect(90, 90, 50, 80)
let rect3 = rect2.aspectFitted(in: rect1)
Create a square that is fitted inside the rectangle
let rect = CGRect(10, 10, 100, 100)
let square = rect.aspectFittedSquare()
Center a rectangle
let rect1 = CGRect(10, 10, 100, 100)
var rect2 = CGRect(90, 90, 50, 80)
rect2.center(to: rect1)
Create a new centered rectangle
let rect1 = CGRect(10, 10, 100, 100)
let rect2 = CGRect(90, 90, 50, 80)
let rect3 = rect2.centered(to: rect1)
Align a rectangle horizontally to the right outside of another rectangle
let rect1 = CGRect(10, 10, 100, 100)
var rect2 = CGRect(90, 90, 50, 80)
rect2.horizontalAligned(to: rect1, with: .rightOutside)
Create an new aligned rectangle horizontally stretched
let rect1 = CGRect(10, 10, 100, 100)
let rect2 = CGRect(90, 90, 50, 80)
let rect3 = rect2.horizontalAligned(to: rect1, with: .stretchInside)
Inset a rect by specific UIEdgeInsets
let rect = CGRect(10, 10, 100, 100)
let insets = UIEdgeInsets(top: 5, left: 10, bottom: 15, right: 30)
rect.inset(by: insets)
Create an new rectange of a rectangle that is insetted by specific UIEdgeInsets
let rect = CGRect(10, 10, 100, 100)
let insets = UIEdgeInsets(top: 5, left: 10, bottom: 15, right: 30)
let rect2 = rect.insetted(by: insets)
Align a rectangle horizontally to the top outside of another rectangle
let rect1 = CGRect(10, 10, 100, 100)
var rect2 = CGRect(90, 90, 50, 80)
rect2.verticalAlign(to: rect1, with: .topOutside)
Create an new aligned rectangle vertical stretched
let rect1 = CGRect(10, 10, 100, 100)
let rect2 = CGRect(90, 90, 50, 80)
let rect3 = rect2.verticalAligned(to: rect1, with: .stretchInside)