Skip to content

Instantly share code, notes, and snippets.

danielgarbien

View GitHub Profile
View String+UIImage.swift
extension String {
func draw(fontSize: CGFloat) -> UIImage? {
draw(font: .systemFont(ofSize: fontSize))
}
func draw(font: UIFont) -> UIImage? {
draw(attributes: [.font: font], backgroundColor: .clear)
}
View Codable+NSCoding.swift
extension KeyedDecodingContainer {
func decode<T: NSCoding>(_ type: T.Type, forKey key: KeyedDecodingContainer<K>.Key) throws -> T? {
let data = try decode(Data.self, forKey: key)
return try NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(data) as? T ?? nil
}
}
extension KeyedEncodingContainer {
View Timer_iOS9
@objcMembers
private class BlockHandler: NSObject {
var block: (() -> Void)?
@objc func runBlock() {
block?()
}
}
@danielgarbien
danielgarbien / SessionDelegate.swift
Last active Mar 14, 2018
Simple network synchronizer with caching
View SessionDelegate.swift
import Foundation
/**
The only benefit SessionDelegate provides is to accompany URL respones with cache control headers which are not provided by the backend at the time of writing.
(Cache control headers in responses let us use Foundation's NSURLCache abilities.)
Once the backend implementation is changed to provide responses with cache control headers SessionDelegate should no longer be used.
NOT THREAD SAFE - it should be used on the delegation queue of NSURLSession
*/
View FullContentIntrinsicSizeTableView.swift
import UIKit
class FullContentIntrinsicSizeTableView: UITableView {
override var contentSize: CGSize {
didSet {
if contentSize != oldValue {
invalidateIntrinsicContentSize()
}
}
View UIAlertController+LeftAlignedMessage.swift
import UIKit
extension UIAlertController {
func setLeftAlignedMessage(_ message: String) {
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .left
let messageText = NSMutableAttributedString(
string: message,
View UITableView+Registration
import UIKit
extension UITableView {
func dequeueReusableCell(withIdentifier identifier: String, registerNibIfNeededWithNibName nibName: String) -> UITableViewCell? {
guard let cell = dequeueReusableCell(withIdentifier: identifier) else {
register(UINib(nibName: nibName, bundle: nil), forCellReuseIdentifier: identifier)
return dequeueReusableCell(withIdentifier: identifier)
}
View KeyboardAwareScrollViewCoordinator.swift
import Foundation
import UIKit
/**
* Coordinates contentInset on scroll view to always show full content on keyboard appearing.
*/
class KeyboardAwareScrollViewCoordinator: NSObject {
let scrollView: UIScrollView
private let originalContentInset: UIEdgeInsets
View Delay.swift
func delay(_ delay: Double, closure: @escaping () -> Void) {
DispatchQueue.main.asyncAfter(deadline: .now() + delay, execute: closure)
}
View ContainmentWithAutoLayout.swift
import UIKit
extension UIViewController {
/**
Add childController with no transition.
Embeds its view in container.
Calls didMove(toParentViewController:) on childController at a last step.
*/
func addImmediately(childController: UIViewController, embeddedIn container: UIView) {
You can’t perform that action at this time.