Skip to content

Instantly share code, notes, and snippets.

View AndyDentFree's full-sized avatar

Andy Dent AndyDentFree

View GitHub Profile
@IBAction public func onThicknesssliderChanged(_ sender: UISlider) {
let step: Float = 0.25 // using 0.1 causes text render backwards & slanted! Very weird SKLabel internal bug
let roundedValue = roundf(sender.value / step) * step
sender.value = roundedValue // force it to jump in increments
updatePreviewFromThicknessChange()
}
@AndyDentFree
AndyDentFree / onThicknesssliderChanged.swift
Created February 17, 2022 05:01
Code reacting to a slider change with quantised steps
@IBAction public func onThicknesssliderChanged(_ sender: UISlider) {
let step: Float = 0.25 // using 0.1 causes text render backwards & slanted! Very weird SKLabel internal bug
let roundedValue = roundf(sender.value / step) * step
sender.value = roundedValue // force it to jump in increments
updatePreviewFromThicknessChange()
}
@AndyDentFree
AndyDentFree / updatePreviewFromThicknessChange.swift
Created February 17, 2022 04:59
Code reacting to a UI Slider updating line width to outline text.
fileprivate func updatePreviewFromThicknessChange() {
if thicknessAdjuster.value != lineWidth {
thicknessSwatch.setNeedsDisplay() // refresh assuming value adjusted
lineWidth = thicknessAdjuster.value
textStyle.drawStyle = textStyle.drawStyle.butWith(
lineWidthTenths: Int(lineWidth*10))
updateFontStyling()
updateFontDisplay()
}
}
@AndyDentFree
AndyDentFree / makeManyTextExcerpt.swift
Created February 17, 2022 04:51
Sample showing creating a Touchgram from within code, as part of our test suite.
static func makeManyTextVaryingOutlines(workNum: UInt32) -> tgTouchgram {
let fc = tgColor(systemColor: .red)
let ec = tgColor(systemColor: .yellow)
let pt: UInt16 = 48
let lw:[CGFloat] = Array(stride(from: 0.0, through: 4.0, by: 0.5))
return tgTouchgram(title: "Lots of Text Varying Outlines", scenes: [
tgScene(background: tgColorBackground(fill: tgColor(systemColor: .blue)),
triggers: [],
nodes: tgNodeList(nodes: [
tgSimpleTextNode(position: tgNodePosition(anchored: .topLeft, paddingPixels: 8),
@AndyDentFree
AndyDentFree / TextEditorSizingDemo.swift
Created February 9, 2022 06:57
Dynamically sizing a text entry field in an iOS app, depending on keyboard being shown.
// Created by Andy Dent on 9/2/22.
// Copyright © 2022 Touchgram Pty Ltd. All rights reserved.
import Foundation
import UIKit
/// portions showing how the sizing works
class SimpleTextNodeDetailViewController: UIViewController {
@IBOutlet var labelEntryWidthToOverall: NSLayoutConstraint!
@IBOutlet var labelEntryWidthToPreview: NSLayoutConstraint!
@AndyDentFree
AndyDentFree / demoDefer.cpp
Last active August 29, 2015 14:22
Shows how modern C++ can use RAII helper objects to provide the "defer" feature just introduced in Swift 2.0
// shows how C++11 can implement the "defer" introduced in Swift 2.0
#include <iostream>
using simpleClosure = std::function<void()>; // new typedef syntax used for closure
// class only needs to be declared once then used all over to wrap closures
// see http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization
class deferRAII {
public:
deferRAII(simpleClosure cl) : runOnDestruction(cl) {}
@AndyDentFree
AndyDentFree / typeVariantsDemo
Created May 20, 2015 14:31
Working example (from Xamarin Studio) demonstrating Type Variants and discriminated unions
open Microsoft.FSharp.Math
type Shape =
| Circle of radius:float
| Rectangle of width:float * height:float // tuple of float, float
| Triangle of tbase:float * height:float
let area shape =
match shape with
| Circle radius -> Math.PI * radius * radius
| Rectangle (width, height) -> width * height
@AndyDentFree
AndyDentFree / typeVariantsDemo.swift
Created May 20, 2015 15:15
typeVariantsDemo in Swift
// the Swift direct equivalent of the F# gist https://gist.github.com/AndyDentFree/46ac476a31b07d103a03
import Darwin
enum Shape {
case Circle (radius:Double)
case Rectangle (width:Double, height:Double)
case Triangle (tbase:Double, height:Double)
}
func area(shape:Shape) -> Double {
@AndyDentFree
AndyDentFree / safeParentViewController.h
Last active August 29, 2015 14:06
Using Objective-C++ for type-safe walking up parent views
/*
Sometimes in iOS code, you need to be able to walk up the chain of parent views, especially if working on legacy code.
This has been hardcoded in the past with horrible assumptions like this, which broke again in iOS 8:
if (IS_IOS7_AND_UP) {
spdVC = (SuperDuperViewController*)[(UITableView*)self.superview.superview.superview.superview.superview delegate];
} else {
spdVC = (SuperDuperViewController*)[(UITableView*)self.superview.superview.superview delegate];
}