Skip to content

Instantly share code, notes, and snippets.

douglashill / AdaptiveTraitsContainer.swift
Created Apr 13, 2019
Experimenting altering an iOS app size class to be responsive to the Dynamic Text size.
View AdaptiveTraitsContainer.swift
import UIKit
/// A wrapper view controller that makes the horizontal size class
/// be based on both the Dynamic Text size and the width available.
class AdaptiveTraitsContainer: UIViewController {
let wrappedViewController: UIViewController
init(wrappedViewController: UIViewController) {
self.wrappedViewController = wrappedViewController
super.init(nibName: nil, bundle: nil)
douglashill / KeyboardTableView.swift
Last active Apr 9, 2019
A UITableView that allows navigation and selection using a hardware keyboard.
View KeyboardTableView.swift
// Douglas Hill, December 2018
// Made for
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?
douglashill / PSPDFTableViewCell+LayoutMarginsGuide.m
Created Nov 16, 2018
Alternative for a table view cell’s content view’s layoutMarginsGuide that works on iOS 10.
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;
douglashill / ReduceMotionScrollView.swift
Created Oct 21, 2018
A UIScrollView subclass that honours the reduce motion accessibility setting
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.
douglashill / UIContentSizeCategory+ShortDescription.swift
Created Aug 22, 2018
Compact description of an iOS content size category. Useful for analytics.
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"
douglashill / LocalisedStrings.swift
Last active Sep 17, 2018
Generating an enum to ensure only defined localised string keys are used. For development on Apple platforms.
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)
douglashill / LiveSquishButton.swift
Created Apr 27, 2017
(Not working well yet) What if a button had a variable touch down state without using a canned animation?
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)
douglashill / K-means colour matching.m
Created Sep 9, 2016
K-means colour matching — putting this old file somewhere
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)
douglashill / LazyMapCollection-1.swift
Last active Aug 14, 2016
Can’t pass LazyMapCollection to Objective-C API
View LazyMapCollection-1.swift
import Foundation
let dictionary = ["one": 1, "two": 2]
let lazyValues = dictionary.values
let jsonData = try lazyValues, options: [])
// ❗️ Argument type 'LazyMapCollection<Dictionary<String, Int>, Int>' does not conform to expected type 'AnyObject'
douglashill / random-string.m
Created Jun 21, 2016
Generates a random string of real US English words using UITextChecker completions
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)];
You can’t perform that action at this time.