Skip to content

Instantly share code, notes, and snippets.

View auramagi's full-sized avatar

Mike Apurin auramagi

View GitHub Profile
@auramagi
auramagi / CoreTextShape.swift
Last active June 27, 2022 11:16
Use Core Text in SwiftUI
import SwiftUI
struct ContentView: View {
var body: some View {
CoreTextShape(
string: "Hello YUMEMI.swift!",
font: .boldSystemFont(ofSize: 42)
)
.border(.purple)
.padding()
@auramagi
auramagi / LayoutAdaptor.swift
Created June 11, 2022 12:43
Drive UIView frame layout with SwiftUI Layout protocol
// The main layout logic
import SwiftUI
final class LayoutAdaptor: UIView {
var layout: (any Layout)? {
didSet { setNeedsLayout() }
}
override func layoutSubviews() {
@auramagi
auramagi / ImageRendererModifier.swift
Created June 7, 2022 08:08
Using SwiftUI ImageRenderer in view modifier-like view extension
import SwiftUI
struct ContentView: View {
var body: some View {
Text("Hello WWDC22")
.rendered(scale: 2)
}
}
struct ContentView_Previews: PreviewProvider {
@auramagi
auramagi / PTRBug.swift
Created January 12, 2022 19:57
UIRefreshControl stuck when changing tabs in UITabBarController
import UIKit
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
func makeRefreshControl() -> UIRefreshControl {
let refresh = UIRefreshControl()
@auramagi
auramagi / BracketScanner.swift
Created November 29, 2021 03:27
Parse Swift type into a tree where the child nodes are the generic types. Useful for making trees from SwiftUI View types.
import Foundation
final class BracketScanner {
let text: String
init(text: String) {
self.text = text.trimmingCharacters(in: CharacterSet(charactersIn: "()"))
}
lazy var scanner: Scanner = {
@auramagi
auramagi / ToDoApp.swift
Last active September 2, 2021 10:36
ForEachでBindingを渡す書き方のサンプルコード
import SwiftUI
@main
struct ToDoApp: App {
@StateObject var model: Model = .init()
var body: some Scene {
WindowGroup {
ScrollView {
VStack(alignment: .leading) {
@auramagi
auramagi / ConsoleOutput.txt
Last active October 3, 2024 04:07
SwiftUI _viewDebugData
// Output of print(_viewDebugData), formatted for easier viewing
[SwiftUI._ViewDebug.Data(
data: [
SwiftUI._ViewDebug.Property.value: SwiftUI._SafeAreaInsetsModifier(elements: [], nextInsets: nil),
SwiftUI._ViewDebug.Property.displayList: (display-list
(item #:identity 2 #:version 6
(frame (143.66666666666666 354.0; 88.0 20.333333333333332))
(text "Hello World" #:size (88.0, 20.333333333333332))
)
@auramagi
auramagi / Cats.swift
Last active December 11, 2020 03:21
Swift cat images
enum Cats {
private struct CatResponse: Decodable {
let url: URL
}
@discardableResult
static func getRandomImageURL(completion: @escaping (URL?) -> Void) -> URLSessionDataTask {
let request = URLRequest(url: URL(string: "https://api.thecatapi.com/v1/images/search")!)
let task = URLSession.shared.dataTask(with: request) { data, _, _ in
let catURL: URL? = {
// reloadDataを使わないようにしましょう
import UIKit
import DiffableDataSources // https://github.com/ra1028/DiffableDataSources
@available(iOS, introduced: 13.0)
typealias TableViewDiffableDataSource = UITableViewDiffableDataSource
@available(iOS, introduced: 13.0)
typealias DiffableDataSourceSnapshot = NSDiffableDataSourceSnapshot
@auramagi
auramagi / PickerLabel.swift
Created February 18, 2019 16:52
Present UIPicker on a UILabel to select from a few options. Supports a placeholder value. For this example the picker selects a weekday.
import UIKit
enum Weekday: Int {
case monday = 1
case tuesday = 2
case wednesday = 3
case thursday = 4
case friday = 5
case saturday = 6
case sunday = 0