Skip to content

Instantly share code, notes, and snippets.

View acrookston's full-sized avatar
👨‍💻
Coding!

Andrew Crookston acrookston

👨‍💻
Coding!
View GitHub Profile
//: [Previous](@previous)
import Foundation
var input = """
626 2424 2593 139 2136 163 1689 367 2235 125 2365 924 135 2583 1425 2502
183 149 3794 5221 5520 162 5430 4395 2466 1888 3999 3595 195 181 6188 4863
163 195 512 309 102 175 343 134 401 372 368 321 350 354 183 490
2441 228 250 2710 200 1166 231 2772 1473 2898 2528 2719 1736 249 1796 903
3999 820 3277 3322 2997 1219 1014 170 179 2413 183 3759 3585 2136 3700 188
//
// LICENSE: MIT
// Created by Andrew C on 4/25/17.
// Copyright © 2017 Andrew Crookston. All rights reserved.
//
class TopAlignedLabel: UILabel {
override func drawText(in rect: CGRect) {
if let string = text as NSString? {
let size = string.boundingRect(with: CGSize(width: self.frame.width,height: CGFloat.greatestFiniteMagnitude),
@acrookston
acrookston / DateExtensions.swift
Created February 7, 2018 18:26
Swift Date helpers
//
// CoreExtensions
//
// Created by Andrew Crookston on 1/30/18.
//
import Foundation
public extension DateComponents {
public static func with(year: Int? = nil, month: Int? = nil, day: Int? = nil, hour: Int? = nil, minute: Int? = nil, second: Int? = nil, calendar: Calendar = .current, timeZone: TimeZone? = nil) -> DateComponents {
@acrookston
acrookston / BackgroundColorButton.swift
Last active February 15, 2018 18:14
A iOS UIButton subclass with support for background colors
//
// LICENSE: MIT
// Source: https://gist.github.com/acrookston/af50c921089b93d1d71e7a9349dc7af7
// Created by Andrew C on 4/16/17.
// Copyright © 2017 Andrew Crookston. All rights reserved.
//
import UIKit
extension UIControlState: Hashable {
@acrookston
acrookston / TimeIntervalExtensions.swift
Created February 15, 2018 21:27
TimeInterval extensions
extension TimeInterval {
var milliseconds: Int {
return Int(truncatingRemainder(dividingBy: 1) * 100)
}
var minutes: Int {
return Int(self / 60)
}
var seconds: Int {
@acrookston
acrookston / UIColorExtensions.swift
Created February 15, 2018 21:28
UIColor Extensions
extension UIColor {
static func rgba(_ r: CGFloat, _ g: CGFloat, _ b: CGFloat, _ a: CGFloat) -> UIColor {
return UIColor(red: r / 255.0, green: g / 255.0, blue: b / 255.0, alpha: a)
}
}
@acrookston
acrookston / fast_code.swift
Last active February 19, 2018 03:37
This code takes several minutes (3min on MacBook Pro i5 2.4Ghz) to compile
struct Test {
let computed: Int
init(a: Int, b: Int, c: Int, d: Int, e: Int = 0, f: Int = 0, g: Int = 0, h: Int = 0) {
var value: Int = (a << 56)
value |= (b << 48)
value |= (c << 40)
value |= (d << 32)
value |= (e << 24)
value |= (f << 16)
value |= (g << 8)
@acrookston
acrookston / HeaderCollectionViewController.swift
Last active February 26, 2018 01:15
UICollectionView example in Swift
class HeaderCollectionViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UICollectionViewDelegate {
var collectionView : UICollectionView?
override func viewDidLoad() {
super.viewDidLoad()
var layout = CSStickyHeaderFlowLayout()
layout.parallaxHeaderReferenceSize = CGRectMake(... size of the header...)
layout.minimumInteritemSpacing = 0
@acrookston
acrookston / MigrationCreateItems.swift
Last active April 28, 2018 22:09
Simple SQLite migration for Swift with the SQLite.swift framework
//
// MigrationCreateItems.swift
// Stash
//
// Created by Andrew C on 8/30/17.
// Copyright © 2017 Andrew Crookston. All rights reserved.
// License: MIT
//
import Foundation
extension String {
func localized(_ args: CVarArg...) -> String {
return String(format: NSLocalizedString(self, comment: ""), arguments: args)
}
}
let format = NSLocalizedString("Hello %@", comment: "")
print(String(format: format, "World"))