Instantly share code, notes, and snippets.

View KeyboardTableView.swift
// Douglas Hill, December 2018
// Made for https://douglashill.co/reading-app/
import UIKit
/// A table view that allows navigation and selection using a hardware keyboard.
/// Only supports a single section.
class KeyboardTableView: UITableView {
// These properties may be set or overridden to provide discoverability titles for key commands.
var selectAboveDiscoverabilityTitle: String?
View PSPDFTableViewCell+LayoutMarginsGuide.m
@implementation PSPDFTableViewCell // UITableViewCell subclass
/**
On iOS 10, constraints involving a UITableViewCell’s contentView’s layoutMarginsGuide are removed for some
reason before the cell appears, which breaks the layout. This layout guide is a working alternative.
*/
- (UILayoutGuide *)pspdf_layoutMarginsGuide {
if (@available(iOS 11.0, *)) {
return self.contentView.layoutMarginsGuide;
}
View ReduceMotionScrollView.swift
// Douglas Hill, October 2018
import UIKit
/**
A scroll view that honours the reduce motion accessibility setting.
If reduce motion is enabled, animated adjustments to contentOffset
will use a cross dissolve instead of translation.
View UIContentSizeCategory+ShortDescription.swift
private extension UIContentSizeCategory {
var shortDescription: String {
switch self {
case .unspecified: return "unspecified"
case .extraSmall: return "XS"
case .small: return "S"
case .medium: return "M"
case .large: return "L"
case .extraLarge: return "XL"
View LocalisedStrings.swift
// Douglas Hill, February 2018
import Foundation
/// Returns a localised string with the key as an enum case so the compiler checks it exists.
/// The enum should be automatically generated using UpdateLocalisedStringKeys.swift.
public func localisedString(_ key: LocalisedStringKey) -> String {
return Bundle.main.localizedString(forKey: key.rawValue, value: nil, table: nil)
}
View LiveSquishButton.swift
import UIKit
class LiveSquishButton: UIView {
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
updateSquish(with: 0.05)
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesMoved(touches, with: event)
View K-means colour matching.m
@import Foundation;
@import CoreGraphics;
@import ImageIO;
static NSUInteger maxThumbnailSize = 20;
extern uint64_t dispatch_benchmark(size_t count, void (^block)(void));
/// Returns a CGImage with a +1 retain count.
static CGImageRef createThumbnailFromURL(NSURL *imageURL, NSUInteger maxPixelSize)
View LazyMapCollection-1.swift
import Foundation
let dictionary = ["one": 1, "two": 2]
let lazyValues = dictionary.values
let jsonData = try JSONSerialization.data(withJSONObject: lazyValues, options: [])
// ❗️ Argument type 'LazyMapCollection<Dictionary<String, Int>, Int>' does not conform to expected type 'AnyObject'
View random-string.m
@implementation NSArray (DHRandom)
- (nullable id)dh_random {
if (self.count == 0) {
return nil;
}
return self[arc4random_uniform((u_int32_t)self.count)];
}
@end
View DHLazyLinkedList.h
@import Foundation;
NS_ASSUME_NONNULL_BEGIN
@interface DHLazyLinkedList<ObjectType> : NSArray<ObjectType>
- (instancetype)initWithRootObject:(ObjectType)rootObject nextObjectBlock:(ObjectType _Nullable (^)(ObjectType))next NS_DESIGNATED_INITIALIZER;
- (nullable ObjectType)objectAfterObject:(ObjectType)object;