Skip to content

Instantly share code, notes, and snippets.

@foxicode
foxicode / HeaderStyle.swift
Created February 5, 2023 18:53
PropertyWrapper styles
import UIKit
@propertyWrapper
struct HeaderStyle {
var wrappedValue: UILabel
init(_ label: UILabel = .init()) {
label.numberOfLines = 0
label.font = UIFont(name: "Helvetica Neue", size: 32)
label.textColor = UIColor(red: 0.16, green: 0.16, blue: 0.16, alpha: 1.00)
@foxicode
foxicode / SnapViewController.swift
Created February 5, 2023 18:16
ViewController using SnapKit extensions
import SnapKit
import UIKit
class ViewControllerLayout: Layout {
lazy var square = UIView(backgroundColor: .red)
lazy var circle = UIView(
backgroundColor: .blue.withAlphaComponent(0.9),
cornerRadius: 50
)
@foxicode
foxicode / Layout.swift
Last active February 5, 2023 18:14
Layout classes
import UIKit
class Layout: UIView {
required init() {
super.init(frame: .zero)
backgroundColor = .white
layer.masksToBounds = true
}
@foxicode
foxicode / UIView+init.swift
Last active February 7, 2023 19:08
UIView init extensions
import UIKit
extension UIView {
convenience init(backgroundColor: UIColor) {
self.init()
self.backgroundColor = backgroundColor
}
convenience init(backgroundColor: UIColor, cornerRadius: CGFloat) {
import SnapKit
import UIKit
extension ConstraintMaker {
func square(view: UIView) {
width.equalTo(view.snp.height)
}
func square(size: CGFloat) {
width.equalTo(size)
@foxicode
foxicode / ConstraintMaker+fill.swift
Created February 5, 2023 17:51
ConstraintMaker fill extension
import SnapKit
import UIKit
extension ConstraintMaker {
func fillHorizontally(margin: CGFloat = 0) {
leading.equalToSuperview().offset(margin)
trailing.equalToSuperview().offset(-margin)
}
func fillVertically(margin: CGFloat = 0) {
@foxicode
foxicode / UIView+addSubviewWithConstraints.swift
Created February 5, 2023 17:46
Add subview with constraints
import SnapKit
import UIKit
extension UIView {
func addSubview(_ view: UIView, closure: (ConstraintMaker) -> Void) {
addSubview(view)
view.snp.makeConstraints(closure)
}
}
@foxicode
foxicode / SnapKitSample.swift
Created February 5, 2023 17:35
SnapKit Sample
import SnapKit
import UIKit
class ViewControllerLayout: UIView {
lazy var square: UIView = {
let view = UIView()
view.backgroundColor = .red
return view
}()
@foxicode
foxicode / ProtocolExtensions.swift
Created July 17, 2022 17:11
Protocol extensions in Swift
enum Access {
case create
case read
case write
case delete
}
protocol PFile {
func open(access: Access)
func close()
@foxicode
foxicode / BoolConversions.swift
Created October 15, 2020 20:17
Swift Bool conversions
import Foundation
let b1 = Bool("1") // nil
let b0 = Bool("0") // nil
let bYes = Bool("YES") // nil
let bNo = Bool("NO") // nil
let bTrue = Bool("true") // true
let bFalse = Bool("false") // false
print(true) // "true\n"