diff --git a/SteviaPod/Classes/Stevia+Alignment.swift b/SteviaPod/Classes/Stevia+Alignment.swift index 0a8c226..ed30c2a 100644 --- a/SteviaPod/Classes/Stevia+Alignment.swift +++ b/SteviaPod/Classes/Stevia+Alignment.swift @@ -8,425 +8,426 @@ #if canImport(UIKit) import UIKit - -/** Aligns an array of views Horizontally (on the X Axis) - - Example Usage: - ``` - align(horizontally: label,button,arrow) - ``` - - Can also be used directly on horizontal layouts since they return the array of views : - ``` - align(horizontally: |-image1-image2-image3-|) - ``` - - - Returns: The array of views, enabling chaining, - - */ -@discardableResult -public func align(horizontally views: UIView...) -> [UIView] { - return align(horizontally: views) -} - -@available(*, deprecated, renamed: "align(horizontally:)") -@discardableResult -public func alignHorizontally(_ views: UIView...) -> [UIView] { - return align(horizontally: views) -} - -/** Aligns an array of views Horizontally (on the X Axis) - - Example Usage: - ``` - align(horizontally: label,button,arrow) - ``` - - Ca also be used directly on horizontal layouts since they return the array of views : - ``` - align(horizontally: |-image1-image2-image3-|) - ``` - - - Returns: The array of views, enabling chaining, - - */ -@discardableResult -public func align(horizontally views: [UIView]) -> [UIView] { - align(.horizontal, views: views) - return views -} - -@available(*, deprecated, renamed: "align(horizontally:)") -@discardableResult -public func alignHorizontally(_ views: [UIView]) -> [UIView] { - align(.horizontal, views: views) - return views -} - -/** Aligns an array of views Vertically (on the Y Axis) - - Example Usage: - ``` - align(vertically: label,field,button) - ``` - - - Returns: The array of views, enabling chaining, - - */ -public func align(vertically views: UIView...) { - align(vertically: views) -} - -@available(*, deprecated, renamed: "align(vertically:)") -public func alignVertically(_ views: UIView...) { - align(vertically: views) -} - -/** Aligns an array of views Vertically (on the Y Axis) - - Example Usage: - ``` - align(vertically: label,field,button) - ``` - - - Returns: The array of views, enabling chaining, - - */ -public func align(vertically views: [UIView]) { - align(.vertical, views: views) -} - -@available(*, deprecated, renamed: "align(vertically:)") -public func alignVertically(_ views: [UIView]) { - align(.vertical, views: views) -} - -/** Aligns the center of two views - - Example Usage: - ``` - alignCenter(button, with:image) - ``` - */ -public func alignCenter(_ v1: UIView, with v2: UIView) { - alignHorizontally(v1, with: v2) - alignVertically(v1, with: v2) -} - -/** Aligns two views Horizontall (on the X Axis) - - Example Usage: - ``` - alignHorizontally(label, with:field) - ``` - - */ -public func alignHorizontally(_ v1: UIView, with v2: UIView, offset: Double = 0) { - align(.horizontal, v1: v1, with: v2, offset: offset) -} - -/** Aligns two views Vertically (on the Y Axis) - - Example Usage: - ``` - alignVertically(label, with:field) - ``` - - */ -public func alignVertically(_ v1: UIView, with v2: UIView, offset: Double = 0) { - align(.vertical, v1: v1, with: v2, offset: offset) -} - -private func align(_ axis: NSLayoutConstraint.Axis, views: [UIView]) { - for (i, v) in views.enumerated() where views.count > i+1 { - let v2 = views[i+1] - if axis == .horizontal { - alignHorizontally(v, with: v2) - } else { - alignVertically(v, with: v2) - } +extension UIView { + /** Aligns an array of views Horizontally (on the X Axis) + + Example Usage: + ``` + align(horizontally: label,button,arrow) + ``` + + Can also be used directly on horizontal layouts since they return the array of views : + ``` + align(horizontally: |-image1-image2-image3-|) + ``` + + - Returns: The array of views, enabling chaining, + + */ + @discardableResult + public func align(horizontally views: UIView...) -> [UIView] { + return align(horizontally: views) } -} - -func align(_ axis: NSLayoutConstraint.Axis, v1: UIView, with v2: UIView, offset: Double) { - if let spv = v1.superview { - let center: NSLayoutConstraint.Attribute = axis == .horizontal ? .centerY : .centerX - let c = constraint(item: v1, attribute: center, toItem: v2, constant: offset) - spv.addConstraint(c) + + @available(*, deprecated, renamed: "align(horizontally:)") + @discardableResult + public func alignHorizontally(_ views: UIView...) -> [UIView] { + return align(horizontally: views) } -} - -// MARK: Align sides - -/** Aligns tops of an array of views - - Example Usage: - ``` - align(tops: label,button,arrow) - ``` - - Ca also be used directly on horizontal layouts since they return the array of views : - ``` - align(tops: |-image1-image2-image3-|) - ``` - - - Returns: The array of views, enabling chaining, - - */ -@discardableResult -public func align(tops views: UIView...) -> [UIView] { - return align(tops: views) -} - -@available(*, deprecated, renamed: "align(tops:)") -@discardableResult -public func alignTops(_ views: UIView...) -> [UIView] { - return align(tops: views) -} - -/** Aligns tops of an array of views - - Example Usage: - ``` - align(tops: label,button,arrow) - ``` - - Ca also be used directly on horizontal layouts since they return the array of views : - ``` - align(tops: |-image1-image2-image3-|) - ``` - - - Returns: The array of views, enabling chaining, - - */ -@discardableResult -public func align(tops views: [UIView]) -> [UIView] { - align(.top, views: views) - return views -} - -@available(*, deprecated, renamed: "align(tops:)") -@discardableResult -public func alignTops(_ views: [UIView]) -> [UIView] { - align(.top, views: views) - return views -} - -/** Aligns bottoms of an array of views - - Example Usage: - ``` - align(bottoms: label,button,arrow) - ``` - - Ca also be used directly on horizontal layouts since they return the array of views : - ``` - align(bottoms: |-image1-image2-image3-|) - ``` - - - Returns: The array of views, enabling chaining, - - */ -@discardableResult -public func align(bottoms views: UIView...) -> [UIView] { - return align(bottoms: views) -} - -@available(*, deprecated, renamed: "align(bottoms:)") -@discardableResult -public func alignBottoms(_ views: UIView...) -> [UIView] { - return align(bottoms: views) -} - -/** Aligns bottoms of an array of views - - Example Usage: - ``` - align(bottoms: label,button,arrow) - ``` - - Ca also be used directly on horizontal layouts since they return the array of views : - ``` - align(bottoms: |-image1-image2-image3-|) - ``` - - - Returns: The array of views, enabling chaining, - - */ -@discardableResult -public func align(bottoms views: [UIView]) -> [UIView] { - align(.bottom, views: views) - return views -} - -@available(*, deprecated, renamed: "align(bottoms:)") -@discardableResult -public func alignBottoms(_ views: [UIView]) -> [UIView] { - align(.bottom, views: views) - return views -} - -/** Aligns left sides of an array of views - - Example Usage: - ``` - align(lefts: label,field,button) - ``` - - - Returns: The array of views, enabling chaining, - - */ -@discardableResult -public func align(lefts views: UIView...) -> [UIView] { - return align(lefts: views) -} - -@available(*, deprecated, renamed: "align(lefts:)") -@discardableResult -public func alignLefts(_ views: UIView...) -> [UIView] { - return align(lefts: views) -} - -/** Aligns left sides of an array of views - - Example Usage: - ``` - align(lefts: label,field,button) - ``` - - - Returns: The array of views, enabling chaining, - - */ -@discardableResult -public func align(lefts views: [UIView]) -> [UIView] { - align(.left, views: views) - return views -} - -@available(*, deprecated, renamed: "align(lefts:)") -@discardableResult -public func alignLefts(_ views: [UIView]) -> [UIView] { - align(.left, views: views) - return views -} - -/** Aligns right sides of an array of views - - Example Usage: - ``` - align(rights: label,field,button) - ``` - - - Returns: The array of views, enabling chaining, - - */ -@discardableResult -public func align(rights views: UIView...) -> [UIView] { - return align(rights: views) -} - -@available(*, deprecated, renamed: "align(rights:)") -@discardableResult -public func alignRights(_ views: UIView...) -> [UIView] { - return align(rights: views) -} - -/** Aligns right sides of an array of views - - Example Usage: - ``` - align(rights: label,field,button) - ``` - - - Returns: The array of views, enabling chaining, - - */ -@discardableResult -public func align(rights views: [UIView]) -> [UIView] { - align(.right, views: views) - return views -} - -@available(*, deprecated, renamed: "align(rights:)") -@discardableResult -public func alignRights(_ views: [UIView]) -> [UIView] { - align(.right, views: views) - return views -} - -/** Aligns leading sides of an array of views - -Example Usage: -``` -align(leadings: [label,field,button]) -``` - -- Returns: The array of views, enabling chaining, - -*/ -@discardableResult -public func align(leadings views: [UIView]) -> [UIView] { - align(.leading, views: views) - return views -} - -/** Aligns leading sides of an array of views - -Example Usage: -``` -align(leadings: label,field,button) -``` - -- Returns: The array of views, enabling chaining, - -*/ -@discardableResult -public func align(leadings views: UIView...) -> [UIView] { - align(.leading, views: views) - return views -} - -/** Aligns trailing sides of an array of views - -Example Usage: -``` -align(trailing: [label,field,button]) -``` - -- Returns: The array of views, enabling chaining, - -*/ -@discardableResult -public func align(trailings views: [UIView]) -> [UIView] { - align(.trailing, views: views) - return views -} - -/** Aligns trailing sides of an array of views - -Example Usage: -``` -align(trailing: label,field,button) -``` - -- Returns: The array of views, enabling chaining, - -*/ -@discardableResult -public func align(trailings views: UIView...) -> [UIView] { - align(.trailing, views: views) - return views -} - -@discardableResult -public func align(_ attribute: NSLayoutConstraint.Attribute, views: [UIView]) -> [UIView] { - for (i, v) in views.enumerated() where views.count > i+1 { - let v2 = views[i+1] - if let spv = v.superview { - let c = constraint(item: v, attribute: attribute, toItem: v2) + + /** Aligns an array of views Horizontally (on the X Axis) + + Example Usage: + ``` + align(horizontally: label,button,arrow) + ``` + + Ca also be used directly on horizontal layouts since they return the array of views : + ``` + align(horizontally: |-image1-image2-image3-|) + ``` + + - Returns: The array of views, enabling chaining, + + */ + @discardableResult + public func align(horizontally views: [UIView]) -> [UIView] { + align(.horizontal, views: views) + return views + } + + @available(*, deprecated, renamed: "align(horizontally:)") + @discardableResult + public func alignHorizontally(_ views: [UIView]) -> [UIView] { + align(.horizontal, views: views) + return views + } + + /** Aligns an array of views Vertically (on the Y Axis) + + Example Usage: + ``` + align(vertically: label,field,button) + ``` + + - Returns: The array of views, enabling chaining, + + */ + public func align(vertically views: UIView...) { + align(vertically: views) + } + + @available(*, deprecated, renamed: "align(vertically:)") + public func alignVertically(_ views: UIView...) { + align(vertically: views) + } + + /** Aligns an array of views Vertically (on the Y Axis) + + Example Usage: + ``` + align(vertically: label,field,button) + ``` + + - Returns: The array of views, enabling chaining, + + */ + public func align(vertically views: [UIView]) { + align(.vertical, views: views) + } + + @available(*, deprecated, renamed: "align(vertically:)") + public func alignVertically(_ views: [UIView]) { + align(.vertical, views: views) + } + + /** Aligns the center of two views + + Example Usage: + ``` + alignCenter(button, with:image) + ``` + */ + public func alignCenter(_ v1: UIView, with v2: UIView) { + alignHorizontally(v1, with: v2) + alignVertically(v1, with: v2) + } + + /** Aligns two views Horizontall (on the X Axis) + + Example Usage: + ``` + alignHorizontally(label, with:field) + ``` + + */ + public func alignHorizontally(_ v1: UIView, with v2: UIView, offset: Double = 0) { + align(.horizontal, v1: v1, with: v2, offset: offset) + } + + /** Aligns two views Vertically (on the Y Axis) + + Example Usage: + ``` + alignVertically(label, with:field) + ``` + + */ + public func alignVertically(_ v1: UIView, with v2: UIView, offset: Double = 0) { + align(.vertical, v1: v1, with: v2, offset: offset) + } + + private func align(_ axis: NSLayoutConstraint.Axis, views: [UIView]) { + for (i, v) in views.enumerated() where views.count > i+1 { + let v2 = views[i+1] + if axis == .horizontal { + alignHorizontally(v, with: v2) + } else { + alignVertically(v, with: v2) + } + } + } + + func align(_ axis: NSLayoutConstraint.Axis, v1: UIView, with v2: UIView, offset: Double) { + if let spv = v1.superview { + let center: NSLayoutConstraint.Attribute = axis == .horizontal ? .centerY : .centerX + let c = constraint(item: v1, attribute: center, toItem: v2, constant: offset) spv.addConstraint(c) } } - return views + + // MARK: Align sides + + /** Aligns tops of an array of views + + Example Usage: + ``` + align(tops: label,button,arrow) + ``` + + Ca also be used directly on horizontal layouts since they return the array of views : + ``` + align(tops: |-image1-image2-image3-|) + ``` + + - Returns: The array of views, enabling chaining, + + */ + @discardableResult + public func align(tops views: UIView...) -> [UIView] { + return align(tops: views) + } + + @available(*, deprecated, renamed: "align(tops:)") + @discardableResult + public func alignTops(_ views: UIView...) -> [UIView] { + return align(tops: views) + } + + /** Aligns tops of an array of views + + Example Usage: + ``` + align(tops: label,button,arrow) + ``` + + Ca also be used directly on horizontal layouts since they return the array of views : + ``` + align(tops: |-image1-image2-image3-|) + ``` + + - Returns: The array of views, enabling chaining, + + */ + @discardableResult + public func align(tops views: [UIView]) -> [UIView] { + align(.top, views: views) + return views + } + + @available(*, deprecated, renamed: "align(tops:)") + @discardableResult + public func alignTops(_ views: [UIView]) -> [UIView] { + align(.top, views: views) + return views + } + + /** Aligns bottoms of an array of views + + Example Usage: + ``` + align(bottoms: label,button,arrow) + ``` + + Ca also be used directly on horizontal layouts since they return the array of views : + ``` + align(bottoms: |-image1-image2-image3-|) + ``` + + - Returns: The array of views, enabling chaining, + + */ + @discardableResult + public func align(bottoms views: UIView...) -> [UIView] { + return align(bottoms: views) + } + + @available(*, deprecated, renamed: "align(bottoms:)") + @discardableResult + public func alignBottoms(_ views: UIView...) -> [UIView] { + return align(bottoms: views) + } + + /** Aligns bottoms of an array of views + + Example Usage: + ``` + align(bottoms: label,button,arrow) + ``` + + Ca also be used directly on horizontal layouts since they return the array of views : + ``` + align(bottoms: |-image1-image2-image3-|) + ``` + + - Returns: The array of views, enabling chaining, + + */ + @discardableResult + public func align(bottoms views: [UIView]) -> [UIView] { + align(.bottom, views: views) + return views + } + + @available(*, deprecated, renamed: "align(bottoms:)") + @discardableResult + public func alignBottoms(_ views: [UIView]) -> [UIView] { + align(.bottom, views: views) + return views + } + + /** Aligns left sides of an array of views + + Example Usage: + ``` + align(lefts: label,field,button) + ``` + + - Returns: The array of views, enabling chaining, + + */ + @discardableResult + public func align(lefts views: UIView...) -> [UIView] { + return align(lefts: views) + } + + @available(*, deprecated, renamed: "align(lefts:)") + @discardableResult + public func alignLefts(_ views: UIView...) -> [UIView] { + return align(lefts: views) + } + + /** Aligns left sides of an array of views + + Example Usage: + ``` + align(lefts: label,field,button) + ``` + + - Returns: The array of views, enabling chaining, + + */ + @discardableResult + public func align(lefts views: [UIView]) -> [UIView] { + align(.left, views: views) + return views + } + + @available(*, deprecated, renamed: "align(lefts:)") + @discardableResult + public func alignLefts(_ views: [UIView]) -> [UIView] { + align(.left, views: views) + return views + } + + /** Aligns right sides of an array of views + + Example Usage: + ``` + align(rights: label,field,button) + ``` + + - Returns: The array of views, enabling chaining, + + */ + @discardableResult + public func align(rights views: UIView...) -> [UIView] { + return align(rights: views) + } + + @available(*, deprecated, renamed: "align(rights:)") + @discardableResult + public func alignRights(_ views: UIView...) -> [UIView] { + return align(rights: views) + } + + /** Aligns right sides of an array of views + + Example Usage: + ``` + align(rights: label,field,button) + ``` + + - Returns: The array of views, enabling chaining, + + */ + @discardableResult + public func align(rights views: [UIView]) -> [UIView] { + align(.right, views: views) + return views + } + + @available(*, deprecated, renamed: "align(rights:)") + @discardableResult + public func alignRights(_ views: [UIView]) -> [UIView] { + align(.right, views: views) + return views + } + + /** Aligns leading sides of an array of views + + Example Usage: + ``` + align(leadings: [label,field,button]) + ``` + + - Returns: The array of views, enabling chaining, + + */ + @discardableResult + public func align(leadings views: [UIView]) -> [UIView] { + align(.leading, views: views) + return views + } + + /** Aligns leading sides of an array of views + + Example Usage: + ``` + align(leadings: label,field,button) + ``` + + - Returns: The array of views, enabling chaining, + + */ + @discardableResult + public func align(leadings views: UIView...) -> [UIView] { + align(.leading, views: views) + return views + } + + /** Aligns trailing sides of an array of views + + Example Usage: + ``` + align(trailing: [label,field,button]) + ``` + + - Returns: The array of views, enabling chaining, + + */ + @discardableResult + public func align(trailings views: [UIView]) -> [UIView] { + align(.trailing, views: views) + return views + } + + /** Aligns trailing sides of an array of views + + Example Usage: + ``` + align(trailing: label,field,button) + ``` + + - Returns: The array of views, enabling chaining, + + */ + @discardableResult + public func align(trailings views: UIView...) -> [UIView] { + align(.trailing, views: views) + return views + } + + @discardableResult + public func align(_ attribute: NSLayoutConstraint.Attribute, views: [UIView]) -> [UIView] { + for (i, v) in views.enumerated() where views.count > i+1 { + let v2 = views[i+1] + if let spv = v.superview { + let c = constraint(item: v, attribute: attribute, toItem: v2) + spv.addConstraint(c) + } + } + return views + } } #endif