145

I am simply instantiating a UITextField and noticing that the text doesn't center vertically. Instead, it is flush with the top of my button, which I find kind of odd since I would expect the default to center it vertically. How can I center it vertically, or is there some default setting that I am missing?

Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
Joey
  • 7,537
  • 12
  • 52
  • 104

5 Answers5

371
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

In swift use:

textField.contentVerticalAlignment = UIControlContentVerticalAlignment.center
gebirgsbärbel
  • 2,327
  • 1
  • 22
  • 38
Roger
  • 15,793
  • 4
  • 51
  • 73
  • 1
    @user353877: This still works fine. Note that in Roger's response, `textField` is the name of his `UITextField` instance - yours may be different. So he will have something like `UITextField *textField = [[UITextField alloc] init];`. If you use a different variable name (anything other than `textField` after the `*`), you need to do `yourDifferentVariableNameHere.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter` instead. – GeneralMike Sep 06 '13 at 14:35
  • @user353877 Add a dot right before "Center": UIControlContentVerticalAlignment.Center; – Josh Jun 15 '15 at 08:21
  • 1
    is there same method for UITextView? – CaffeineShots Feb 01 '16 at 08:24
  • Is there a swift equivalent for this property? – demonofthemist Nov 14 '16 at 11:24
  • 3
    In Swift 3.0, this is the recommended way in using an enum: `textField.contentVerticalAlignment = .center` – Glenn Posadas Feb 17 '17 at 17:35
10

This has potentially got several complicating factors, some alluded to in previous answers:

  • What you're trying to align (just numbers, just letters, just uppercase letters or a mix)
  • Placeholders
  • Clear button

What you're trying to align is important because of which point in the font should be vertically centered due to line height, ascenders, descenders etc.
vertical font characteristics
(source: ilovetypography.com)

(Image thanks to http://ilovetypography.com/2009/01/14/inconspicuous-vertical-metrics/ )

When you're dealing with just numbers for example, the standard center alignment won't look quite right. Compare the difference in the two below, which use the same alignment (in the code below), one of which looks correct and the other which looks slightly off:

Not quite right with a mix of letters:

letters not looking centered

but looks right if it's just numbers

numbers looking centered

So, unfortunately, it may need a bit of trial and error and manual adjustment to get it looking visually correct.

I placed the below code in a subclass of UITextField. It calls superclass methods as this takes into account the clear button being present.

override func awakeFromNib() {
    contentVerticalAlignment = UIControlContentVerticalAlignment.Center
}

override func textRectForBounds(bounds: CGRect) -> CGRect {
    let boundsWithClear = super.textRectForBounds(bounds)
    let delta = CGFloat(1)
    return CGRect(x: boundsWithClear.minX, y: delta, width: boundsWithClear.width, height: boundsWithClear.height - delta/2)
}

override func editingRectForBounds(bounds: CGRect) -> CGRect {
    let boundsWithClear = super.editingRectForBounds(bounds)
    let delta = CGFloat(1)
    return CGRect(x: boundsWithClear.minX, y: delta, width: boundsWithClear.width, height: boundsWithClear.height - delta/2)
}

override func placeholderRectForBounds(bounds: CGRect) -> CGRect {
    let delta = CGFloat(1)
    return CGRect(x: bounds.minX, y: delta, width: bounds.width, height: bounds.height - delta/2)
}
Glorfindel
  • 21,988
  • 13
  • 81
  • 109
ginchly
  • 355
  • 4
  • 8
  • I think the `placeholderRectForBounds` extends too much to the right. I have a rightview and it goes into it unlike the other bounds. I just implemented `textRectForBounds` and called it from the other 2 funcs :) – Sasho Jun 09 '17 at 08:51
5

In the storyboard: Under control -> change the vertical as well as horizontal alignment, as to your needs.

enter image description here

Forge
  • 6,538
  • 6
  • 44
  • 64
Kumar KL
  • 15,315
  • 9
  • 38
  • 60
1

This works fine for textField's text. But if you want to use placeholder text (a text that will appear while textfield is blank), on iOS 7 you will encounter problems.

I solved it by overriding 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.

AndroC
  • 4,758
  • 2
  • 46
  • 69
1

You can also resolve the issue by adjusting the text baseline.

// positive: up, negative:down
// NSAttributedStringKey.baselineOffset:0

let attDic: [NSAttributedString.Key: Any] = [
    .font: UIFont.systemFont(ofSize: 16),
    .baselineOffset: 0
];

textfield.placeholder = NSAttributedString(string: "....", attributes:  attDic);

SHANG
  • 21
  • 3