Skip to content

Instantly share code, notes, and snippets.

@haikusw
haikusw / WWDC.json
Created May 26, 2023 18:51 — forked from harishgonnabattula/WWDC.json
WWDC 2015-2018 Video urls and summary
[
{"url": "https://devstreaming-cdn.apple.com/videos/wwdc/2018/236mwbxbxjfsvns4jan/236/236_hd_avspeechsynthesizer_making_ios_talk.mp4?dl=1", "title": "AVSpeechSynthesizer: Making iOS Talk", "summary": "Speech can enhance the audio experience of your app, whether you are generating spoken feedback for accessibility, or providing critical information beyond simple alerts or notifications. AVSpeechSynthesizer produces synthesized speech from text and allows you to control and monitor the progress of ongoing speech. Learn the ins and outs of AVSpeechSynthesizer and how to add computer-generated speech output to your app."},
{"url": "https://devstreaming-cdn.apple.com/videos/wwdc/2018/405bjty1j94taqv8ii/405/405_hd_measuring_performance_using_logging.mp4?dl=1", "title": "Measuring Performance Using Logging", "summary": "Learn how to use signposts and logging to measure performance. Understand how the Points of Interest instrument can be used to examine logged data. Get an introduction into creating and using custo
@haikusw
haikusw / swiftui.swift
Created March 15, 2023 06:53 — forked from chriseidhof/swiftui.swift
SwiftUI - iOS
import Combine
import CoreFoundation
import CoreGraphics
import CoreText
import Darwin
import Foundation
import SwiftUI
import UIKit
import os.log
import os
@haikusw
haikusw / ViewToPDF.swift
Created January 7, 2023 08:59 — forked from chriseidhof/ViewToPDF.swift
Image Rendering
import SwiftUI
extension View {
@MainActor
func pdf(size: ProposedViewSize) -> Data {
let renderer = ImageRenderer(content: self)
renderer.proposedSize = size
var pdfData = NSMutableData()
renderer.render { size, render in
var mediaBox = CGRect(origin: .zero, size: size)
@haikusw
haikusw / Macro_FCInfoToMouse.FCMacro
Created September 1, 2022 23:11 — forked from mario52a/Macro_FCInfoToMouse.FCMacro
Provide informations coordinates, length and angles in real time on the mouse in a bubble annotation displayed in the 3D screen
# -*- coding: utf-8 -*-
"""
***************************************************************************
* Copyright (c) 2016 <mario52> *
* *
* This file is a supplement to the FreeCAD CAx development system. *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License (LGPL) *
* as published by the Free Software Foundation; either version 2 of *
@haikusw
haikusw / LayoutCallAsFunction.swift
Created July 8, 2022 20:09 — forked from ole/LayoutCallAsFunction.swift
SwiftUI: How any `Layout`-conforming type becomes a container view when it's used with a `@ViewBuilder` closure.
// Excerpt from `SwiftUI.framework/Modules/SwiftUI.swiftmodule/arm64-apple-ios.swiftinterface`
// in Xcode 14.0b1
//
// This is how any `Layout`-conforming type becomes a container view
// when it's used with a `@ViewBuilder` closure.
@available(iOS 16.0, macOS 13.0, tvOS 16.0, watchOS 9.0, *)
extension SwiftUI.Layout {
@_alwaysEmitIntoClient public func callAsFunction<V>(@SwiftUI.ViewBuilder _ content: () -> V) -> some SwiftUI.View where V : SwiftUI.View {
return _VariadicView.Tree(
root: _LayoutRoot(self), content: content())
struct ContentView: View {
var body: some View {
Image(nsImage: NSImage(named: .init(NSImage.applicationIconName))!)
.resizable()
.frame(width: 100, height: 100)
.tapWithHighlight(onTap: {
print("Tap")
}, onLongPress: {
print("Long Press")
})
@haikusw
haikusw / KeyCommand.swift
Created May 24, 2022 23:25 — forked from steipete/KeyCommand.swift
Add Keyboard Shortcuts to SwiftUI on iOS 13 when using `UIHostingController`. Requires using KeyboardEnabledHostingController as hosting class) See https://steipete.com/posts/fixing-keyboardshortcut-in-swiftui/
//
// KeyCommand.swift
// Adds Keyboard Shortcuts to SwiftUI on iOS 13
// See https://steipete.com/posts/fixing-keyboardshortcut-in-swiftui/
// License: MIT
//
// Usage: (wrap view in `KeyboardEnabledHostingController`)
// Button(action: {
// print("Button Tapped!!")
// }) {
@haikusw
haikusw / StringGetSizeThatFits.swift
Created September 7, 2021 20:51 — forked from krzyzanowskim/StringGetSizeThatFits.swift
Calculate frame of String, that fits given width
// Excerpt from https://github.com/krzyzanowskim/CoreTextWorkshop
// Licence BSD-2 clause
// Marcin Krzyzanowski marcin@krzyzanowskim.com
func getSizeThatFits(_ attributedString: NSAttributedString, maxWidth: CGFloat) -> CGSize {
let framesetter = CTFramesetterCreateWithAttributedString(attributedString)
let rectPath = CGRect(origin: .zero, size: CGSize(width: maxWidth, height: 50000))
let ctFrame = CTFramesetterCreateFrame(framesetter, CFRange(), CGPath(rect: rectPath, transform: nil), nil)
import SwiftUI
import Yams
extension String {
var yamlToJSON: String {
do {
guard let parsed = try Yams.load(yaml: self) else { return "" }
let data = try JSONSerialization.data(withJSONObject: parsed, options: [.sortedKeys, .prettyPrinted])
return String(decoding: data, as: UTF8.self)
} catch {
// A URLSession extension that fetches data from a URL and decodes to some Decodable type.
// Usage: let user = try await URLSession.shared.decode(UserData.self, from: someURL)
// Note: this requires Swift 5.5.
extension URLSession {
func decode<T: Decodable>(
_ type: T.Type,
from url: URL,
keyDecodingStrategy: JSONDecoder.KeyDecodingStrategy = .useDefaultKeys,
dataDecodingStrategy: JSONDecoder.DataDecodingStrategy = .deferredToData,
dateDecodingStrategy: JSONDecoder.DateDecodingStrategy = .deferredToDate