Skip to content

Instantly share code, notes, and snippets.

View kreeger's full-sized avatar

Ben Kreeger kreeger

View GitHub Profile
@kreeger
kreeger / UIButton+Convenience.m
Created December 14, 2012 20:54
Creates a UIButton with a custom view that mimics the style of a UIBarButtonSystemItem.
#import <UIKit/UIKit.h>
@interface UIButton (Convenience)
+ (id)buttonWithBarItemStyleAndCustomView:(UIView *)customView;
@end
@implementation UIButton (Convenience)
@kreeger
kreeger / ColorFunctions.m
Created December 16, 2012 22:25
Tint an image with a particular color, using `kCGBlendModeNormal`.
UIImage *TintImageWithTintColor(UIImage *image, UIColor *tintColor) {
// Huge props to http://stackoverflow.com/questions/3514066/how-to-tint-a-transparent-png-image-in-iphone
UIGraphicsBeginImageContext(image.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, 0, image.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGRect rect = (CGRect){ CGPointZero, image.size };
@kreeger
kreeger / ConfigureLogging.m
Created December 17, 2012 20:14
Today is brought to you by Crayola color names and Objective-C. http://en.wikipedia.org/wiki/List_of_Crayola_crayon_colors
- (void)configureLogging {
[DDLog addLogger:[DDTTYLogger sharedInstance]];
[DDTTYLogger sharedInstance].colorsEnabled = YES;
[DDTTYLogger sharedInstance].logFormatter = [[GCLog alloc] init];
[[DDTTYLogger sharedInstance] setForegroundColor:[UIColor colorWithCrayola:@"Cornflower"]
backgroundColor:nil forFlag:LOG_FLAG_INFO];
[[DDTTYLogger sharedInstance] setForegroundColor:[UIColor colorWithCrayola:@"Screamin' Green"]
backgroundColor:nil forFlag:LOG_FLAG_API];
[[DDTTYLogger sharedInstance] setForegroundColor:[UIColor colorWithCrayola:@"Canary"]
backgroundColor:nil forFlag:LOG_FLAG_UI];
@kreeger
kreeger / CGGeometryLove.m
Created December 19, 2012 19:30
Some additional CGGeometry love.
CGRect CGRectAdjustSize(CGRect rect, CGSize size) {
rect.size = size;
return rect;
}
CGRect CGRectAdjustOrigin(CGRect rect, CGPoint origin) {
rect.origin = origin;
return rect;
}
@kreeger
kreeger / surrogate.rb
Last active September 5, 2021 08:18
Converts a number into a UTF16 surrogate pair.
#!/usr/bin/env ruby
require 'fileutils'
require 'debugger'
class Fixnum
def to_surrogate_pair
if self >= 0x10000 && self <= 0x10FFFF
high = ((self - 0x10000) / 0x400).floor + 0xD800
low = ((self - 0x10000) % 0x400) + 0xDC00
@kreeger
kreeger / uncross.rb
Last active December 10, 2015 23:55
XML => acrosslite.
#!/usr/bin/env ruby
require 'nokogiri'
require 'ostruct'
# Read in the file, convert to UTF-8 if needed, and parse with Nokogiri.
glob = ARGV[0]
exit if glob.nil?
#!/usr/bin/env ruby
# Convert a text crossword into JSON.
require 'active_resource'
ActiveResource::Base
filename = ARGV[0]
data = File.open(filename, 'r') { |f| f.read }
@kreeger
kreeger / UIScrollView+PullToRefresh.m
Last active November 6, 2016 17:15
Adds pull to refresh for a UIScrollView (or a UITableView). Abstracts away differences in frameworks between iOS 6 and iOS 5 (in the case of the latter, ODRefreshControl is used. Attempts to be as similar in fashion as possible to the interface set forth in SVPullToRefresh, with a couple of minor exceptions.
//
// UIScrollView+PullToRefresh.h
// Created by Ben Kreeger (@kreeger), 2013/01/23.
//
#import <UIKit/UIKit.h>
/** Adds pull to refresh for a UIScrollView (or a UITableView). Abstracts away differences in frameworks between iOS 6
* and iOS 5 (in the case of the latter, `ODRefreshControl` is used.
*/
@kreeger
kreeger / translate-strings.rb
Last active December 11, 2015 20:59
Translates an Objective-C .strings file using the free Microsoft Translator API.
#!/usr/bin/env ruby
# Uses the Microsoft Translator API to draft a translated version of an Objective-C strings file.
# USAGE: translate-strings <FILEPATH> <NEW_LANG_CODE>
# NOTE: must have a config.yml file next to this script with `client_id` and `client_secret`
# defined as keys (and their respective values).
require 'uri'
require 'httparty'
require 'nokogiri'
@kreeger
kreeger / do-end-(do).sublime-snippet
Created January 30, 2013 15:53
Ruby Sublime Text bliss.
<snippet>
<content><![CDATA[do
$0
end]]></content>
<tabTrigger>do</tabTrigger>
<scope>source.ruby</scope>
<description>Insert do … end</description>
</snippet>