Skip to content

Instantly share code, notes, and snippets.

@waterskier2007
waterskier2007 / penrose.swift
Created January 14, 2021 04:25
A swift implementation of Penrose tiling
//MARK: - Playground code
//: A UIKit based Playground for presenting user interface
import UIKit
import PlaygroundSupport
class MyViewController : UIViewController {
override func loadView() {
let view = PenroseView(frame: CGRect(x: 0, y: 0, width: 1000, height: 800))
@gk-plastics
gk-plastics / SetTextPrependedBySFSymbols.swift
Created April 22, 2020 15:30
UILabel extension that sets text prepended by a SF Symbols image
extension UILabel {
func setText(_ text: String, prependedBySymbolNameed symbolSystemName: String, font: UIFont? = nil) {
if #available(iOS 13.0, *) {
if let font = font { self.font = font }
let symbolConfiguration = UIImage.SymbolConfiguration(font: self.font)
let symbolImage = UIImage(systemName: symbolSystemName, withConfiguration: symbolConfiguration)?.withRenderingMode(.alwaysTemplate)
let symbolTextAttachment = NSTextAttachment()
symbolTextAttachment.image = symbolImage
let attributedText = NSMutableAttributedString()
attributedText.append(NSAttributedString(attachment: symbolTextAttachment))
abandoned
able
absolute
adorable
adventurous
academic
acceptable
acclaimed
accomplished
accurate
@iandundas
iandundas / String+emoji.swift
Last active April 23, 2022 16:30
Random Emoji
// Returns a random Emoji 🌿
extension String{
func randomEmoji()->String{
let range = 0x1F601...0x1F64F
let ascii = range.startIndex + Int(arc4random_uniform(UInt32(range.count)))
let emoji = String(UnicodeScalar(ascii))
return emoji
}
}
@jkosoy
jkosoy / CGSize+AspectFunctions.swift
Last active June 3, 2023 18:11
Aspect Fill and Aspect Fit calculations in Swift
// port of http://stackoverflow.com/a/17948778/3071224
import UIKit
import Foundation
extension CGSize {
static func aspectFit(aspectRatio : CGSize, var boundingSize: CGSize) -> CGSize {
let mW = boundingSize.width / aspectRatio.width;
let mH = boundingSize.height / aspectRatio.height;
@proxpero
proxpero / Permutations.swift
Last active September 18, 2023 13:17
Generate the permutations of a Swift array.
//: Permutations
// based on https://www.objc.io/blog/2014/12/08/functional-snippet-10-permutations/
// but updated for Swift 2.0 (Xcode 7.0)
extension Array {
func decompose() -> (Generator.Element, [Generator.Element])? {
guard let x = first else { return nil }
return (x, Array(self[1..<count]))
}
}
@adunsmoor
adunsmoor / gist:e848356a57980ab9f822
Last active December 17, 2018 08:45
closed convex hull in Swift
import UIKit
// Returns a list of points on the convex hull in counter-clockwise order.
// Note: the last point in the returned list is the same as the first one.
//
// https://en.wikibooks.org/wiki/Algorithm_Implementation/Geometry/Convex_hull/Monotone_chain
//
func closedConvexHull(points_ : [CGPoint]) -> [CGPoint] {
// 2D cross product of OA and OB vectors, i.e. z-component of their 3D cross product.