Skip to content

Instantly share code, notes, and snippets.

//
// RecipePageRenderer.swift
// (c) 2014 Nate Cook, licensed under the MIT License
//
import UIKit
import AVFoundation
/// Units for printing content insets
let POINTS_PER_INCH: CGFloat = 72
import Foundation
extension Collection where Element: Comparable {
func sorted(ascending: Bool = true, using comparator: (Element) -> (Element) -> ComparisonResult) -> [Element] {
return self.sorted { lhs, rhs in
comparator(lhs)(rhs) == (ascending ? .orderedAscending : .orderedDescending)
}
}
}
import Foundation
extension Character {
var isEmoji: Bool {
return unicodeScalars.allSatisfy { $0.properties.isEmoji }
}
}
func recentlyUsedEmoji() -> [Character]? {
#if os(iOS)
import Foundation
struct TruthyValue {
var value: Bool?
init(_ value: Bool?) {
self.value = value
}
init(_ value: Int) {
switch value {
@mattt
mattt / NSHipster-Call-For-Tricks-And-Tips.md
Created November 26, 2012 13:59
NSHipster Call for Tips & Tricks!

Howdy howdy, NSHipsters!

If you alloc init an NSCalendar, you'll notice that New Year's Eve falls on a Monday this year, a.k.a. "the day NSHipster is published every week". What fun!

So in celebration of the upcoming year++, I thought it'd be fun to compile a list of some of your favorite tips and tricks of the trade. Submit your favorite piece of Objective-C trivia, framework arcana, hidden Xcode feature, or anything else you think is cool, and you could have it featured in the year-end blowout article. Just comment on this gist below!

Here are a few examples of the kind of things I'd like to see:

/*
# Problem
In Swift, it can be cumbersome to work with Unicode characters that are
non-printing, confusable, or have difficulty rendering in the editor.
For example, to generate the "Family: Woman, Girl" emoji:
*/
// Option 1: Unicode Scalar Value Escapes
@mattt
mattt / UTTypeForImageData.m
Created March 27, 2014 23:19
A quick function for determining an image's file type by its first couple of bytes
@import MobileCoreServices;
static CFStringRef UTTypeForImageData(NSData *data) {
const unsigned char * bytes = [data bytes];
if (data.length >= 8) {
if (bytes[0] == 0x89 && bytes[1] == 0x50 && bytes[2] == 0x4E && bytes[3] == 0x47 && bytes[4] == 0x0D && bytes[5] == 0x0A && bytes[6] == 0x1A && bytes[7] == 0x0A) {
return kUTTypePNG;
}
}
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns:d="http://www.apple.com/DTDs/DictionaryService-1.0.rng">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<d:entry id="m_en_gb0035330" d:title="apple" class="entry">
<span class="hg">
<span d:dhw="1" role="text" linebreaks="apple" class="hw">apple</span>
@mattt
mattt / UIImageForSwatchOfColorWithSize.h
Created September 27, 2013 01:25
Create a UIImage swatch of a color with a specified size.
static UIImage * UIImageForSwatchOfColorWithSize(UIColor *color, CGSize size) {
UIImage *image = nil;
CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);
UIGraphicsBeginImageContext(rect.size);
{
CGContextRef c = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(c, [color CGColor]);
<h1>Types</h1>
<ul>
<li><a href="./DeclarationCollector">DeclarationCollector</a>:​
A Swift syntax visitor that collects declarations.</li>
<li><a href="./AssociatedType">AssociatedType</a>:​
An associated type declaration.</li>
<li><a href="./Class">Class</a>:​
A class declaration.</li>
<li><a href="./ConditionalCompilationBlock">ConditionalCompilationBlock</a>:​
A conditional compilation block declaration.</li>