Skip to content

Instantly share code, notes, and snippets.

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)
}
import UIKit
extension UIAlertController {
func setLeftAlignedMessage(_ message: String) {
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .left
let messageText = NSMutableAttributedString(
string: message,
import UIKit
class FullContentIntrinsicSizeTableView: UITableView {
override var contentSize: CGSize {
didSet {
if contentSize != oldValue {
invalidateIntrinsicContentSize()
}
}
@objcMembers
private class BlockHandler: NSObject {
var block: (() -> Void)?
@objc func runBlock() {
block?()
}
}
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 {
extension String {
func draw(fontSize: CGFloat) -> UIImage? {
draw(font: .systemFont(ofSize: fontSize))
}
func draw(font: UIFont) -> UIImage? {
draw(attributes: [.font: font], backgroundColor: .clear)
}
@danielgarbien
danielgarbien / SortDescriptor.swift
Created July 14, 2022 14:45
Multi-attribute sorting with sort descriptors
import Foundation
struct SortDescriptor<Value> {
let compare: (Value, Value) -> ComparisonResult
}
extension Collection {
func sorted(by sortDescriptors: [SortDescriptor<Element>]) -> [Element] {
sorted { a, b in
class TouchGoThroughView: UIView {
override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
super.point(inside: point, with: event) && pointInsideVisibleSubviews(point, with: event)
}
}
extension UIView {
func pointInsideVisibleSubviews(_ point: CGPoint, with event: UIEvent?) -> Bool {