Skip to content

Instantly share code, notes, and snippets.

@algal
algal / UIColor+Hex.swift
Last active August 29, 2015 14:20
UIColor from hex, and hex is a number dammit.
extension UIColor
{
convenience init(RGBValue:UInt32)
{
let red = (RGBValue & 0xFF0000) >> 16
let green = (RGBValue & 0x00FF00) >> 8
let blue = (RGBValue & 0x0000FF) >> 0
self.init(red:CGFloat(red)/255.0,green:CGFloat(green)/255.0,blue:CGFloat(blue)/255.0,alpha:CGFloat(1.0))
}
}
@algal
algal / CenteredTagCloudView.swift
Last active November 26, 2015 14:39
CenteredTagCloudView playground code
//
// MultilineLabelCloudView.swift
//
//
import UIKit
/**
Presents a wrapping grid of tokens presented as pills.
@algal
algal / GenericTableViewCell.swift
Last active August 29, 2015 14:22
Broken generic table view cell
/// Not sure why this generic version does not work.
private class WrapperTableViewCell<T:UIView> : UITableViewCell
{
class var classReuseIdentifier:String { return T.self.description() + "CellIdentifier" }
override class func requiresConstraintBasedLayout() -> Bool { return true }
override init(style: UITableViewCellStyle, reuseIdentifier: String?)
{
super.init(style: UITableViewCellStyle.Default, reuseIdentifier: reuseIdentifier)
@algal
algal / ViewWrappingTableViewCell.swift
Last active February 2, 2016 12:37
Abstract UITableViewCell class for quickly defining UITableViewCell subclasses that merely wrap a custom UIView subclass
// ViewWrappingTableViewCell.swift
// Created by Alexis Gallagher on 6/4/15.
import UIKit
/**
Abstract superclass for quickly defining a `UITableViewCell` subclass that wraps a
specific `UIView` subclass. The resulting `UITableViewCell` will hug the wrapped view
with layout constraints, so that both the cell and the wrapped view can influence
@algal
algal / FontUtils.swift
Last active January 12, 2018 18:43
Type-safe font initiazer
//
// MARK: Font utilities
//
enum FontFamily : String {
case HelveticaNeue = "Helvetica Neue"
case MontSerrat = "Montserrat"
}
enum FontWeight {
@algal
algal / NSString+RelativePaths.m
Created June 16, 2015 22:53
stringRelativePath
//
// NSString+RelativePaths.m
#import "NSString+RelativePaths.h"
/**
Returns a path to resourcePath, relative to anchorPath.
@algal
algal / JSONService.swift
Created June 17, 2015 17:31
Helper to encapsulate loading JSON over the network.
//
// MARK: generic networking and JSON-decoding helper
//
// give it a NSURLRequest and a session, it gives you back a aresult with an AnyObject or an NSError
public class JSONService
{
/// returns JSON result on statusCode 200, and an error otherwise
public class func taskWithJSONRequest(
@algal
algal / URLComponentsHelpers.swift
Created June 17, 2015 19:18
Fluent interface for URLComponents
private extension NSURLComponents
{
func withQueryItem(name:String,intValue:Int) -> NSURLComponents {
return self.withQueryItem(name, stringValue: NSNumber(integer: intValue).stringValue)
}
func withQueryItem(name:String,floatValue:Float) -> NSURLComponents {
return self.withQueryItem(name, stringValue: NSNumber(float: floatValue).stringValue)
}
@algal
algal / NSDate+RFC3339.swift
Last active January 11, 2019 21:28
NSDate+RFC3339
// known-good: Xcode 7.3 (Swift 2.2)
import Cocoa
private var rfc3339formatter:NSDateFormatter = {
let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z"
formatter.timeZone = NSTimeZone(forSecondsFromGMT: 0)
formatter.calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierISO8601)!
formatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
@algal
algal / diffs.swift
Last active July 30, 2018 09:15
diff calculator
// swift 1.2
/**
:param: old array of items
:param: new array of items
:return: a tuple where with the following components:
- `newIndexesAdded` indexes in new, for elements that did not exist in old
- `oldIndexesDeleted` indexes in old, for elements not in new