Sunday, June 2, 2013

UITextField textAlignment example in Objective C (iOS).

UITextField textAlignment

The technique to use for aligning the text.

@property(nonatomic) NSTextAlignment textAlignment

Discussion of [UITextField textAlignment]
This property applies to the both the main text string and the placeholder string. The default value of this property is NSLeftTextAlignment.

If you are using styled text in iOS 6 or later, assigning a new value to this property causes the text alignment to be applied to the entirety of the string in the attributedText property. If you want to apply the alignment to only a portion of the text, create a new attributed string with the desired style information and associate it with the text field.

UITextField textAlignment example.
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    textField.textAlignment = NSTextAlignmentLeft ;
    return YES ;

Example of [UITextField textAlignment].
text = [[UITextField alloc] initWithFrame:CGRectMake(80.0, 8.0, 210.0, 30.0)];
text.clearsOnBeginEditing = YES;
text.delegate = self;
text.borderStyle = UITextBorderStyleRoundedRect;
text.textAlignment = UITextAlignmentCenter;

UITextField textAlignment example.
For VerticalAlignment.

textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
Centered both vertically and horizontally.

theTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
theTextField.textAlignment = UITextAlignmentCenter;

End of UITextField textAlignment example article.