Skip to content

Instantly share code, notes, and snippets.

View chrishulbert's full-sized avatar

Chris Hulbert chrishulbert

View GitHub Profile
@chrishulbert
chrishulbert / line.js
Created July 11, 2011 06:34
Reading a line from the console in node.js
console.log ('Input some code:');
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.once('data', function (someCode) {
process.stdin.pause();
console.log ('Code: ' + someCode);
});
@chrishulbert
chrishulbert / trim_whitespace_vc.m
Created July 9, 2011 22:08
Trim whitespaces on all text fields in a view controller
// Trim them
for (UIView* v in self.view.subviews) {
if ([v isKindOfClass:[UITextField class]]) {
UITextField* tf = (id)v;
tf.text = [tf.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
}
}
@chrishulbert
chrishulbert / BabyTapGesture.m
Created June 21, 2011 05:07
Baby tap gesture - like UITapGestureRecognizer but accepts any number of fingers
// BabyTapGesture.h
// Created by Chris ([email protected]) on 21/06/11.
#import <Foundation/Foundation.h>
#import <UIKit/UIGestureRecognizerSubclass.h>
@interface BabyTapGesture : UIGestureRecognizer
@end
----
@chrishulbert
chrishulbert / WeakReferenceSet.m
Created June 21, 2011 04:18
NSMutableSet with weak references eg doesn't retain added objects
// WeakReferenceSet.h
// Created by [email protected] on 21/06/11.
#import <Foundation/Foundation.h>
@interface WeakReferenceSet : NSObject
+ (WeakReferenceSet*)set;
- (void)addObject:(id)object;
- (void)removeObject:(id)object;
@chrishulbert
chrishulbert / colourise.m
Created June 19, 2011 11:47
Image manipulation in obj-c / iphone
typedef unsigned char byte;
#define Clamp255(a) (a>255 ? 255 : a)
+ (UIImage*) fromImage:(UIImage*)source toColourR:(int)colR g:(int)colG b:(int)colB {
// Thanks: http://brandontreb.com/image-manipulation-retrieving-and-updating-pixel-values-for-a-uiimage/
CGContextRef ctx;
CGImageRef imageRef = [source CGImage];
NSUInteger width = CGImageGetWidth(imageRef);
NSUInteger height = CGImageGetHeight(imageRef);
@chrishulbert
chrishulbert / MemoryWarnings.m
Created June 1, 2011 03:46
Simulating iOS memory warnings
#if TARGET_IPHONE_SIMULATOR
#import <objc/objc-class.h>
// http://cocoawithlove.com/2008/02/imp-of-current-method.html
IMP impOfCallingMethod(id lookupObject, SEL selector)
{
NSUInteger returnAddress = (NSUInteger)__builtin_return_address(0);
NSUInteger closest = 0;
// Iterate over the class and all superclasses
@chrishulbert
chrishulbert / close_actionsheet_above_tap.m
Created May 30, 2011 05:34
Allow closing a UIActionSheet by tapping above it, in the shaded area
// For detecting taps outside of the alert view
-(void)tapOut:(UIGestureRecognizer *)gestureRecognizer {
CGPoint p = [gestureRecognizer locationInView:self];
if (p.y < 0) { // They tapped outside
[self dismissWithClickedButtonIndex:0 animated:YES];
}
}
-(void) showFromTabBar:(UITabBar *)view {
[super showFromTabBar:view];
@chrishulbert
chrishulbert / uibutton_on_uitoolbar.m
Created May 30, 2011 04:48
Putting a UIButton on a UIToolbar
UIButton *myBn = [UIButton buttonWithType:UIButtonTypeCustom];
[myBn setTitle:@"Blah" forState:UIControlStateNormal];
myBn.frame = CGRectMake(0, 0, 100, 44);
[myBn addTarget:self action:@selector(myBnWasClicked:) forControlEvents:UIControlEventTouchUpInside];
myBn.titleLabel.font = [UIFont fontWithName:@"Some Font" size:16];
[myBn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
myBn.showsTouchWhenHighlighted = YES;
UIBarButtonItem *myBbi = [[[UIBarButtonItem alloc] initWithCustomView:sortCustomButton] autorelease];
@chrishulbert
chrishulbert / curvisview.m
Created May 25, 2011 05:00
Finding the currently visible view in a UITabBarController
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Firstly, we want to figure out who is the currently visible view controller.
// There are 4 possibilities here:
// 1- They could be a non-nav controller, with a visible tab (ie not under the 'more' tab)
// Simple: self.selectedViewController
// 2- They could be a nav controller, with a visible tab (ie not under the 'more' tab)
// Get it's visible view: self.selectedViewController.visibleViewController
// 3- They could be a non-nav controller, under the 'more' tab
// Simple: self.selectedViewController
// 4- They could be a nav controller, under the 'more' tab
@chrishulbert
chrishulbert / centeredbuttoninnavbar.m
Created May 17, 2011 06:11
Centered UIButton in a navigation bar on the iphone
UIButton* centeredNavBarButton = [UIButton buttonWithType:UIButtonTypeCustom];
centeredNavBarButton.frame = CGRectMake(0, 0, 150, 31);
[centeredNavBarButton setBackgroundImage:[UIImage imageNamed:@"mybutton_normal"] forState:UIControlStateNormal];
[centeredNavBarButton setBackgroundImage:[UIImage imageNamed:@"mybutton_selected"] forState:UIControlStateSelected];
[centeredNavBarButton setTitle:@"Blah blah" forState:UIControlStateNormal];
[centeredNavBarButton addTarget:self action:@selector(myActionToCallWhenTapped) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.titleView = centeredNavBarButton;