Skip to content

Instantly share code, notes, and snippets.

@douglashill
douglashill / SidebarTest.swift
Created Oct 9, 2020
Trivial Catalyst test app to see if `primaryBackgroundStyle = .sidebar` works to get a translucent blurry sidebar on Big Sur
View SidebarTest.swift
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
}
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
private var _window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
@douglashill
douglashill / FourColumns.swift
Created Jun 23, 2020
A sample UIKit app that sets up a four column layout with new iOS 14 API on UISplitViewController.
View FourColumns.swift
import UIKit
class FourColumnsContainerViewController: UIViewController {
let outerSplitViewController = UISplitViewController(style: .tripleColumn)
let innerSplitViewController = UISplitViewController(style: .doubleColumn)
let primary = makeContentViewController("App")
let secondary = makeContentViewController("Files")
let mainContent = makeContentViewController("File Content")
let inspector = makeContentViewController("Inspector")
@douglashill
douglashill / LazyLet.swift
Created Apr 22, 2020
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.
View LazyLet.swift
/// 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)
@douglashill
douglashill / MenuAlignment.swift
Last active Aug 15, 2020
Swizzles the iOS contextual menu and share sheet to improve usability by showing the icon on the leading side. Read more: https://douglashill.co/menu-icon-swizzling/
View MenuAlignment.swift
// Douglas Hill, March 2020
// Code for the article at https://douglashill.co/menu-icon-swizzling/
import UIKit
struct MenuAlignmentFixError: Error, CustomStringConvertible {
let description: String
}
@douglashill
douglashill / extract-most-common-localised-strings.swift
Last active Apr 12, 2020
Extracts the most common translations from Apple’s glossary files. Read more: https://douglashill.co/localisation-using-apples-glossaries/
View extract-most-common-localised-strings.swift
#! /usr/bin/swift
// Douglas Hill, March 2020
/*
Extracts the most common translations from Apple’s glossary files.
This script helped with localisation for KeyboardKit (https://github.com/douglashill/KeyboardKit) by leveraging Apple’s existing translations.
More detail in the article at https://douglashill.co/localisation-using-apples-glossaries/
@douglashill
douglashill / extract-specific-localised-strings.swift
Last active Sep 28, 2020
Extracts specific localised strings from Apple’s glossary files. Read more: https://douglashill.co/localisation-using-apples-glossaries/
View extract-specific-localised-strings.swift
#! /usr/bin/swift
// Douglas Hill, March 2020
// This file is made available under the MIT license included at the bottom of this file.
/*
Extracts specific localised strings from Apple’s glossary files.
This script helped with localisation for KeyboardKit (https://github.com/douglashill/KeyboardKit) by leveraging Apple’s existing translations.
@douglashill
douglashill / explore-localisation-glossaries.swift
Last active Nov 16, 2020
Prints out translations from Apple’s glossary files matching text in a supplied English .strings file. Read more: https://douglashill.co/localisation-using-apples-glossaries/
View explore-localisation-glossaries.swift
#! /usr/bin/swift
// Douglas Hill, March 2020
// Prints out translations from Apple’s glossary files matching text in a supplied English .strings file.
// More detail in the article at https://douglashill.co/localisation-using-apples-glossaries/
import Foundation
let stringsSource = URL(fileURLWithPath: "PUT THE PATH TO YOUR ENGLISH .strings FILE HERE")
@douglashill
douglashill / updateSafeAreaForKeyboardFromNotification.swift
Last active Sep 25, 2020
Avoid the keyboard by leveraging additionalSafeAreaInsets.
View updateSafeAreaForKeyboardFromNotification.swift
// Avoids the keyboard in a UIKit app by leveraging additionalSafeAreaInsets.
// You can put this in the root view controller so the whole app will avoid the keyboard.
// Only tested on iOS 13.3.
// Made for https://douglashill.co/reading-app/
@objc func updateSafeAreaForKeyboardFromNotification(_ notification: Notification) {
guard let endFrameInScreenCoords = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect else {
return
}
// Please consider whether the force unwrap here is safe for your own use case.
@douglashill
douglashill / FindReplace.swift
Created Feb 11, 2020
Swift script helper to find and replace an array of substitutions.
View FindReplace.swift
/// Performs a textual find and replace with the specified substitutions on all files in directory (recursive) that have the specified filename extensions.
func find(_ substitutions: [(find: String, replace: String)], allowedExtensions: Set<String>, directory rootURL: URL) {
let fileURLs = FileManager.default.enumerator(at: rootURL, includingPropertiesForKeys: nil)!.map {
$0 as! URL
}
.filter {
$0.hasDirectoryPath == false
}
.filter {
allowedExtensions.contains($0.pathExtension)
@douglashill
douglashill / PlatformDifferences.swift
Created Dec 13, 2019
The current file I use to smooth over API differences between AppKit and UIKit.
View PlatformDifferences.swift
// 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
You can’t perform that action at this time.