Skip to content

Instantly share code, notes, and snippets.

View jeffbailey's full-sized avatar

Jeff Bailey jeffbailey

View GitHub Profile
@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 / 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 / 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 / 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 / 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 / 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 / gist:f59c737d2a2d9399003e
Created May 2, 2014 18:55
Autolayout Trace command from lldb
po [[UIWindow keyWindow] _autolayoutTrace]
@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 / Multiline UILabel
Created July 28, 2014 20:11
Multiline UILabel in Interface Builder
// The first two lines can be set in Interface builder
self.titleLabel.numberOfLines = 0;
self.titleLabel.lineBreakMode:NSLineBreakByWordWrapping;
[self.titleLabel setPreferredMaxLayoutWidth:self.frame.size.width-50];
// Also in IB, set a constraint on the label so the height it >= the height of a single line
@jeffbailey
jeffbailey / CreateAndCenterButtonWithAutoLayout
Created August 10, 2014 20:51
Create and center a button using Autolayout
self.insertTextButton = [UIButton buttonWithType:UIButtonTypeSystem];
[self.insertTextButton setTitle:@"Press Me" forState:UIControlStateNormal];
[self.insertTextButton sizeToFit];
self.insertTextButton.translatesAutoresizingMaskIntoConstraints = NO;
[self.insertTextButton addTarget:self action:@selector(insertText) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.insertTextButton];
[self.view addConstraint: [NSLayoutConstraint constraintWithItem:self.insertTextButton
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual