Skip to content

Instantly share code, notes, and snippets.

@joninsky
joninsky / UIFont_Extensions.swift
Last active October 18, 2018 03:32
UIFont extensions
extension UIFont {
/// 1)
/// Function that will register a new `UIFont` from the provided bundle usign Core Graphics. If the font can't be registered a fatal error is thrown. We throw a fatal error because we want to know right away if our font's don't load. Maybe a more appropriate way is to throw `Error`'s but this removes a lot of optional unwrapping code when using the font's as they can be used as non optional values.
///
/// - Parameters:
/// - bundle: The bundle the font file lives in. Defaults to `Bundle.main`
/// - fontName: The name of the font file.
/// - fontExtension: The extension of the font file
static public func registerFont(bundle: Bundle? = Bundle.main, fontName: String, fontExtension: String) {
guard let fontURL = bundle?.url(forResource: fontName, withExtension: fontExtension) else {
@joninsky
joninsky / IconFont.swift
Last active October 18, 2018 04:29
Struct that represents the necessary functions and properties to get started using the IcoMoon free pack.
import UIKit
/// Struct that lets you access the linear icons in code.
struct IconFont {
//MARK: Properties
/// The name of the IcoMoon-Free file used ot register the font.
let iconFontName = "IcoMoon-Free"
/// Upstanding up arrow
let arrow_up = "\u{ea3a}"
@joninsky
joninsky / ViewController.swift
Created October 18, 2018 04:07
Shows basic use of Icon Font on a UILabel. Also demonstrates basic constraints in code.
import UIKit
class RootViewController: UIViewController {
//MARK: Properties
let label = UILabel(frame: .zero)
//MARK: Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
//Set up label
@joninsky
joninsky / ImageFromIconFont.swift
Created October 18, 2018 04:24
Function that will take a unicode character from the Linear Icon set and turn it into a `UIImage`
/// Function that will take a unicode character from the Linear Icon set and turn it into a `UIImage`
///
/// - Parameters:
/// - unicode: The unicode character (Icon) to turn into an image
/// - color: The color you want the Icon to be
/// - size: The size you want the Icon to be
/// - Returns: Gives you a crisp `UIImage`
public func image(fromUnicode unicode: String, color: UIColor, size: CGFloat) -> UIImage {
let attributes: [NSAttributedString.Key: Any] = [NSAttributedString.Key.font: self.iconFont(size), NSAttributedString.Key.backgroundColor: UIColor.clear, NSAttributedString.Key.foregroundColor: color]
let S = CGSize(width: size, height: size)
@joninsky
joninsky / ApolloController.swift
Last active December 13, 2018 01:43
Apollo Controller
import Apollo
struct ApolloController {
//MARK: Properties
//The Apollo graph QL Root object
var apollo: ApolloClient?
//MARK: Init
init() {
@joninsky
joninsky / LazyApollo.swift
Created December 13, 2018 01:51
Lazy Apollo
@UIApplicationMain
class AppDelegate: UIResponder {
/// The window object.
var window: UIWindow?
/// The Apollo Controller.
lazy var apolloController = ApolloController()
}
extension AppDelegate: UIApplicationDelegate {
protocol ApolloControllerAvailable {
var apolloController: ApolloController? { get }
}
extension ApolloControllerAvailable {
var apolloController: ApolloController? {
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
return nil
}
return appDelegate.apolloController
@joninsky
joninsky / LoginViewController.swift
Created December 13, 2018 02:06
Conforming view controller
class LoginViewController: UIViewController, ApolloControllerAvailable {
//MARK: Properties
//MARK: Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
//We have access!!
self.apolloController
}
}
@joninsky
joninsky / CosineSimilarity.swift
Last active February 6, 2024 16:05
Cosine Similarity in Swift
/** Cosine similarity **/
private func cosineSim(A: [Double], B: [Double]) -> Double {
return dot(A: A, B: B) / (magnitude(A: A) * magnitude(A: B))
}
/** Dot Product **/
private func dot(A: [Double], B: [Double]) -> Double {
var x: Double = 0
for i in 0...A.count-1 {
x += A[i] * B[i]
@joninsky
joninsky / EuclideanDistance.swift
Created January 11, 2019 00:50
Euclidean Distance between two arrays of Double values in Swift
func euclideanDistance(first: [Double], second: [Double]) -> Double {
guard first.count == second.count else {
fatalError("Vector space is not the same")
}
var result: Double = 0
for i in 0..<first.count {
result += pow(first[i] - second[i], 2.0)
}
return sqrt(result)
}