Skip to content

Instantly share code, notes, and snippets.

@vzqwer
Created March 11, 2022 20:16
Show Gist options
  • Save vzqwer/0e408d55a1600eabcacc472c41cece10 to your computer and use it in GitHub Desktop.
Save vzqwer/0e408d55a1600eabcacc472c41cece10 to your computer and use it in GitHub Desktop.
Factory for simplifying UIKit views creation from code.
import UIKit
public struct ViewFactory {
public static var labelColor: UIColor = .black
public static var secondaryLabelColor: UIColor = .gray
public static var labelFont: UIFont = .systemFont(ofSize: 15, weight: .medium)
public static var fieldFont: UIFont = .systemFont(ofSize: 15, weight: .semibold)
public static func view(
_ backgroundColor: UIColor? = UIColor.clear,
corners: CACornerMask? = nil,
radius: CGFloat = 0
) -> UIView {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
view.layer.cornerRadius = radius
corners.flatMap { view.layer.maskedCorners = $0 }
backgroundColor.flatMap { view.backgroundColor = $0 }
return view
}
public static func label(
_ color: UIColor? = labelColor,
font: UIFont? = labelFont,
adjustsFontSizeToFitWidth: Bool = true,
align: NSTextAlignment = .left,
lines: Int = 0,
breakMode: NSLineBreakMode = .byWordWrapping,
minimumScaleMultiplier: CGFloat? = 0.8,
text: String = ""
) -> UILabel {
let view = UILabel()
view.text = text
view.translatesAutoresizingMaskIntoConstraints = false
font.flatMap { view.font = $0 }
color.flatMap { view.textColor = $0 }
view.numberOfLines = 0
view.lineBreakMode = breakMode
view.textAlignment = align
view.numberOfLines = lines
view.adjustsFontSizeToFitWidth = adjustsFontSizeToFitWidth
if let scale = minimumScaleMultiplier {
view.allowsDefaultTighteningForTruncation = true
view.minimumScaleFactor = scale
}
return view
}
public static func textField(
hint: String = "",
textColor: UIColor? = labelColor,
font: UIFont? = fieldFont,
hintColor: UIColor? = secondaryLabelColor
) -> UITextField {
let view = UITextField()
view.translatesAutoresizingMaskIntoConstraints = false
view.font = font
textColor.flatMap { view.textColor = $0 }
hintColor.flatMap {
view.attributedPlaceholder = NSAttributedString(
string: hint,
attributes: [.foregroundColor: $0]
)
}
return view
}
public static func scroll(delegate: UIScrollViewDelegate? = nil) -> UIScrollView {
let view = UIScrollView()
view.translatesAutoresizingMaskIntoConstraints = false
view.delegate = delegate
return view
}
public static func table() -> UITableView {
let view = UITableView()
view.translatesAutoresizingMaskIntoConstraints = false
return view
}
public static func stack(
_ spacing: CGFloat = 0,
margins: NSDirectionalEdgeInsets = .zero,
axis: NSLayoutConstraint.Axis = .vertical,
align: UIStackView.Alignment = .fill,
distrib: UIStackView.Distribution = .fill,
subviews: [UIView] = []
) -> UIStackView {
let view = UIStackView(arrangedSubviews: subviews)
view.translatesAutoresizingMaskIntoConstraints = false
view.spacing = spacing
view.distribution = distrib
view.alignment = align
view.axis = axis
view.isLayoutMarginsRelativeArrangement = true
view.directionalLayoutMargins = margins
return view
}
public static func image(
_ imageName: String? = nil,
image: UIImage? = nil,
tint: UIColor? = nil,
contentMode: UIView.ContentMode = .scaleAspectFit
) -> UIImageView {
let view = UIImageView()
view.translatesAutoresizingMaskIntoConstraints = false
view.contentMode = contentMode
imageName.flatMap { view.image = UIImage(named: $0) }
image.flatMap { view.image = $0 }
tint.flatMap { view.tintColor = $0 }
return view
}
}
public extension CACornerMask {
static let top: CACornerMask = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
static let bottom: CACornerMask = [.layerMinXMaxYCorner, .layerMaxXMaxYCorner]
static let all: CACornerMask = top.union(bottom)
static let none: CACornerMask = []
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment