Skip to content

Instantly share code, notes, and snippets.

@dfelber
dfelber / NumericRemap.swift
Last active March 20, 2016 14:03
Convert a value from one number range into another one. e.g.: 50.remap(from: (min: 0.0, max: 100.0), to: (min: 0.0, max: 1.0)) => 0.5
protocol Numeric: SignedNumberType
{
func * (lhs: Self, rhs: Self) -> Self
func + (lhs: Self, rhs: Self) -> Self
func - (lhs: Self, rhs: Self) -> Self
func / (lhs: Self, rhs: Self) -> Self
}
extension Numeric
{
@dfelber
dfelber / BouncyButton.swift
Last active December 6, 2016 15:44
Bouncy UIButton in swift. Becomes small when pressed and restores its size when released. Swift 2
//
// BouncyButton.swift
//
// Created by Domink Felber on 20.03.16.
// Copyright © 2016 Domink Felber. All rights reserved.
//
import Foundation
import UIKit
@dfelber
dfelber / String+Score.swift
Last active April 7, 2016 09:29
Calculates a score describing how good two strings match.
//
// String+Score
//
// Created by Dominik Felber on 07.04.2016.
// Copyright (c) 2016 Dominik Felber. All rights reserved.
//
// Based on the Objective-C implementation of Nicholas Bruning https://github.com/thetron/StringScore
// and the original JavaScript implementation of Joshaven Potter https://github.com/joshaven/string_score
//
import Foundation
import UIKit
@IBDesignable
class PatternImageView: UIImageView
{
@IBInspectable var repeats: Bool = true
{
didSet {
@dfelber
dfelber / PrintFonts.swift
Last active May 11, 2016 14:55
Prints a list of all available fonts
import UIKit
for family: String in UIFont.familyNames() {
print("\(family)")
for names: String in UIFont.fontNamesForFamilyName(family) {
print("\t\(names)")
}
print("")
@dfelber
dfelber / UIViewInPlayground.swift
Created May 15, 2016 16:50
Minimal setup to show UIView in a Xcode Playground.
import UIKit
import XCPlayground
let someView = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
XCPlaygroundPage.currentPage.liveView = someView
@dfelber
dfelber / Color+Hex.swift
Last active August 6, 2021 10:05
Extension for UIColor and NSColor to initialize them with hexadecimal values like `0xcf0b69`
#if os(OSX)
import AppKit
typealias Color = NSColor
#elseif os(iOS) || os(tvOS)
import UIKit
typealias Color = UIColor
#endif
extension Color
@dfelber
dfelber / CarrierInfo.swift
Created June 14, 2016 14:53
Get carrier info from your iOS Device
import CoreTelephony
let info: CTTelephonyNetworkInfo = CTTelephonyNetworkInfo()
guard let carrier: CTCarrier = info.subscriberCellularProvider else {
// No carrier info available
return
}
print(carrier.carrierName)
print(carrier.mobileCountryCode)
@dfelber
dfelber / NSDate+Comparable.swift
Last active August 26, 2016 06:58
Compare NSDate with comparison operators
public func <(a: NSDate, b: NSDate) -> Bool {
return a.compare(b) == NSComparisonResult.OrderedAscending
}
public func >(a: NSDate, b: NSDate) -> Bool {
return a.compare(b) == NSComparisonResult.OrderedDescending
}
@dfelber
dfelber / Array+RandomValue.swift
Last active September 2, 2016 07:01
Returns a random value from an array
extension Array {
func randomValue() -> Element? {
guard self.isEmpty == false else {
return nil
}
let idx = Int(arc4random() % UInt32(count))
return self[idx]
}
}