Skip to content

Instantly share code, notes, and snippets.

View simonbs's full-sized avatar
🐼

Simon B. Støvring simonbs

🐼
View GitHub Profile
@simonbs
simonbs / SwiftUIHostingConfiguration.md
Last active November 10, 2024 07:46
In iOS 16, Apple added UIHostingConfiguration, making it straightforward to use a SwiftUI view in instances of UICollectionViewCell and UITableViewCell. This project shows how UIHostingConfiguration can be backported to iOS 14 by implementing a type that conforms to the UIContentConfiguration protocol.

SwiftUIHostingConfiguration

In iOS 16, Apple added UIHostingConfiguration, making it straightforward to use a SwiftUI view in instances of UICollectionViewCell and UITableViewCell. This gist shows how UIHostingConfiguration can be backported to iOS 14 by implementing a type that conforms to UIContentConfiguration.

Starting from iOS 16, we can use SwiftUI views in an instance of UICollectionView or UITableViewCell like this:

cell.contentConfiguration = UIHostingConfiguration {
    ExampleCellContentView(color: Color(item.color), text: item.text)
}
@simonbs
simonbs / UISearchAtomView+Swizzle.swift
Created May 31, 2024 09:21
UISearchAtomView+Swizzle
enum UISearchAtomViewSwizzler {
private static var searchAtomViewClass: AnyClass? {
NSClassFromString("_UISearchAtomView")
}
private static var searchAtomViewVisualStyleClass: AnyClass? {
NSClassFromString("_UISearchAtomViewVisualStyle_default")
}
static func swizzle() {
@simonbs
simonbs / Proxy.swift
Created November 4, 2023 19:09
Property wrapper for proxying values.
@propertyWrapper
struct Proxy<EnclosingType, Value> {
typealias ValueKeyPath = ReferenceWritableKeyPath<EnclosingType, Value>
typealias SelfKeyPath = ReferenceWritableKeyPath<EnclosingType, Self>
static subscript(
_enclosingInstance instance: EnclosingType,
wrapped wrappedKeyPath: ValueKeyPath,
storage storageKeyPath: SelfKeyPath
) -> Value {
@simonbs
simonbs / docc.sh
Last active May 2, 2023 08:04
Generates documentation with Apple's DocC for a target that supports both iOS and macOS.
#!/bin/bash
#######################################################################
# Generates documentation for a target, for example a Swift package,
# that supports both iOS and macOS.
#
# The script is designed to be placed in the root of a Swift package.
# Change the paths as needed if your project structure is different.
#
# In order for DocC to annotate all symbols with the platforms
@simonbs
simonbs / README.md
Last active March 15, 2023 22:21
Installing a specific version with Homebrew

Installing a specific version with Homebrew

The Internet has documented several ways of installing a specific version of a piece of software using Homebrew. None of them seem to work anymore. It seems like a feature that the developers of Homebrew do not want to support fully or at all.

A way of installing a specific version is by executing the following commands. The following assumes that we want to install version 3.24.0 of a package named foo.

Start by creating a new tap. The tap can contain multiple formula so we name it something generic.

@simonbs
simonbs / AppKitTextView.swift
Created January 27, 2023 08:39
Shows how a multi-platform SwiftUI can bridge to UIKit and AppKit.
// Implementation of the view using AppKit.
#if os(macOS)
import AppKit
import SwiftUI
final class AppKitTextView: NSView {
let textView: NSTextView = {
let this = NSTextView()
this.translatesAutoresizingMaskIntoConstraints = false
return this
@simonbs
simonbs / XcodeReleases.js
Last active November 14, 2022 01:50
Sample app that lets you browse Xcode releases. Depends on ScriptUI, my yet-to-be-releases project for building native SwiftUI interfaces in JavaScript.
UI.view = RootView()
function RootView() {
const [isLoading, setLoading] = createSignal(true)
const [xcodeGroups, setXcodeGroups] = createSignal([])
fetchXcodes().then((xcodes) => {
setLoading(false)
setXcodeGroups(groupXcodes(xcodes))
})
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// always-run-in-app: true; icon-color: red;
// icon-glyph: film; share-sheet-inputs: url, plain-text;
// Manages a watchlist of movies. Run the script from the share sheet in the IMDb app to add a movie to the watchlist.
// When a movie is added, you can press the "✅"-button to mark the movie as watched. Press the "👀"-button to mark the button to be watched.
// The watchlist is backed by a JSON file stored in iCloud Drive.
let txts = args.plainTexts
let urls = args.urls
// If a plain text and URL input, we're adding a mocke to the list.
@simonbs
simonbs / Cycling Classifications.js
Created July 2, 2022 09:31
Shows rankings for Tour de France 2022.
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: yellow; icon-glyph: bicycle;
let results = await loadResults()
if (config.runsInWidget) {
if (config.widgetFamily == "large") {
let widget = createWidget(results, 8)
await widget.presentLarge()
} else if (config.widgetFamily == "medium") {
let widget = createWidget(results, 3)
@simonbs
simonbs / WWDC22.js
Created May 30, 2022 08:09
Scriptable widget counting down to WWDC22
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: cyan; icon-glyph: apple-alt;
const TITLE = "WWDC22"
const PAST_EVENT_TEXT = "Have a great Dub Dub!"
const DATE = "2022-06-06T17:00:00Z"
const BG_IMG_URL = "https://i.ibb.co/9t9CbxX/wwdc22.png"
const LINK = "https://developer.apple.com/wwdc22/"
let widget = await createWidget()