The CSS just sizes the div, vertically center aligns the span by setting the div's line-height equal to its height, and makes the span an inline-block with vertical-align: middle. Then it sets the line-height back to normal for the span, so its contents will flow naturally inside the block.
For vertical alignment, set the parent element's width / height to 100% and add display: table . Then for the child element, change the display to table-cell and add vertical-align: middle . For horizontal centering, you could either add text-align: center to center the text and any other inline children elements.
Use line-height for vertical centering with a fixed height To vertically center a single line of text or an icon within its container, we can use the line-height property. This property controls the height of a line in a run of text and adds an equal amount of space above and below the line-box of inline elements.
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
In swift use:
textField.contentVerticalAlignment = UIControlContentVerticalAlignment.center
This has potentially got several complicating factors, some alluded to in previous answers:
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.
(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:
but looks right if it's just numbers
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)
}
In the storyboard: Under control -> change the vertical as well as horizontal alignment, as to your needs.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With