UIButton title and image alignment

iPhone SDK Tips & Tricks

[myButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];

Make a Phone Call

iPhone SDK Tips & Tricks


[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel:1-877-829-7902"]];

Multiline UIButton title

iPhone SDK Tips & Tricks

UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
myButton.frame = CGRectMake(200, 30, 100, 90); // size and position of button
[myButton setBackgroundImage:[UIImage imageNamed:@"blue_bars.png"] forState:UIControlStateNormal];
[myButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[myButton setTitle:@"Call Us\nNow" forState:UIControlStateNormal];
myButton.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
myButton.titleLabel.textAlignment = UITextAlignmentCenter;
myButton.titleLabel.font = [UIFont boldSystemFontOfSize:22];
myButton.backgroundColor = [UIColor clearColor];

Color with RGB

iPhone SDK Tips & Tricks

UIColor *myColor = [UIColor colorWithRed:32/255.0 green:93/255.0 blue:137/255.0 alpha:1.0];

Set Date format

iPhone SDK Tips & Tricks

NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease];
[dateFormat setDateFormat:@"dd-MMM-yyyy"];
NSString *myString = [dateFormat stringFromDate:[NSDate date]];

Determine device (iphone or simulator)

iPhone SDK Tips & Tricks

#if !(TARGET_IPHONE_SIMULATOR)
NSLog(@"iphone");
#else
NSLog(@"simulator");
#endif

Get sender title and tag

iPhone SDK Tips & Tricks


- (void)buttonClick:(id )sender{
NSLog(@"tag: %d", [sender tag]);
NSLog(@"title: %@", [sender currentTitle]);
}

Amount of days difference between two dates

iPhone SDK Tips & Tricks

NSDateComponents *compsDisplay = [[NSDateComponents alloc] init];
[compsDisplay setDay:25];
[compsDisplay setMonth:12];
[compsDisplay setYear:2012];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *expDate = [gregorian dateFromComponents:compsDisplay];
[compsDisplay release];

int daysRemaining = [expDate timeIntervalSinceNow]/86400;
//minutes - divide by 60
//hours - divide by 3600

NSString *msg = [NSString stringWithFormat:@"%d days remaining.", dayRemaining];

Transparant webview

UIWebView *myWebView = [[UIWebView alloc] initWithFrame:CGRectMake(15, 10, 300, 10)];
myWebView.backgroundColor = [UIColor clearColor];
[myWebView setOpaque:NO];
myWebView.delegate = self;
[myView addSubview:myWebView];
NSString *bgClear = @"<body style="" background-color: transparent""> bla bla bla";
[myWebView loadHTMLString:bgClear baseURL:baseUrl];
[myWebView sizeThatFits:CGSizeZero];