Skip to content

Instantly share code, notes, and snippets.

View hannesoid's full-sized avatar

Hannes Oud hannesoid

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)
}
@manmal
manmal / ScrollViewThatFits.swift
Last active March 1, 2024 16:08
Wrap a SwiftUI View in a ScrollView, expanding to full height/width & preventing bouncing when possible (iOS > = 16.4)
import SwiftUI
/// Wraps the content in a ScrollView that only bounces
/// if the content size exceeds the visible area;
/// and makes the content fill the ScrollView if desired.
/// Per default, the content fills the ScrollView only
/// vertically.
///
/// Usage:
///
@ryanlintott
ryanlintott / KeyboardButtonStyle.swift
Last active February 25, 2024 01:15
A SwiftUI ButtonStyle that looks like an iOS keyboard key.
//
// KeyboardButtonStyle.swift
// Wordhord
//
// Created by Ryan Lintott on 2024-02-06.
//
import ShapeUp
import SwiftUI
@lukaskubanek
lukaskubanek / SwiftUIListRowHighlightFix.swift
Last active March 26, 2024 17:51
A workaround for fixing SwiftUI’s list row highlighting behavior by preventing unwanted delays
import InterposeKit
import SwiftUI
/// A workaround for an issue in SwiftUI related to delayed highlighting of list rows upon user
/// interaction, rendering it inconsistent with UIKit.
///
/// This fix implements the solution proposed by Léo Natan and utilizes `InterposeKit` by Peter
/// Steinberger to modify the behavior of `UICollectionViewCell` instances used internally
/// by SwiftUI.
///
@manmal
manmal / TaskAutoCancellation.swift
Last active July 27, 2022 20:52
Swift Structured Concurrency - Task Auto Cancellation
public extension Task {
/// Cancels this `Task` when the surrounding `Task` is cancelled.
/// This is necessary if `Task {}` and `Task.detached {}`
/// should be automatically cancelled - otherwise, such Tasks
/// just run until finished.
///
/// Usage:
///
/// await Task { await myAsyncFunc() }.autoCancel()
func autoCancel() async -> Void {
@manmal
manmal / AsyncSequenceExtensions.swift
Created July 6, 2022 16:57
AsyncSequence.eraseToAsyncStream()
import Foundation
// Props to @pteasima and @MichalKleinJr:
// https://twitter.com/pteasima/status/1544723987606929408?s=21&t=JL1oIuL87Ms_VPBQBZQ7Rg
public extension AsyncSequence {
func eraseToAsyncStream() -> AsyncStream<Element> {
return AsyncStream { continuation in
let task = Task {
do {
for try await value in self {
@CDRussell
CDRussell / .zshrc
Last active December 22, 2021 11:28
# Git branch in command prompts
autoload -Uz vcs_info
precmd() {vcs_info}
setopt PROMPT_SUBST
# the last part of this command dictates what is shown for git info, referenced later by the prompt command.
# here, showing branch name %b in color 38 (blue) and the repo name %r in color 226 (yellow)
zstyle ':vcs_info:git:*' formats '%F{38}%b%f %F{226}(%r)%f'
# can set both the left (PROMPT) and right (RPROMPT) prompts
@jverkoey
jverkoey / UIFont+CustomizedDynamicType.m
Created April 14, 2021 01:07
Dynamic Type system fonts with custom point sizes, weight, and italics
static const CGFloat kFontWeightEpsilon = FLT_EPSILON;
@implementation UIFont (CustomizedDynamicType)
+ (nonnull UIFont *)preferredFontWithDefaultSize:(CGFloat)size
textStyle:(nonnull UIFontTextStyle)textStyle {
return [self preferredFontWithDefaultSize:size
textStyle:textStyle
fontWeight:UIFontWeightRegular
italic:NO];
@smic
smic / BorderlessWindow.swift
Last active July 7, 2023 20:19
Extension to create borderless windows in SwiftUI
import SwiftUI
extension CGRect {
fileprivate func point(anchor: UnitPoint) -> CGPoint {
var point = self.origin
point.x += self.size.width * anchor.x
#if os(macOS)
point.y += self.size.height * (1 - anchor.y)
#else
point.y += self.size.height * anchor.y
@hayakawa
hayakawa / run_antelope.sh
Last active January 6, 2020 23:53
[ ATTENTION!: This script will be unnecessary just now! / See also: https://en.antelopeaudio.com/2020/01/antelope-audio-product-compatibility-with-macos-catalina/ ]Little code to run Antelope Launcher and Server on MacOS Catalina
#!/bin/bash
### Get my uid
UserID=`id -u`
echo "Your UID: ${UserID}"
### Unload root process
sudo launchctl unload /Library/LaunchDaemons/com.antelopeaudio.daemon.plist
### Kill the process if it was already awaken