views:

47

answers:

1

Hi

I am creating a UITextField programatically and placing it inside a UIView. The font size is set to 15.0f (system font). But the placeholder that appears is not centered in the text view. Any one know how to resolve this?

I found some references on SO for blurred text etc and tried setting the frame of the textfield with integer values, but it doesn't make a difference.

UITextField *txtField = [[UITextField alloc] initWithFrame:CGRectMake(5.0f, 6.0f, 278.0f, 32.0f)];
[txtField setPlaceholder:@"My placeholder"];
[txtField setFont:[UIFont systemFontOfSize:15.0]];
[txtField setBorderStyle:UITextBorderStyleRoundedRect];
[txtField setAutocorrectionType:UITextAutocorrectionTypeNo];
[txtField setAutocapitalizationType:UITextAutocapitalizationTypeNone];
[txtField setKeyboardType:UIKeyboardTypeEmailAddress];
[txtField setReturnKeyType:UIReturnKeyDone];
[txtField setClearButtonMode:UITextFieldViewModeWhileEditing];

Thank you for any help

Note: I mean that the text is not centered vertically in the textfield (it is a bit towards the top). So setting the text alignment is not the solution.

Adding an image of the issue for clarification - as seen in the image, the placeholder text is more towards the top and not in the center vertically. alt text

+1  A: 

You need to add:

txtField.textAlignment = UITextAlignmentCenter;

Keep in mind, this adjusts both the alignment of the placeholder text as well as the text the user will enter in.

jer
Sorry about that. I guess the question was a bit mis-leading. I meant that the text is not centered vertically. Updated the question.
lostInTransit
Then link us to a screenshot so we can see exactly what you're talking about. I've never seen this myself, and I've done this a fair bit.
jer
Added the image as well. Please let me know if that helps in pin-pointing the issue. Not sure what I could be doing wrong. The method for creating a textfield is pretty straightforward!
lostInTransit
Ah ok, I see what's going on. Try setting your text field's `contentVerticalAlignment` property to `UIControlContentVerticalAlignmentCenter` that should fix you right up.
jer
Aahh.. That fixed it! Thanks a lot!
lostInTransit