Skip to content

Instantly share code, notes, and snippets.

View a2's full-sized avatar
🐼

Alex Akers a2

🐼
View GitHub Profile
@a2
a2 / ScaledValue.swift
Created March 16, 2022 19:40
ScaledValue
import SwiftUI
import UIKit
@propertyWrapper
struct ScaledValue: DynamicProperty {
@Environment(\.dynamicTypeSize) private var dynamicTypeSize
private var lastDynamicTypeSize: DynamicTypeSize!
private(set) var wrappedValue: CGFloat
import SwiftUI
struct LocationTapGesture: Gesture {
enum Value {
case recognized(CGPoint)
case failed
}
var coordinateSpace: CoordinateSpace

This works, but unnecessarily exposes T in Unwrap's generic parameters.

import SwiftUI

struct Unwrap<T, Content>: View where Content: View {
    let body: Content

    init(value: T?, @ViewBuilder ifSome: (T) -> Content, @ViewBuilder ifNone: () -> Content) {
        if let value = value {
@a2
a2 / MyPlayground.swift
Last active August 23, 2019 06:28
Tibetan characters cause NSLayoutManager to return incorrect bounding rect (rdar://48745247)
let string = "If the Tibetan characters དོམ་ཁྲ are removed, then the function boundingRect(forGlyphRange:in:) returns the correct value. Note that the values are incorrect for any range in a paragraph containing these characters."
let textStorage = NSTextStorage(string: string)
textStorage.addAttribute(.font, value: UIFont.systemFont(ofSize: UIFont.labelFontSize), range: NSRange(0 ..< textStorage.length))
// Xcode in dark mode makes the text hard to read.
textStorage.addAttribute(.foregroundColor, value: UIColor.white, range: NSRange(0 ..< textStorage.length))
let textContainer = NSTextContainer(size: CGSize(width: 300, height: 300))
let layoutManager = NSLayoutManager()
layoutManager.addTextContainer(textContainer)
@a2
a2 / install_booted.sh
Last active September 25, 2018 12:11
Installs app at argument path to all booted simulators and launches them
#!/bin/sh
if [ "$#" -ne 1 ]; then
echo "$0 <path>"
exit 1
fi
APP_PATH=$1
BUNDLE_ID=`/usr/libexec/PlistBuddy -c "Print :CFBundleIdentifier" "$APP_PATH/Info.plist"`
for DEVICE in $(xcrun simctl list -j devices | jq --raw-output ".devices[][] | select(.state == \"Booted\") | .udid"); do
@a2
a2 / Workaround42199059.swift
Created July 14, 2018 18:59
Workaround for rdar://42199059
extension NSLayoutManager {
func invalidateLayout() {
let characterRange: NSRange
if let textStorage = textStorage {
// fast path
characterRange = NSRange(0 ..< textStorage.length)
} else {
characterRange = self.characterRange(forGlyphRange: NSRange(0 ..< numberOfGlyphs), actualGlyphRange: nil)
}
import UIKit
extension UIView {
var superviews: UnfoldFirstSequence<UIView> {
return sequence(first: self) { view in view.superview }
}
func enclosingView(where predicate: (UIView) -> Bool) -> UIView? {
return superviews.first(where: predicate)
}
Verifying my Blockstack ID is secured with the address 1PLiBanbTudwosuxUjb8ifCsufcx2AHkiY https://explorer.blockstack.org/address/1PLiBanbTudwosuxUjb8ifCsufcx2AHkiY
struct Person {
let firstName: String
let lastName: String
}
let names = """
Sandy Maynez
Kelli Ragland
Tracee Santucci
Bella Burts
@a2
a2 / pass-payload.js
Created August 13, 2017 16:17
Swiftly Passcards
{
// must be 1
"formatVersion": 1,
// required for "dynamic" passes
"authenticationToken": "<secret>",
"serialNumber": "<secret>",
"webServiceURL": "https://pass.a2.io/",
// top-left "logo" text