Pages

Subscribe:

Tuesday, January 11, 2011

Creating a custom alert

//Background of AlertView is an image And you can change this image

UIAlertView *theAlert = [[[UIAlertView alloc] initWithTitle:@"Your Title"
message: @"Your Message Here"
delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];

[theAlert show];

UILabel *theTitle = [theAlert valueForKey:@"_titleLabel"];
[theTitle setTextColor:[UIColor redColor]];

UILabel *theBody = [theAlert valueForKey:@"_bodyTextLabel"];
[theBody setTextColor:[UIColor blueColor]];

UIImage *theImage = [UIImage imageNamed:@"Background.png"];
theImage = [theImage stretchableImageWithLeftCapWidth:16 topCapHeight:16];
CGSize theSize = [theAlert frame].size;

UIGraphicsBeginImageContext(theSize);
[theImage drawInRect:CGRectMake(0, 0, theSize.width, theSize.height)];
theImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

[[theAlert layer] setContents:[theImage CGImage]];

Friday, January 7, 2011

validating & limit the character length of a TextField

*** This is a delegate method for UITextField


- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSCharacterSet *invalidCharSet = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789"] invertedSet];
NSString *filtered = [[string componentsSeparatedByCharactersInSet:invalidCharSet] componentsJoinedByString:@""];

if (textField.text.length >= MAX_LENGTH && range.length == 0)
{
return NO; // return NO to not change text
}
else
{
if(![string isEqualToString:filtered])
return NO;
else
return YES;
}
}

Wednesday, January 5, 2011

Using a custom image button in a navigation bar

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:@"myImage.png"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(blah) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(0, 0, 32, 32)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];

***the title of the nav controller will be auto resized this way. If you do it your way (subview of the nav bar) the text will go behind or ontop of the image

***it will also stay on the screen as you pop and push view controllers.

***once again, you can use an imageview as the custom view and not the button. Here is code:


UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,32,32)];
[iv setImage:[UIImage imageNamed:@"myImage.png"]];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:iv];
[iv release];

Vibrate iPhone device through application

-(void)vibrate
{
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}

Vibrate iPhone device through application

-(void)vibrate
{
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}

Jerking Effects with an ImageView in y-axis

-(void)shakeImageInY
{
CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"transform.translation.y"];
anim.values = [NSArray arrayWithObjects:[NSNumber numberWithFloat:-15],
[NSNumber numberWithFloat:15],
nil];
anim.duration = 0.2;
anim.autoreverses = YES;

anim.repeatCount = 0;
anim.additive = YES;
anim.fillMode=kCAFillModeForwards;
[imageView.layer addAnimation:anim forKey:@"wiggleTranslationY"];
}

Jerking Effects with an ImageView in x-axis

-(void)shakeImageInX
{
CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"transform.translation.y"];
anim.values = [NSArray arrayWithObjects:[NSNumber numberWithFloat:-15],
[NSNumber numberWithFloat:15],
nil];
anim.duration = 0.2;
anim.autoreverses = YES;

anim.repeatCount = 0;
anim.additive = YES;
anim.fillMode=kCAFillModeForwards;
[imageView.layer addAnimation:anim forKey:@"wiggleTranslationY"];
}