Cocoa-Matic

iPhone, iPod touch, iPad tutorials, examples and sample code. Come and get it!

Wednesday, January 26, 2011

UITextField horizontal and vertical alignment

Horizontal alignment in a UITextField was quick and easy to find:
myTextField.textAlignment = UITextAlignmentLeft;
myTextField.textAlignment = UITextAlignmentCenter;
myTextField.textAlignment = UITextAlignmentRight;

Vertical alignment in a UITextField is equally as easy, just a little harder to find:
myTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
myTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
myTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentBottom;
myTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentFill;

Enjoy!

Labels: , , ,

Tuesday, August 3, 2010

UITextField: Only allow numeric entry using NSScanner

Have a UITextField and want to prevent the user from entering anything but numbers (and decimal point)? This quick function utilizing the UITextFieldDelegate will do the trick.

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

 NSString *resultingString = [textField.text stringByReplacingCharactersInRange: range withString: string];
 
 // This allows backspace
 if ([resultingString length] == 0) {
  return true;
 }

 double holder;
 NSScanner *scan = [NSScanner scannerWithString: resultingString];
 
 return [scan scanDouble: &holder] && [scan isAtEnd];
}
Read more »

Labels: , , ,


« Older Entries