Skip to content

Instantly share code, notes, and snippets.

@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.
@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
@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
@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"
@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)
// 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
@eospi
eospi / RealityKitGestures.swift
Created August 13, 2019 16:08
Gestures in RealityKit
import RealityKit
import UIKit
class ViewController: UIViewController {
let arView: ARView
func addGestures() {
// Use any entity. Here we're using a cube with size 0.5m
let entity = ModelEntity(mesh: MeshResource.generateBox(size: 0.5))
entity.generateCollisionShapes(recursive: true)
@verebes1
verebes1 / ColorSupport.swift
Last active August 18, 2021 19:09
Using UIColors from iOS13 with backward compatibility to previous iOS versions.
import UIKit
public enum ColorSupport {
public static var label: UIColor {
if #available(iOS 13, *) {
return .label
}
return UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 1.0)
}
@mecid
mecid / PagerView.swift
Last active August 15, 2024 06:38
PagerView in SwiftUI
//
// PagerView.swift
//
// Created by Majid Jabrayilov on 12/5/19.
// Copyright © 2019 Majid Jabrayilov. All rights reserved.
//
import SwiftUI
struct PagerView<Content: View>: View {
let pageCount: Int
@RoLYroLLs
RoLYroLLs / DeviceConstant.swift
Created December 31, 2019 19:03
A list of all Apple device names easily accessible through enums. particularly useful for PreviewProvider in Swift
//
// DeviceConstants.swift
// SampleApp
//
// Created by RoLY roLLs on 12/31/19.
// Copyright © 2019 RoLYroLLs Enterprises, LLC. All rights reserved.
//
/// Taken from https://developer.apple.com/documentation/swiftui/securefield/3289399-previewdevice