The placeholder text is not oriented to a UITextField created programmatically

I create a UITextField programmatically and put it in a UIView. The font size is 15.0f (system font). But the replacement object is not in the text view. Does anyone know how to solve this?

I found some SO links for blurry text, etc. and tried to set a text box frame with integer values, but that doesn't matter.

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]; 

Thanks for any help

Note. I mean, the text is not centered vertically in the text box (it is a little closer to the top). Therefore, setting text alignment is not a solution.

Adding an image of a problem for clarification - as you can see in the image, the placeholder text refers more to the vertex rather than vertically centered. alt text

+43
iphone uitextfield
Sep 08 '10 at 7:11
source share
4 answers

You need to add:

 txtField.textAlignment = NSTextAlignmentCenter; // Pre-iOS6 SDK: UITextAlignmentCenter 

Keep in mind that this adjusts both the alignment of the placeholder text and the text that the user enters.

How to center vertically

Since the original question has been updated to ask how to vertically align the placeholder text and that the answer is similar to comments, here's how to do it:

 txtField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; 
+56
08 Sep '10 at 7:19
source share

This does not work on iOS7 as expected. On iOS7, you have to override the TextField class and

 - (void) drawPlaceholderInRect:(CGRect)rect 

method.

Like this:

 - (void) drawPlaceholderInRect:(CGRect)rect { [[UIColor blueColor] setFill]; CGRect placeholderRect = CGRectMake(rect.origin.x, (rect.size.height- self.font.pointSize)/2, rect.size.width, self.font.pointSize); [[self placeholder] drawInRect:placeholderRect withFont:self.font lineBreakMode:NSLineBreakByWordWrapping alignment:self.textAlignment]; } 

Works for both iOS7 and earlier versions.

+18
Oct 07 '13 at 12:18
source share

I used only color, but we also need to set the font.

This worked for me:

 [[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setDefaultTextAttributes: @{ NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName: [UIFont systemFontOfSize:12] }]; 
+2
Jul 21 '15 at 17:41
source share

Changing the minimum line height using NSParagraphStyle was the only solution that worked for me:

  NSMutableParagraphStyle *style = [self.addressBar.defaultTextAttributes[NSParagraphStyleAttributeName] mutableCopy]; style.minimumLineHeight = self.addressBar.font.lineHeight - (self.addressBar.font.lineHeight - placeholderFont.lineHeight) / 2.0; self.addressBar.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"Placeholder text" attributes:@{ NSForegroundColorAttributeName: [UIColor colorWithRed:79/255.0f green:79/255.0f blue:79/255.0f alpha:0.5f], NSFontAttributeName : placeholderFont, NSParagraphStyleAttributeName : style }]; 
0
May 10 '17 at 11:02
source share



All Articles