Skip to content

Instantly share code, notes, and snippets.

View imjhk03's full-sized avatar
🪴
Gardening

Joohee Kim imjhk03

🪴
Gardening
View GitHub Profile
@imjhk03
imjhk03 / ScreenSize and DeviceTypes of iPhone
Last active December 13, 2020 14:44
Get screen size or device types of iPhone
enum ScreenSize {
static let width = UIScreen.main.bounds.size.width
static let height = UIScreen.main.bounds.size.height
static let maxLength = max(ScreenSize.width, ScreenSize.height)
static let minLength = min(ScreenSize.width, ScreenSize.height)
}
enum DeviceTypes {
static let idiom = UIDevice.current.userInterfaceIdiom
@imjhk03
imjhk03 / UIView+Layout.swift
Created December 13, 2020 15:57
Layout(anchor) views easily extending from UIView
import UIKit
struct AnchoredConstraints {
var top, leading, bottom, trailing, width, height: NSLayoutConstraint?
}
extension UIView {
@discardableResult
func anchor(top: NSLayoutYAxisAnchor?, leading: NSLayoutXAxisAnchor?, bottom: NSLayoutYAxisAnchor?, trailing: NSLayoutXAxisAnchor?, padding: UIEdgeInsets = .zero, size: CGSize = .zero) -> AnchoredConstraints {
import UIKit
extension UIView {
func addSubviews(_ views: UIView...) {
for view in views { addSubview(view) }
}
}
@imjhk03
imjhk03 / String+Extension.swift
Created December 20, 2020 13:43
String+Extension
extension String {
var isValidEmail: Bool {
let emailFormat = ""
let emailPredicate = NSPredicate(format: "SELF MATCHES %@", emailFormat)
return emailPredicate.evaluate(with: self)
}
var isValidPassword: Bool {
//Regex restricts to 8 character minimum, 1 capital letter, 1 lowercase letter, 1 number
@imjhk03
imjhk03 / filter-map-reduce.swift
Created March 1, 2021 17:27
A simple example of filter, map, reduce method
/*
Filter
*/
let familyNames = ["Park", "Lee", "Jong", "Kim", "Choi"]
let longFamilyNames = familyNames.filter { $0.count > 3 }
print(longFamilyNames)
// ["Park", "Jong", "Choi"]
let familyNames = ["Park", "Lee", "Jong", "Kim", "Choi"]
@imjhk03
imjhk03 / String+Date.swift
Created March 25, 2021 02:58
Useful String and Date extensions
import Foundation
extension String {
func toDate(format: String) -> Date? {
let formatter = DateFormatter()
formatter.dateFormat = format
return formatter.date(from: self)
}
}
@imjhk03
imjhk03 / UIHelper+Zeplin.swift
Created January 9, 2021 07:26
A function to convert Zeplin line height to iOS line spacing
import UIKit
enum UIHelper {
/// Convert Zeplin line height to iOS line spacing
static func lineSpacing(from lineHeight: CGFloat, font: UIFont) -> CGFloat {
let value = lineHeight - font.lineHeight
return value
}
@imjhk03
imjhk03 / scrollbar.swift
Last active May 25, 2021 06:38
Shows scrollbar over header or footer view (iOS 12 and below)
func collectionView(_ collectionView: UICollectionView, willDisplaySupplementaryView view: UICollectionReusableView, forElementKind elementKind: String, at indexPath: IndexPath) {
// works from iOS 12 and below
view.layer.zPosition = 0
}
@imjhk03
imjhk03 / Protocol Extensions.swift
Created July 29, 2021 05:49
Sample code about providing default value with protocol extensions
import UIKit
protocol Moveable {
func move(to point: CGPoint)
}
extension Moveable {
func move(to point: CGPoint = .zero) {
move(to: point)
}
@imjhk03
imjhk03 / PizzaBuilder.swift
Last active September 22, 2021 16:58
Sample code for builder pattern in swift.
import Foundation
enum Dough: String {
case basic
case cheeseCrust
}
enum Topping: String, CaseIterable {
case pepperoni
case sausage