Skip to content

Instantly share code, notes, and snippets.

View jeffbailey's full-sized avatar

Jeff Bailey jeffbailey

View GitHub Profile
@jeffbailey
jeffbailey / ResignFirstResponder
Created May 15, 2014 01:02
Generically resign the first responder (i.e., dismiss keyboard)
[[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:self forEvent:nil]
@jeffbailey
jeffbailey / gist:f59c737d2a2d9399003e
Created May 2, 2014 18:55
Autolayout Trace command from lldb
po [[UIWindow keyWindow] _autolayoutTrace]
@jeffbailey
jeffbailey / WeakStrongDance
Created April 28, 2014 15:27
Example of capturing a weak reference in a block and converting to a strong reference when the block executes.
__weak typeof(self) weakSelf = self;
[self doABlockOperation:^{
__strong typeof(weakSelf) strongSelf = weakSelf;
if (strongSelf) {
...
}
}];
@jeffbailey
jeffbailey / HeightForNSString
Created March 30, 2014 11:06
Find the height of a string to dynamically determine the height of a label. New in iOS 7.
+ (CGSize)labelSizeForString:(NSString *)string fontSize:(CGFloat)fontSize
{
return [string boundingRectWithSize:CGSizeMake(DETAIL_TEXT_LABEL_WIDTH, 2000.0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:fontSize]} context:nil].size;
}
@jeffbailey
jeffbailey / TARGET_IPHONE_SIMULATOR
Created March 20, 2014 12:40
Test code that only runs in the simulator: TARGET_IPHONE_SIMULATOR
#ifdef TARGET_IPHONE_SIMULATOR
// Code to only run in the simulator
#endif
@jeffbailey
jeffbailey / UISegmentedControlMask
Created February 17, 2014 17:50
Depending on your background it may be necessary mask the background of a UISegmentedControl in iOS 7.
CAShapeLayer* mask = [[CAShapeLayer alloc] init];
mask.frame = CGRectMake(0, 0, segmentedControl.bounds.size.width-1, segmentedControl.bounds.size.height);
mask.path = [[UIBezierPath bezierPathWithRoundedRect:mask.frame cornerRadius:4] CGPath];
segmentedControl.layer.mask = mask;
@jeffbailey
jeffbailey / moveButtonImageToRight
Created February 10, 2014 20:08
Move a UIButtonImage to the right hand side to appear after the label
button.imageEdgeInsets = UIEdgeInsetsMake(0., button.frame.size.width - (image.size.width + 15.), 0., 0.);
button.titleEdgeInsets = UIEdgeInsetsMake(0., 0., 0., image.size.width);
@jeffbailey
jeffbailey / BitsExample
Last active August 29, 2015 13:56
Example of setting, getting, toggling and clearing bits. Source: http://iosdevelopertips.com/c/creating-bit-fields-in-c-and-objective-c.html
#define BIT_ONE 1
#define BIT_TWO 2
#define BIT_THREE 4
#define BIT_FOUR 8
int flag = 0;
// Set bits
flag |= BIT_ONE | BIT_THREE;
NSLog(@"Bits: %@", [self intToBinaryString:flag]);
@jeffbailey
jeffbailey / CircleUIImageViewMask
Created January 25, 2014 12:25
Mask an UIImageView to a circle.
- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
self.imageView.layer.masksToBounds = YES;
self.imageView.layer.cornerRadius = self.imageView.bounds.size.width / 2.;
}
@jeffbailey
jeffbailey / CropUIImage
Created January 22, 2014 18:52
Crop a UIImage
CGImageRef image = CGImageCreateWithImageInRect([orginalImage CGImage],cropRect);
UIImage *cropedImage = [UIImage imageWithCGImage:image];
CGImageRelease(image);