Skip to content

Instantly share code, notes, and snippets.

View westerlund's full-sized avatar

Simon Westerlund westerlund

View GitHub Profile
@westerlund
westerlund / NSAttributedString+sizeInWidth.h
Last active August 29, 2015 13:57
Calculate NSString height with multiple attributes
//
// NSAttributedString+sizeInWidth.h
//
// Created by Simon Westerlund on 13/03/14.
// Copyright (c) 2014 Simon Westerlund. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface NSAttributedString (sizeInWidth)
@westerlund
westerlund / keybase.md
Created May 12, 2014 10:34
keybase.md

Keybase proof

I hereby claim:

  • I am westerlund on github.
  • I am westerlund (https://keybase.io/westerlund) on keybase.
  • I have a public key whose fingerprint is 3769 3506 050F 206C 8F5E 28C5 84AC A96F 3E1A DC6F

To claim this, I am signing this object:

----BEGIN PGP MESSAGE-----
Version: GnuPG/MacGPG2 v2.0.22 (Darwin)
Comment: GPGTools - https://gpgtools.org
hQIMA9UKSPis5L98AQ//Q7dblsb52hEkAPbPoBTSeDAwYMI72fsbmM2cZpkT0hnw
whm7xcYBlttjAVctWc/lcte9mSvJMq15V/MkWzFC6haW/fW8pOlgPtKkySqSCFd0
R3Hf+j2cQyFafkTxx0leffRoorqA8w90eIPwA+3YMYpwXGMIA+Dx5AMd1Q73YeG0
0GXb6RnLvVJzLsFpJAw2WYpij+IkQibU0JPWIFuOZPkrXXcuPrVFGpkKgS4QfACP
4CAiDB3SG5svrsMGQSErLHEueP2hGgUGHGJA1VYw+RZ4w5ofmnDkjYCFHBn1Lq/r
@westerlund
westerlund / async.swift
Created July 31, 2014 08:40
Waiting in Swift
// github doesn't support .playground files, just copy this in a playground to quick run it
class AsyncThing {
// set up a optional callback variable that takes a closure with a return of Void
// that closure takes another (non-optional) closure named "done" which returns Void
var callback:((done: () -> Void) -> Void)?
// just a dummy method to execute the blocks
func run() {
// set up a constant block, this is just for readability
//
// RBResizer.swift
// Locker
//
// Created by Hampton Catlin on 6/20/14.
// Copyright (c) 2014 rarebit. All rights reserved.
//
import UIKit
@westerlund
westerlund / gif.swift
Created December 22, 2014 17:07
Create an animated gif in swift
func createGIF(with images: [UIImage], loopCount: Int = 0, frameDelay: Double, callback: (data: NSData?, error: NSError?) -> ()) {
let fileProperties = [kCGImagePropertyGIFDictionary as String: [kCGImagePropertyGIFLoopCount as String: loopCount]]
let frameProperties = [kCGImagePropertyGIFDictionary as String: [kCGImagePropertyGIFDelayTime as String: frameDelay]]
let documentsDirectory = NSTemporaryDirectory()
let url = NSURL(fileURLWithPath: documentsDirectory)?.URLByAppendingPathComponent("animated.gif")
if let url = url {
let destination = CGImageDestinationCreateWithURL(url, kUTTypeGIF, UInt(images.count), nil)
CGImageDestinationSetProperties(destination, fileProperties)
@westerlund
westerlund / valueForKey.swift
Created April 21, 2015 15:08
Attempted to implement ValueForKey in Swift
class SomeClass {
var a: String?
var b: [Int]?
private func _u(named: String, value: AnyObject?, set: Bool) -> AnyObject? {
switch named {
case "a":
if set {
a = value as? String
@westerlund
westerlund / dispatch.swift
Created June 23, 2015 14:08
Dispatch after swift
func dispatch(#after: NSTimeInterval, queue: dispatch_queue_t = dispatch_get_main_queue(), #closure: dispatch_block_t) {
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(Double(after) * Double(NSEC_PER_SEC)))
dispatch_after(time, dispatch_get_main_queue(), closure)
}
func dispatch(#after: NSTimeInterval, closure: dispatch_block_t) {
dispatch(after: after, closure: closure)
}
dispatch(after: 0.5, { () -> Void in
@westerlund
westerlund / NSImageDisk.swift
Created November 29, 2015 10:25
Write NSImage to disk
extension NSImage {
func writeToFile(file: String, atomically: Bool, usingType type: NSBitmapImageFileType) -> Bool {
let properties = [NSImageCompressionFactor: 1.0]
guard
let imageData = TIFFRepresentation,
imageRep = NSBitmapImageRep(data: imageData),
fileData = imageRep.representationUsingType(type, properties: properties) else {
return false
}
return fileData.writeToFile(file, atomically: atomically)
@westerlund
westerlund / cocoa-drawing.swift
Last active November 29, 2015 10:33 — forked from randomsequence/cocoa-drawing.swift
Drawing images with CGContext and NSGraphicsContext in Swift
//: Playground - noun: a place where people can play
import Cocoa
let bounds = CGRectMake(0, 0, 100, 100);
func DrawImageInCGContext(size: CGSize, drawFunc: (context: CGContextRef, rect: CGRect) -> Void) -> NSImage? {
let colorSpace = CGColorSpaceCreateDeviceRGB()
let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.PremultipliedLast.rawValue)
if let context = CGBitmapContextCreate(