Skip to content

Instantly share code, notes, and snippets.

// The SwiftUI Lab: https://swiftui-lab.com
// Article: Inspecting the View Tree – Part 2
// https://swiftui-lab.com/communicating-with-the-view-tree-part-2/
import SwiftUI
struct MyTextPreferenceData {
let viewIdx: Int
var topLeading: Anchor<CGPoint>? = nil
var bottomTrailing: Anchor<CGPoint>? = nil
@scottmatthewman
scottmatthewman / AdaptsToSoftwareKeyboard.swift
Last active April 19, 2024 12:56
An example of using Combine to automatically adapt a SwiftUI scrollable view to accommodate an iOS onscreen keyboard
import SwiftUI
import Combine
struct AdaptsToSoftwareKeyboard: ViewModifier {
@State var currentHeight: CGFloat = 0
func body(content: Content) -> some View {
content
.padding(.bottom, currentHeight)
.edgesIgnoringSafeArea(.bottom)
@stinger
stinger / CombineFetcher.swift
Last active January 28, 2023 18:07
Combine - fetching data using URLSession publishers
import Foundation
import Combine
enum APIError: Error, LocalizedError {
case unknown, apiError(reason: String)
var errorDescription: String? {
switch self {
case .unknown:
return "Unknown error"
@ozgurshn
ozgurshn / NLTokenizer.swift
Created March 10, 2019 19:59
NLTokenizer
import NaturalLanguage
let text = "All human beings are born free and equal in dignity and rights. They are endowed with reason and conscience and should act towards one another in a spirit of brotherhood."
let tokenizer = NLTokenizer(unit: .word)
tokenizer.string = text
//let tokenArray = tokenizer.tokens(for: strRange)
tokenizer.enumerateTokens(in: text.startIndex..<text.endIndex) { tokenRange, _ in
print(text[tokenRange])
return true
@hlung
hlung / PlaceholderTextView.swift
Created November 27, 2018 10:41
A UITextView subclass with placeholder text support. Swift 4.2
import UIKit
/// A UITextView subclass with placeholder text support.
/// It uses another UILabel to show the placeholder, shown when text is empty.
class PlaceholderTextView: UITextView {
lazy var placeholderLabel: UILabel = {
let label = UILabel()
label.textColor = UIColor(white: 0.5, alpha: 0.85)
label.backgroundColor = .clear
@warrenm
warrenm / ARQLThumbnailGenerator.swift
Created June 12, 2018 06:31
Generating thumbnail images of 3D model files for use with AR QuickLook
import Foundation
import SceneKit
class ARQLThumbnailGenerator {
private let device = MTLCreateSystemDefaultDevice()!
/// Create a thumbnail image of the asset with the specified URL at the specified
/// animation time. Supports loading of .scn, .usd, .usdz, .obj, and .abc files,
/// and other formats supported by ModelIO.
@UglyBlueCat
UglyBlueCat / XCUIElement+GentleSwipe.swift
Created January 19, 2018 15:32
Recently I found that XCUIElement.swipeUp() was far too violent, swiping way past the element in the UI I wanted my test to tap, so I wrote an extension to XCUIElement that swiped gently using XCUIElement.press(forDuration:thenDragTo:)
//
// XCUIElement+GentleSwipe.swift
//
// Created by Robin Spinks on 11/10/2017.
//
import Foundation
import XCTest
extension XCUIElement
@inamiy
inamiy / struct-CodingKeys
Created June 21, 2017 08:02
Swift 4 Decodable + `struct CodingKeys`
import Foundation
struct User: Decodable {
let name: String
let age: Int?
// private enum CodingKeys: String, CodingKey {
// case name
// case NAME
// }
@ylem
ylem / CenterItemInCollectionView.playground
Last active October 22, 2024 04:04
Scrolling item on a horizontal UICollectionView, stopped item on center of screen.
//: Playground - noun: a place where people can play
import UIKit
import PlaygroundSupport
class WLCollectionCell: UICollectionViewCell {
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
@aaadonai
aaadonai / DocumentType.swift
Created April 19, 2016 01:02
A Swift enum that recognizes basic mimetypes in a NSData
// Inspired by: http://stackoverflow.com/questions/4147311/finding-image-type-from-nsdata-or-uiimage/5042365#5042365
import Foundation
enum DocumentType: String {
case jpeg = "image/jpeg"
case png = "image/png"
case gif = "image/gif"
case tiff = "image/tiff"