Restrict length for UITextField

by alex 29. September 2010 20:39

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)strin{

//Check that response is should be maximum 5 characters

if ([strin length] > 0) {

return [textField.text  length] < 5;

}

return YES;

}


 

Hide keyboard when pressing Done

by alex 26. October 2009 12:01

// You need to add this method to your controller class

- (BOOL)textFieldShouldReturn:(UITextField *)textField{

[textField resignFirstResponder];

return YES;

}


Move alert (UIAlretView)

by alex 29. July 2009 11:44

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"Alert" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];

alert.transform = CGAffineTransformTranslate( alert.transform, 0.0, 100.0 );

[alert show];

[alert release];

 

Move screen when keyboard popup

by alex 7. May 2009 14:18

//h file

 

@interface TestViewController : UIViewController<UITextFieldDelegate> {

    CGFloat animatedDistance;

}

 

//m file

 

 

 

- (void)textFieldDidBeginEditing:(UITextField *)textField{

    

    static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;

    static const CGFloat MINIMUM_SCROLL_FRACTION = 0.2;

    static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.8;

    static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 216;

    static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 162;

    

    CGRect textFieldRect =

    [self.view.window convertRect:textField.bounds fromView:textField];

    CGRect viewRect =

    [self.view.window convertRect:self.view.bounds fromView:self.view];

    

    CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height;

    CGFloat numerator = midline - viewRect.origin.y - MINIMUM_SCROLL_FRACTION * viewRect.size.height;

    CGFloat denominator = (MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION) * viewRect.size.height;

    CGFloat heightFraction = numerator / denominator;

    if (heightFraction < 0.0)

    {

        heightFraction = 0.0;

    }

    else if (heightFraction > 1.0)

    {

        heightFraction = 1.0;

    }

    

    UIInterfaceOrientation orientation =

    [[UIApplication sharedApplication] statusBarOrientation];

    if (orientation == UIInterfaceOrientationPortrait ||

        orientation == UIInterfaceOrientationPortraitUpsideDown)

    {

        animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);

    }

    else

    {

        animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);

    }

    

    CGRect viewFrame = self.view.frame;

    viewFrame.origin.y -= animatedDistance;

    

    [UIView beginAnimations:nil context:NULL];

    [UIView setAnimationBeginsFromCurrentState:YES];

    [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];

    

    [self.view setFrame:viewFrame];

    

    [UIView commitAnimations];

    

}

 

 

- (void)textFieldDidEndEditing:(UITextField *)textField{

    

    static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;

    

    CGRect viewFrame = self.view.frame;

    viewFrame.origin.y += animatedDistance;

    

    [UIView beginAnimations:nil context:NULL];

    [UIView setAnimationBeginsFromCurrentState:YES];

    [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];

    

    [self.view setFrame:viewFrame];

    

    [UIView commitAnimations];

}

Create TextField (UITextField) in runtime

by alex 17. February 2009 05:23

UITextField *loginTextField = [[UITextField alloc] initWithFrame:CGRectMake(20, 104, 285, 23)];

 

loginTextField.borderStyle = UITextBorderStyleBezel;

loginTextField.textColor = [UIColor blackColor];

loginTextField.font = [UIFont systemFontOfSize:14.0];

loginTextField.placeholder = @"<enter email>";

loginTextField.backgroundColor = [UIColor whiteColor];

loginTextField.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support

 

loginTextField.keyboardType = UIKeyboardTypeEmailAddress; // use the default type input method (entire keyboard)

loginTextField.returnKeyType = UIReturnKeyDone;

loginTextField.delegate = self;

loginTextField.clearButtonMode = UITextFieldViewModeWhileEditing; // has a clear 'x' button to the right

 

 

[self.view addSubview:loginTextField];


 

Show alert (UIAlertView) on iPhone

by alex 13. February 2009 16:03

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

if (buttonIndex == 1) {

//OK clicked

} else {

}

}

 

- (void) _showAlert:(NSString*)title

{

UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:title message:@"Check your networking configuration." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];

[alertView show];

[alertView release];

}

Alert (UIAlertView) with text field (UITextField)

by alex 13. February 2009 14:18

// open a alert with text field,  OK and cancel button

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Send Email" message:@" "

       delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Send", nil];

 

CGRect frame = CGRectMake(14, 45, 255, 23);

if(!emailField) {

emailField = [[UITextField alloc] initWithFrame:frame];

emailField.borderStyle = UITextBorderStyleBezel;

emailField.textColor = [UIColor blackColor];

emailField.textAlignment = UITextAlignmentCenter;

emailField.font = [UIFont systemFontOfSize:14.0];

emailField.placeholder = @"<enter email>";


emailField.backgroundColor = [UIColor whiteColor];

emailField.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support

emailField.keyboardType = UIKeyboardTypeEmailAddress; // use the default type input method (entire keyboard)

emailField.returnKeyType = UIReturnKeyDone;

emailField.delegate = self;

emailField.clearButtonMode = UITextFieldViewModeWhileEditing; // has a clear 'x' button to the right

}

 

[alert addSubview:emailField];

 

[alert show];

[alert release];

Powered by BlogEngine.NET 1.4.5.0
Theme by Mads Kristensen