Created
January 4, 2021 16:13
-
-
Save exorcyze/e5c5d4be1056d0b9434d595581b8bb00 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// UIView+Style.swift | |
// Created / Copyright © : Mike Johnson, 2020 | |
// | |
import Foundation | |
import UIKit | |
// MARK: - App Size Values | |
public struct AppSizing { | |
static let margin: CGFloat = 20 | |
static let cornerRadius: CGFloat = 8 | |
} | |
// MARK: - App Font Values | |
extension UIFont { | |
static let AppBody : UIFont = UIFont.systemFont( ofSize: 24, weight: UIFont.Weight.regular ) | |
} | |
// MARK: - App Color Values | |
public struct AppColor { | |
private struct Palette { | |
static let primaryBlue = UIColor.blue | |
static let bodyText = UIColor.black | |
static let titleText = UIColor.darkGray | |
} | |
static let primaryText = Palette.bodyText | |
static let secondaryText = Palette.primaryBlue | |
static let titleText = Palette.titleText | |
} | |
// MARK: - Unused? | |
protocol Viewable {} | |
extension Viewable where Self: UIView { | |
/// MARK: - Convenience Initializers | |
init( background: UIColor, height: CGFloat ) { | |
self.init() | |
self.backgroundColor = background | |
self.height = height | |
} | |
init( background: UIColor, frame: CGRect ) { | |
self.init() | |
self.backgroundColor = background | |
self.frame = frame | |
} | |
init( background: UIColor, size : CGSize = CGSize( width: 200, height: 200 ) ) { | |
self.init( background: background, frame: CGRect( x: 0, y: 0, width: size.width, height: size.height ) ) | |
} | |
func debugStyle() { | |
self.backgroundColor = .secondarySystemFill | |
} | |
} | |
extension UIView: Viewable {} | |
// MARK: - UIView Extensions | |
extension UIView { | |
enum ViewStyle { | |
case normal | |
case rounded | |
} | |
convenience init(style: ViewStyle ) { | |
self.init() | |
self.style( style ) | |
} | |
@discardableResult func style(_ style: ViewStyle) -> UIView { | |
switch style { | |
case .normal: | |
self.backgroundColor = .white | |
case .rounded: | |
self.layer.cornerRadius = 8 | |
self.layer.borderWidth = 0 | |
} | |
return self | |
} | |
@discardableResult func rounded( cornerRadius: CGFloat = AppSizing.cornerRadius ) -> UIView { | |
self.layer.cornerRadius = 8 | |
return self | |
} | |
@discardableResult func bordered( color: UIColor, width: CGFloat = 1 ) -> UIView { | |
self.layer.borderColor = color.cgColor | |
self.layer.borderWidth = width | |
return self | |
} | |
} | |
// MARK: - UILabel Extensions | |
extension UILabel { | |
enum Style { | |
case body | |
case title | |
} | |
convenience init(style: Style) { | |
self.init() | |
self.style( style ) | |
} | |
@discardableResult func style(_ style: Style) -> UILabel { | |
switch style { | |
case .body: | |
self.font = UIFont.systemFont(ofSize: 14, weight: .regular) | |
case .title: | |
self.font = UIFont.systemFont(ofSize: 24, weight: .bold) | |
self.textColor = AppColor.titleText | |
self.textAlignment = .center | |
} | |
//self.setContentCompressionResistancePriority(.required, for: .vertical) | |
// set defaults for sizing | |
self.width = superview?.width ?? 300 | |
self.height = font.pointSize * 1.3 | |
return self | |
} | |
} | |
// MARK: - UIButton Extensions | |
extension UIButton { | |
enum Style { | |
case standard | |
} | |
convenience init(style: Style) { | |
self.init() | |
self.style( style ) | |
} | |
@discardableResult func style(_ style: Style) -> UIButton { | |
self.layer.cornerRadius = AppSizing.cornerRadius | |
self.titleLabel?.font = UIFont.systemFont(ofSize: 18, weight: .semibold) | |
self.setTitleColor( .white, for: .normal ) | |
self.backgroundColor = .darkGray | |
self.size = CGSize( width: 200, height: 44 ) | |
return self | |
} | |
} | |
// MARK: - UIImageView Extensions | |
extension UIImageView { | |
enum Style { | |
case fill | |
case fit | |
} | |
convenience init( style: Style ) { | |
self.init() | |
self.clipsToBounds = true | |
if style == .fill { | |
self.contentMode = UIView.ContentMode.scaleAspectFill | |
} else { | |
self.contentMode = UIView.ContentMode.scaleAspectFit | |
} | |
backgroundColor = UIColor.secondarySystemFill | |
} | |
convenience init( imageName: String ) { | |
if let img = UIImage( named: imageName ) { | |
self.init( image: img ) | |
self.size = img.size | |
self.contentMode = UIView.ContentMode.scaleAspectFill | |
} | |
else { | |
self.init() | |
} | |
} | |
} | |
// MARK: - UICollectionView Extensions | |
extension UICollectionView { | |
enum Style { | |
case horizontal | |
case vertical | |
} | |
convenience init( style: Style ) { | |
let mylayout = UICollectionViewFlowLayout() | |
//mylayout.itemSize = itemSize | |
mylayout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0) | |
mylayout.minimumLineSpacing = 0 | |
mylayout.minimumInteritemSpacing = 0 | |
mylayout.scrollDirection = ( style == .horizontal ) ? .horizontal : .vertical | |
self.init( frame: CGRect.zero, collectionViewLayout: mylayout ) | |
self.alwaysBounceVertical = ( style == .vertical ) | |
self.alwaysBounceHorizontal = ( style == .horizontal ) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment