Archive for September, 2011

Change the status bar to black


[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque];
Change the style of the navigation bar (from in a view controller):
self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
Save a NSString into NSUserDefaults:
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:loginName forKey:kUserLoginName];
Get an NSString from NSUserDefaults:
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
NSString* loginName = [defaults stringForKey:kUserLoginName];

Leave a comment

Show all NSDictionary Key And Value


void describeDictionary (NSDictionary *dict)
{
NSArray *keys;
int i, count;
id key, value;

keys = [dict allKeys];
count = [keys count];
for (i = 0; i < count; i++)
{
key = [keys objectAtIndex: i];
value = [dict objectForKey: key];
NSLog (@”Key: %@ for value: %@”, key, value);
}
}

Leave a comment

change font size uipicker


– (UIView *)pickerView:(UIPickerView *)pickerView
viewForRow:(NSInteger)row
forComponent:(NSInteger)component
reusingView:(UIView *)view {

UILabel *pickerLabel = (UILabel *)view;

if (pickerLabel == nil) {
CGRect frame = CGRectMake(0.0, 0.0, 80, 32);
pickerLabel = [[[UILabel alloc] initWithFrame:frame] autorelease];
[pickerLabel setTextAlignment:UITextAlignmentLeft];
[pickerLabel setBackgroundColor:[UIColor clearColor]];
[pickerLabel setFont:[UIFont boldSystemFontOfSize:15]];
}

[pickerLabel setText:[pickerDataArray objectAtIndex:row]];

return pickerLabel;

Leave a comment

Brazilian Currency Format


NSNumberFormatter *nf = [[[NSNumberFormatter alloc] init] autorelease];
[nf setCurrencyCode:@”BRL”];
[nf setCurrencyDecimalSeparator:@”,”];
[nf setCurrencyGroupingSeparator:@”.”];
[nf setCurrencySymbol:@”R$”];
[nf setNumberStyle:NSNumberFormatterCurrencyStyle];

Leave a comment

email address validation in objective c


– (BOOL) validateEmail: (NSString *) emailstring {
NSString *emailRegex = @”[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\.[A- Za-z]{2,4}”;
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@”SELF MATCHES %@”, emailRegex];

return [emailTest evaluateWithObject:emailstring];
}

Leave a comment

UITextField border and background ( Objective C)


UITextField *theTextFiels=[[UITextField alloc]initWithFrame:CGRectMake(40, 40, 150, 30)];
theTextFiels.borderStyle=UITextBorderStyleNone;
theTextFiels.layer.cornerRadius=8.0f;
theTextFiels.layer.masksToBounds=YES;
theTextFiels.backgroundColor=[UIColor redColor];
theTextFiels.layer.borderColor=[[UIColor blackColor]CGColor];
theTextFiels.layer.borderWidth= 1.0f;

[self.view addSubview:theTextFiels];
[theTextFiels release];

Leave a comment

Play a video full screen in objective c


– (void) playVideo:(NSString *)fileName
{
NSString *url = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:fileName];

playerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:url]];

[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:[playerViewController moviePlayer]];

[self.view addSubview:playerViewController.view];

//play movie

MPMoviePlayerController *player = [playerViewController moviePlayer];
[player play];
}

// The call back
– (void) movieFinishedCallback:(NSNotification*) aNotification {
MPMoviePlayerController *player = [aNotification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];

//player.initialPlaybackTime = -1;
//[player pause];
[player stop];

[player.view removeFromSuperview];

[player release];
// call autorelease the analyzer says call too many times
// call release the analyzer says incorrect decrement
}

Leave a comment

Add a menu item ( Objective C)


// Beware: the keyEquivalent must be lowercase in order not to have the shift modifier
NSMenuItem *menuItem = [[NSMenuItem alloc] initWithTitle:@”Action” action:@selector(menuItemAction:) keyEquivalent:@”r”];
[menuItem setTarget:self];
[menuItem setKeyEquivalentModifierMask:NSCommandKeyMask | NSAlternateKeyMask];
// Add the menu item at the end of menu whose index is 1 (most probably the File menu)
[[[[NSApp mainMenu] itemAtIndex:1] submenu] addItem:menuItem];

Leave a comment

flipping a view on the iphone sdk ( Objective C)


– (void) flipView {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:kTransitionDuration];

[UIView setAnimationTransition:([mainView superview] ?
UIViewAnimationTransitionFlipFromLeft : UIViewAnimationTransitionFlipFromRight)
forView:self.view cache:YES];

if ([flipView superview])
{
[flipView removeFromSuperview];
[self.view addSubview:mainView];
}
else
{
[mainView removeFromSuperview];
[self.view addSubview:flipView];
}

[UIView commitAnimations];
}

Leave a comment

trim whitespace ( Objective C)


str = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; // trim

Leave a comment