commented code for extensions

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2023-07-25 14:16:08 -05:00
parent a266af0db2
commit e02d4ed83f
5 changed files with 192 additions and 3 deletions

View File

@ -9,6 +9,10 @@ import Foundation
import UIKit
extension NSAttributedString {
/// Spacer in the form of a AttributedString.
/// - Parameter width: size of the space.
/// - Returns: AttributedString with the Space of the width you asked.
public static func spacer(for width: CGFloat) -> NSAttributedString {
let spacerImage = UIImage()
let spacerAttachment = NSTextAttachment()
@ -20,7 +24,18 @@ extension NSAttributedString {
extension NSMutableAttributedString {
/// This will used along with mutableText method to determine if you will use a ratio version of the lineHeight within a TextStyle if you have a scaled font being used.
public static var useScaledLineHeight: Bool = false
/// Creates a MutableAttributedString for the text and other properties you have passed into the method.
/// - Parameters:
/// - text: Text you want to draw onscreen.
/// - textStyle: Style of Typography you want to render the Text.
/// - useScaledFont: Determines if you used a scaledFont or a normal Font from the TextStyle.
/// - textColor: Color of Text to render.
/// - alignment: Text alignment.
/// - lineBreakMode: LineBreak Mode.
/// - Returns: MutableAttributedString from al lof the properties you passed in to help render Text using non-standard properties.
public static func mutableText(for text: String, textStyle: TextStyle, useScaledFont: Bool = true, textColor: UIColor, alignment: NSTextAlignment = .left, lineBreakMode: NSLineBreakMode) -> NSMutableAttributedString {
//var for if you can scale or not

View File

@ -125,6 +125,8 @@ extension UIColor {
return true
}
/// Convert your UIColor to a VDSColor
/// - Returns: VDSColor enum that matches the hexString for this UIColor
public func toVDSColor() -> VDSColor? {
guard let hex = hexString else { return nil }
let found = VDSColor.allCases.first{ $0.uiColor.hexString == hex }

View File

@ -9,6 +9,10 @@ import Foundation
import UIKit
extension UIControl.State {
/// State for Error
public static var error = UIControl.State(rawValue: 1 << 16)
/// State for Success
public static var success = UIControl.State(rawValue: 1 << 17)
}

View File

@ -72,6 +72,9 @@ extension UIView {
// MARK: - CALayer
//--------------------------------------------------
extension CALayer {
/// Removes a sublayer of a specific name.
/// - Parameter layerName: Layer with the name you want to remove.
public func remove(layerName: String) {
guard let sublayers = sublayers else {
return
@ -90,6 +93,12 @@ extension CALayer {
//--------------------------------------------------
extension UIView {
/// Adds a border to a specific sides with the parameters.
/// - Parameters:
/// - side: Side in which to add the border.
/// - width: Width of the border.
/// - color: Color of the border.
/// - offset: offset of the border.
public func addBorder(side: UIRectEdge, width: CGFloat, color: UIColor, offset: CGFloat = 0) {
let layerName = borderLayerName(for: side)
layer.remove(layerName: layerName)
@ -114,6 +123,7 @@ extension UIView {
layer.addSublayer(borderLayer)
}
/// Removes all of the borders addeding using the addBorder method.
public func removeBorders() {
layer.borderWidth = 0
layer.borderColor = nil

View File

@ -21,6 +21,11 @@ extension UIView {
//--------------------------------------------------
extension UIView {
@discardableResult
/// Pins each to the all 4 anchor points to a view.
/// - Parameters:
/// - view: View that you will be pinned within.
/// - edges: Insets for each side.
/// - Returns: Yourself.
public func pin(_ view: UIView, with edges: UIEdgeInsets = UIEdgeInsets.zero) -> Self {
pinLeading(view.leadingAnchor, edges.left)
pinTrailing(view.trailingAnchor, edges.right)
@ -30,6 +35,10 @@ extension UIView {
}
@discardableResult
/// Pins each to the all 4 anchor points to the view you are set within.
/// - Parameter edges: Insets for each side.
/// - Returns: Yourself.
public func pinToSuperView(_ edges: UIEdgeInsets = UIEdgeInsets.zero) -> Self {
if let superview {
pin(superview, with: edges)
@ -44,34 +53,52 @@ extension UIView {
extension UIView {
@discardableResult
/// Adds a heightAnchor.
/// - Parameter constant: Constant size.
/// - Returns: Yourself.
public func height(_ constant: CGFloat) -> Self {
height(constant: constant)
return self
}
@discardableResult
/// Adds a heightAnchor where the height constant passed in using a greaterThanOrEqualTo Constraint.
/// - Parameter constant: Constant size.
/// - Returns: Yourself.
public func heightGreaterThanEqualTo(_ constant: CGFloat) -> Self {
heightGreaterThanEqualTo(constant: constant)
return self
}
@discardableResult
/// Adds a heightAnchor where the height constant passed in using a lessThanOrEqualTo Constraint.
/// - Parameter constant: Constant size.
/// - Returns: Yourself.
public func heightLessThanEqualTo(_ constant: CGFloat) -> Self {
heightLessThanEqualTo(constant: constant)
return self
}
@discardableResult
/// Adds a heightAnchor for the constant passed into the method.
/// - Parameter constant: Constant size.
/// - Returns: The Constraint that was created.
public func height(constant: CGFloat) -> NSLayoutConstraint {
heightAnchor.constraint(equalToConstant: constant).activate()
}
@discardableResult
/// Adds a heightAnchor where the constant passed in using a greaterThanOrEqualTo Constraint.
/// - Parameter constant: Constant size.
/// - Returns: The Constraint that was created.
public func heightGreaterThanEqualTo(constant: CGFloat) -> NSLayoutConstraint {
heightAnchor.constraint(greaterThanOrEqualToConstant: constant).activate()
}
@discardableResult
/// Adds a heightAnchor where the constant passed in using a lessThanOrEqualTo Constraint.
/// - Parameter constant: Constant size.
/// - Returns: The Constraint that was created.
public func heightLessThanEqualTo(constant: CGFloat) -> NSLayoutConstraint {
heightAnchor.constraint(lessThanOrEqualToConstant: constant).activate()
}
@ -84,34 +111,52 @@ extension UIView {
extension UIView {
@discardableResult
/// Adds a widthAnchor.
/// - Parameter constant: Width Constant size.
/// - Returns: Yourself.
public func width(_ constant: CGFloat) -> Self {
width(constant: constant)
return self
}
@discardableResult
/// Adds a widthAnchor where the constant passed in using a greaterThanOrEqualTo Constraint.
/// - Parameter constant: Constant size.
/// - Returns: Yourself.
public func widthGreaterThanEqualTo(_ constant: CGFloat) -> Self {
widthGreaterThanEqualTo(constant: constant)
return self
}
@discardableResult
/// Adds a widthAnchor where the constant passed in using a lessThanOrEqualTo Constraint.
/// - Parameter constant: Constant size.
/// - Returns: Yourself.
public func widthLessThanEqualTo(_ constant: CGFloat) -> Self {
widthLessThanEqualTo(constant: constant)
return self
}
@discardableResult
/// Adds a widthAnchor for the constant passed into the method.
/// - Parameter constant: Constant size.
/// - Returns: The Constraint that was created.
public func width(constant: CGFloat) -> NSLayoutConstraint {
widthAnchor.constraint(equalToConstant: constant).activate()
}
@discardableResult
/// Adds a widthAnchor with the constant passed in using a greaterThanOrEqualTo Constraint.
/// - Parameter constant: Constant size.
/// - Returns: The Constraint that was created.
public func widthGreaterThanEqualTo(constant: CGFloat) -> NSLayoutConstraint {
widthAnchor.constraint(greaterThanOrEqualToConstant: constant).activate()
}
@discardableResult
/// Adds a widthAnchor with the constant passed in using a lessThanOrEqualTo Constraint.
/// - Parameter constant: Constant size.
/// - Returns: The Constraint that was created.
public func widthLessThanEqualTo(constant: CGFloat) -> NSLayoutConstraint {
widthAnchor.constraint(lessThanOrEqualToConstant: constant).activate()
}
@ -123,29 +168,48 @@ extension UIView {
extension UIView {
@discardableResult
/// Adds a topAnchor.
/// - Parameter constant: Constant size.
/// - Returns: Yourself.
public func pinTop(_ constant: CGFloat = 0.0) -> Self {
return pinTop(nil, constant)
}
@discardableResult
/// Adds a topAnchor to a specific YAxisAnchor.
/// - Parameter anchor:The anchor in which to attach the topAnchor
/// - constant: Constant size.
/// - Returns: Yourself.
public func pinTop(_ anchor: NSLayoutYAxisAnchor? = nil, _ constant: CGFloat = 0.0) -> Self {
pinTop(anchor: anchor, constant: constant)
return self
}
@discardableResult
/// Adds a topAnchor to a specific YAxisAnchor passed in using a lessThanOrEqualTo Constraint
/// - Parameter anchor:The anchor in which to attach the topAnchor
/// - constant: Constant size.
/// - Returns: Yourself.
public func pinTopLessThanOrEqualTo(_ anchor: NSLayoutYAxisAnchor? = nil, _ constant: CGFloat = 0.0) -> Self {
pinBottomLessThanOrEqualTo(anchor: anchor, constant: constant)
pinTopLessThanOrEqualTo(anchor: anchor, constant: constant)
return self
}
@discardableResult
/// Adds a topAnchor to a specific YAxisAnchor passed in using a greaterThanOrEqualTo Constraint
/// - Parameter anchor:The anchor in which to attach the topAnchor
/// - constant: Constant size.
/// - Returns: Yourself.
public func pinTopGreaterThanOrEqualTo(_ anchor: NSLayoutYAxisAnchor? = nil, _ constant: CGFloat = 0.0) -> Self {
pinTopGreaterThanOrEqualTo(anchor: anchor, constant: constant)
return self
}
@discardableResult
/// Adds a topAnchor for the constant passed into the method.
/// - Parameter anchor:The anchor in which to attach the topAnchor
/// - constant: Constant size.
/// - Returns: The Constraint that was created.
public func pinTop(anchor: NSLayoutYAxisAnchor?, constant: CGFloat = 0.0) -> NSLayoutConstraint? {
let found: NSLayoutYAxisAnchor? = anchor ?? superview?.topAnchor
guard let found else { return nil }
@ -153,6 +217,10 @@ extension UIView {
}
@discardableResult
/// Adds a topAnchor with the constant passed in using a lessThanOrEqualTo Constraint.
/// - Parameter anchor:The anchor in which to attach the topAnchor
/// - constant: Constant size.
/// - Returns: The Constraint that was created.
public func pinTopLessThanOrEqualTo(anchor: NSLayoutYAxisAnchor?, constant: CGFloat = 0.0) -> NSLayoutConstraint? {
let found: NSLayoutYAxisAnchor? = anchor ?? superview?.topAnchor
guard let found else { return nil }
@ -160,6 +228,10 @@ extension UIView {
}
@discardableResult
/// Adds a topAnchor with the constant passed in using a greaterThanOrEqualTo Constraint.
/// - Parameter anchor:The anchor in which to attach the topAnchor
/// - constant: Constant size.
/// - Returns: The Constraint that was created.
public func pinTopGreaterThanOrEqualTo(anchor: NSLayoutYAxisAnchor?, constant: CGFloat = 0.0) -> NSLayoutConstraint? {
let found: NSLayoutYAxisAnchor? = anchor ?? superview?.topAnchor
guard let found else { return nil }
@ -173,29 +245,48 @@ extension UIView {
//--------------------------------------------------
extension UIView {
@discardableResult
/// Adds a bottomAnchor.
/// - Parameter constant: Constant size.
/// - Returns: Yourself.
public func pinBottom(_ constant: CGFloat = 0.0) -> Self {
return pinBottom(nil, constant)
}
@discardableResult
/// Adds a bottomAnchor to a specific YAxisAnchor.
/// - Parameter anchor:The anchor in which to attach the bottomAnchor
/// - constant: Constant size.
/// - Returns: Yourself.
public func pinBottom(_ anchor: NSLayoutYAxisAnchor? = nil, _ constant: CGFloat = 0.0) -> Self {
pinBottom(anchor: anchor, constant: constant)
return self
}
@discardableResult
/// Adds a bottomAnchor to a specific YAxisAnchor passed in using a lessThanOrEqualTo Constraint
/// - Parameter anchor:The anchor in which to attach the bottomAnchor
/// - constant: Constant size.
/// - Returns: Yourself.
public func pinBottomLessThanOrEqualTo(_ anchor: NSLayoutYAxisAnchor? = nil, _ constant: CGFloat = 0.0) -> Self {
pinBottomLessThanOrEqualTo(anchor: anchor, constant: constant)
return self
}
@discardableResult
/// Adds a bottomAnchor to a specific YAxisAnchor passed in using a greaterThanOrEqualTo Constraint
/// - Parameter anchor:The anchor in which to attach the bottomAnchor
/// - constant: Constant size.
/// - Returns: Yourself.
public func pinBottomGreaterThanOrEqualTo(_ anchor: NSLayoutYAxisAnchor? = nil, _ constant: CGFloat = 0.0) -> Self {
pinBottomGreaterThanOrEqualTo(anchor: anchor, constant: constant)
return self
}
@discardableResult
/// Adds a bottomAnchor for the constant passed into the method.
/// - Parameter anchor:The anchor in which to attach the bottomAnchor
/// - constant: Constant size.
/// - Returns: The Constraint that was created.
public func pinBottom(anchor: NSLayoutYAxisAnchor?, constant: CGFloat = 0.0) -> NSLayoutConstraint? {
let found: NSLayoutYAxisAnchor? = anchor ?? superview?.bottomAnchor
guard let found else { return nil }
@ -203,6 +294,10 @@ extension UIView {
}
@discardableResult
/// Adds a bottomAnchor with the constant passed in using a lessThanOrEqualTo Constraint.
/// - Parameter anchor:The anchor in which to attach the bottomAnchor
/// - constant: Constant size.
/// - Returns: The Constraint that was created.
public func pinBottomLessThanOrEqualTo(anchor: NSLayoutYAxisAnchor?, constant: CGFloat = 0.0) -> NSLayoutConstraint? {
let found: NSLayoutYAxisAnchor? = anchor ?? superview?.bottomAnchor
guard let found else { return nil }
@ -210,6 +305,10 @@ extension UIView {
}
@discardableResult
/// Adds a bottomAnchor with the constant passed in using a greaterThanOrEqualTo Constraint.
/// - Parameter anchor:The anchor in which to attach the bottomAnchor
/// - constant: Constant size.
/// - Returns: The Constraint that was created.
public func pinBottomGreaterThanOrEqualTo(anchor: NSLayoutYAxisAnchor?, constant: CGFloat = 0.0) -> NSLayoutConstraint? {
let found: NSLayoutYAxisAnchor? = anchor ?? superview?.bottomAnchor
guard let found else { return nil }
@ -223,29 +322,48 @@ extension UIView {
extension UIView {
@discardableResult
/// Adds a leadingAnchor.
/// - Parameter constant: Constant size.
/// - Returns: Yourself.
public func pinLeading(_ constant: CGFloat = 0.0) -> Self {
return pinLeading(nil, constant)
}
@discardableResult
/// Adds a leadingAnchor to a specific XAxisAnchor.
/// - Parameter anchor:The anchor in which to attach the leadingAnchor.
/// - constant: Constant size.
/// - Returns: Yourself.
public func pinLeading(_ anchor: NSLayoutXAxisAnchor? = nil, _ constant: CGFloat = 0.0) -> Self {
pinLeading(anchor: anchor, constant: constant)
return self
}
@discardableResult
/// Adds a leadingAnchor to a specific XAxisAnchor passed in using a greaterThanOrEqualTo Constraint
/// - Parameter anchor:The anchor in which to attach the leadingAnchor
/// - constant: Constant size.
/// - Returns: Yourself.
public func pinLeadingLessThanOrEqualTo(_ anchor: NSLayoutXAxisAnchor? = nil, _ constant: CGFloat = 0.0) -> Self {
pinLeadingLessThanOrEqualTo(anchor: anchor, constant: constant)
return self
}
@discardableResult
/// Adds a leadingAnchor to a specific XAxisAnchor passed in using a greaterThanOrEqualTo Constraint
/// - Parameter anchor:The anchor in which to attach the leadingAnchor
/// - constant: Constant size.
/// - Returns: Yourself.
public func pinLeadingGreaterThanOrEqualTo(_ anchor: NSLayoutXAxisAnchor? = nil, _ constant: CGFloat = 0.0) -> Self {
pinLeadingGreaterThanOrEqualTo(anchor: anchor, constant: constant)
return self
}
@discardableResult
/// Adds a leadingAnchor for the constant passed into the method.
/// - Parameter anchor:The anchor in which to attach the leadingAnchor
/// - constant: Constant size.
/// - Returns: The Constraint that was created.
public func pinLeading(anchor: NSLayoutXAxisAnchor?, constant: CGFloat = 0.0) -> NSLayoutConstraint? {
let found: NSLayoutXAxisAnchor? = anchor ?? superview?.leadingAnchor
guard let found else { return nil }
@ -253,6 +371,10 @@ extension UIView {
}
@discardableResult
/// Adds a leadingAnchor with the constant passed in using a lessThanOrEqualTo Constraint.
/// - Parameter anchor:The anchor in which to attach the leadingAnchor
/// - constant: Constant size.
/// - Returns: The Constraint that was created.
public func pinLeadingLessThanOrEqualTo(anchor: NSLayoutXAxisAnchor?, constant: CGFloat = 0.0) -> NSLayoutConstraint? {
let found: NSLayoutXAxisAnchor? = anchor ?? superview?.leadingAnchor
guard let found else { return nil }
@ -260,6 +382,10 @@ extension UIView {
}
@discardableResult
/// Adds a leadingAnchor with the constant passed in using a greaterThanOrEqualTo Constraint.
/// - Parameter anchor:The anchor in which to attach the leadingAnchor
/// - constant: Constant size.
/// - Returns: The Constraint that was created.
public func pinLeadingGreaterThanOrEqualTo(anchor: NSLayoutXAxisAnchor?, constant: CGFloat = 0.0) -> NSLayoutConstraint? {
let found: NSLayoutXAxisAnchor? = anchor ?? superview?.leadingAnchor
guard let found else { return nil }
@ -273,29 +399,48 @@ extension UIView {
extension UIView {
@discardableResult
/// Adds a trailingAnchor.
/// - Parameter constant: Constant size.
/// - Returns: Yourself.
public func pinTrailing(_ constant: CGFloat = 0.0) -> Self {
pinTrailing(nil, constant)
}
@discardableResult
/// Adds a trailingAnchor to a specific XAxisAnchor.
/// - Parameter anchor:The anchor in which to attach the trailingAnchor.
/// - constant: Constant size.
/// - Returns: Yourself.
public func pinTrailing(_ anchor: NSLayoutXAxisAnchor? = nil, _ constant: CGFloat = 0.0) -> Self {
pinTrailing(anchor: anchor, constant: constant)
return self
}
@discardableResult
/// Adds a trailingAnchor to a specific XAxisAnchor passed in using a lessThanOrEqualTo Constraint
/// - Parameter anchor:The anchor in which to attach the trailingAnchor
/// - constant: Constant size.
/// - Returns: Yourself.
public func pinTrailingLessThanOrEqualTo(_ anchor: NSLayoutXAxisAnchor? = nil, _ constant: CGFloat = 0.0) -> Self {
pinTrailingLessThanOrEqualTo(anchor: anchor, constant: constant)
return self
}
@discardableResult
/// Adds a trailingAnchor to a specific XAxisAnchor passed in using a greaterThanOrEqualTo Constraint
/// - Parameter anchor:The anchor in which to attach the trailingAnchor
/// - constant: Constant size.
/// - Returns: Yourself.
public func pinTrailingGreaterThanOrEqualTo(_ anchor: NSLayoutXAxisAnchor? = nil, _ constant: CGFloat = 0.0) -> Self {
pinTrailingGreaterThanOrEqualTo(anchor: anchor, constant: constant)
return self
}
@discardableResult
/// Adds a trailingAnchor for the constant passed into the method.
/// - Parameter anchor:The anchor in which to attach the trailingAnchor
/// - constant: Constant size.
/// - Returns: The Constraint that was created.
public func pinTrailing(anchor: NSLayoutXAxisAnchor?, constant: CGFloat = 0.0) -> NSLayoutConstraint? {
let found: NSLayoutXAxisAnchor? = anchor ?? superview?.trailingAnchor
guard let found else { return nil }
@ -303,6 +448,10 @@ extension UIView {
}
@discardableResult
/// Adds a trailingAnchor with the constant passed in using a lessThanOrEqualTo Constraint.
/// - Parameter anchor:The anchor in which to attach the trailingAnchor
/// - constant: Constant size.
/// - Returns: The Constraint that was created.
public func pinTrailingLessThanOrEqualTo(anchor: NSLayoutXAxisAnchor?, constant: CGFloat = 0.0) -> NSLayoutConstraint? {
let found: NSLayoutXAxisAnchor? = anchor ?? superview?.trailingAnchor
guard let found else { return nil }
@ -310,6 +459,10 @@ extension UIView {
}
@discardableResult
/// Adds a trailingAnchor with the constant passed in using a greaterThanOrEqualTo Constraint.
/// - Parameter anchor:The anchor in which to attach the trailingAnchor
/// - constant: Constant size.
/// - Returns: The Constraint that was created.
public func pinTrailingGreaterThanOrEqualTo(anchor: NSLayoutXAxisAnchor?, constant: CGFloat = 0.0) -> NSLayoutConstraint? {
let found: NSLayoutXAxisAnchor? = anchor ?? superview?.trailingAnchor
guard let found else { return nil }
@ -320,17 +473,22 @@ extension UIView {
extension NSLayoutConstraint {
@discardableResult
/// Activates yourself
/// - Returns: Self
public func activate() -> Self{
isActive = true
return self
}
@discardableResult
/// Deactivates yourself
/// - Returns: Self
public func deactivate() -> Self{
isActive = false
return self
}
/// Helper class that holds onto all types of Contraints.
public class Container {
public var topConstraint: NSLayoutConstraint?
public var leadingConstraint: NSLayoutConstraint?