This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@import Foundation; | |
@import ImageIO; | |
BOOL exportAtVariousCompressionQualities(NSURL *sourceLocation, NSURL *exportLocation, NSError **error) | |
{ | |
if (![[NSFileManager defaultManager] createDirectoryAtURL:exportLocation withIntermediateDirectories:YES attributes:nil error:error]) { | |
return NO; | |
} | |
CGImageSourceRef source = CGImageSourceCreateWithURL((__bridge CFURLRef)(sourceLocation), NULL); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Douglas Hill, April 2017 | |
// From https://douglashill.co/reading-app/ | |
#if os(macOS) | |
import AppKit | |
public typealias Colour = NSColor | |
public typealias Font = NSFont | |
public typealias FontDescriptor = NSFontDescriptor | |
public typealias Image = NSImage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Douglas Hill, December 2019 | |
// Made for https://douglashill.co/reading-app/ | |
import Foundation | |
/// Tries really hard to read a string from a file. | |
/// | |
/// Brute forces encodings if necessary. This will only fail if the file can’t be interpreted | |
/// in any encoding, or if some other error occurs like not being able to read from the file. | |
/// |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NSError *error = [NSError errorWithDomain:@"domain" code:123 userInfo:nil]; | |
[(NSFileHandle *)nil seekToOffset:0 error:&error]; | |
assert(error != nil); // 💥 Not OK. The error has gone! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Douglas Hill, November 2019 | |
// Find the latest version of this file at https://github.com/douglashill/KeyboardKit | |
import UIKit | |
/// A scroll view that allows scrolling using a hardware keyboard like `NSScrollView`. | |
/// Supports arrow keys, option + arrow keys, command + arrow keys, space bar, page up, page down, home and end. | |
/// Limitations: | |
/// - Paging scroll views (isPagingEnabled = true) are not supported yet. | |
/// - The scroll view must become its own delegate so setting the delegate is not supported yet. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@import ImageIO; | |
@implementation UIImage (DHImageDecoding) | |
/// Warning: do not use. This looks like it should work, but it blocks the main thread for non-obvious reasons. | |
+ (UIImage *)dh_decodedImageWithData:(NSData *)data | |
{ | |
CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL); | |
if (source == NULL) { | |
return nil; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// A Swift property wrapper that implements ‘lazy let’. I.e. a read-only property that loads its value when first read. | |
/// Not safe for access from multiple threads. | |
/// Does not work the same as the lazy keyboard because the property initialiser will run before self is available. | |
/// Adapted from https://github.com/apple/swift-evolution/blob/master/proposals/0258-property-wrappers.md | |
@propertyWrapper enum LazyLet<Value> { | |
case uninitialised(() -> Value) | |
case initialised(Value) | |
init(wrappedValue: @autoclosure @escaping () -> Value) { | |
self = .uninitialised(wrappedValue) |