Skip to content

Instantly share code, notes, and snippets.

@alexpersian
alexpersian / UITextView+Truncated.swift
Last active August 21, 2023 03:22
Extension on UITextView that allows for the detection of truncation.
extension UITextView {
// Massive credit to Dave Delong for his extensive help with this solution.
/// Returns whether or not the `UITextView` is displaying truncated text. This includes text
/// that is visually truncated with an ellipsis (...), and text that is simply cut off through
/// word wrapping.
///
/// - Important:
/// This only works properly when the `NSLineBreakMode` is set to `.byTruncatingTail` or `.byWordWrapping`.
///
/// - Remark:
func processHTML(_ string: String) -> NSAttributedString {
let stringData = string.data(using: .utf8)!
let attrString = try! NSMutableAttributedString(
data: stringData,
options: [.documentType: NSAttributedString.DocumentType.html],
documentAttributes: nil
)
var boldRanges: [NSRange] = []
var italicRanges: [NSRange] = []
#!/bin/bash
# Credit to Joel Ekström at Spotify for original creation.
# USAGE:
# 1. Replace `{ insert repo url here }` at the bottom with your GH repo's URL
# 2. `chmod +x open-xcode-selection-in-gh.sh` to make it executable
# 3. Create a new Behaviour in Xcode settings. In the bottom, select "Run script", and choose this file
# 4. Next to your new behaviour in the list, click the cmd symbol to add to keyboard shortcut
@alexpersian
alexpersian / GlowingPillView.swift
Created January 19, 2021 22:29
Rough attempt at making a glowing pill style view
//: A UIKit based Playground for presenting user interface
import UIKit
import PlaygroundSupport
class MyViewController : UIViewController {
override func loadView() {
let pillView = UIView(frame: CGRect(x: 64, y: 64, width: 32, height: 128))
pillView.backgroundColor = .white
pillView.layer.cornerRadius = pillView.frame.width / 2
@alexpersian
alexpersian / wasd_rule.json
Last active January 16, 2021 20:22 — forked from icanswiftabit/rule.json
Arrow pad with caps_lock as toggle key
{
"title": "WASD Arrow Keys",
"rules": [
{
"description": "WASD Mode [Capslock as trigger key]",
"manipulators": [
{
"type": "basic",
"from": {
"key_code": "caps_lock"
//
// TurnipCalculator
//
// Translated from https://gist.github.com/Treeki/85be14d297c80c8b3c0a76375743325b#file-turnipprices-cpp
//
// TODO: Calculation is slightly off from C++ impl still
import Foundation
// MARK: Lighten Blend Filter
let lightenFilter = CIFilter(name: "CILightenBlendMode")!
let solidDarkColorImage = CIImage(color:CIColor(red: 0.09, green: 0.14, blue: 0.32))
lightenFilter.setValuesForKeys([
"inputImage": multiplyFilter.outputImage!,
"inputBackgroundImage": solidDarkColorImage
])
// MARK: Multiply Blend Filter
let multiplyFilter = CIFilter(name: "CIMultiplyBlendMode")!
let solidLightColorImage = CIImage(color: CIColor(red: 0.33, green: 0.98, blue: 0.25))
multiplyFilter.setValuesForKeys([
"inputImage": grayscaleFilter.outputImage!,
"inputBackgroundImage": solidLightColorImage
])
// MARK: Grayscale Filter
let grayscaleFilter = CIFilter(name: "CIPhotoEffectMono")!
grayscaleFilter.setValue(CIImage(cgImage: image.cgImage!), forKey: kCIInputImageKey)
@alexpersian
alexpersian / BoundingBox.swift
Last active January 15, 2020 02:12
ViewController class that will render a shape and then on-tap will draw a bounding box around that shape.
//
// ViewController with a single shape drawn inside it.
// Tapping on the shape will cause a red minimum-bounding box to be drawn
// around the shape's bounds.
//
// Included as a single monofile for the sake of including it all within a single gist.
// Core logic centers around the Flood Fill algorithm with an interative, BFS approach used.
//
import UIKit