Skip to content

Instantly share code, notes, and snippets.

View douglashill's full-sized avatar

Douglas Hill douglashill

View GitHub Profile
@douglashill
douglashill / PSPDFTableViewCell+LayoutMarginsGuide.m
Created November 16, 2018 07:50
Alternative for a table view cell’s content view’s layoutMarginsGuide that works on iOS 10.
@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
douglashill / ReduceMotionScrollView.swift
Created October 21, 2018 16:10
A UIScrollView subclass that honours the reduce motion accessibility setting
// 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
douglashill / UIContentSizeCategory+ShortDescription.swift
Created August 22, 2018 15:12
Compact description of an iOS content size category. Useful for analytics.
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
douglashill / LocalisedStrings.swift
Last active September 17, 2018 09:02
Generating an enum to ensure only defined localised string keys are used. For development on Apple platforms.
// 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
douglashill / LiveSquishButton.swift
Created April 27, 2017 08:49
(Not working well yet) What if a button had a variable touch down state without using a canned animation?
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
douglashill / K-means colour matching.m
Created September 9, 2016 17:15
K-means colour matching — putting this old file somewhere
@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
douglashill / LazyMapCollection-1.swift
Last active August 14, 2016 23:15
Can’t pass LazyMapCollection to Objective-C API
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'
@douglashill
douglashill / random-string.m
Created June 21, 2016 17:32
Generates a random string of real US English words using UITextChecker completions
@implementation NSArray (DHRandom)
- (nullable id)dh_random {
if (self.count == 0) {
return nil;
}
return self[arc4random_uniform((u_int32_t)self.count)];
}
@end
@douglashill
douglashill / DHLazyLinkedList.h
Created May 7, 2016 14:03
Linked list with an array interface. Can be used for implicit linked lists like superviews or the responder chain.
@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;
@douglashill
douglashill / RGB-XYZ.c
Created May 7, 2016 13:37
Transform between RGB and XYZ colours with come calculations I found on the web
#include <CoreGraphics/CoreGraphics.h>
static inline CGFloat gammaCorrect(CGFloat component) {
if (component > 0.04045f) {
return pow((component + 0.055f) / 1.055f, 2.4f);
}
return component / 12.92f;
}