ios 如何垂直居中 UITextField 文本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5439099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:22:57  来源:igfitidea点击:

How do I vertically center UITextField Text?

iosuitextfieldalignment

提问by Joey

I am simply instantiating a UITextFieldand 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?

我只是实例化 aUITextField并注意到文本没有垂直居中。相反,它与我的按钮顶部齐平,我觉得这有点奇怪,因为我希望默认设置垂直居中。如何将其垂直居中,或者是否缺少某些默认设置?

回答by Roger

textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

In swift use:

快速使用:

textField.contentVerticalAlignment = UIControlContentVerticalAlignment.center

回答by ginchly

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
  • 您要对齐的内容(仅数字、字母、大写字母或混合)
  • 占位符
  • 清除按钮

Whatyou'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/)

什么你要对齐是重要的,因为这点在字体应垂直居中,由于行高,伸,伸等。(来源:ilovetypography.com(图片感谢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.

我将下面的代码放在 UITextField 的子类中。它调用超类方法,因为这考虑了存在的清除按钮。

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)
}

回答by Kumar KL

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

在故事板中:在控制之下 -> 根据您的需要更改垂直和水平对齐方式。

enter image description here

在此处输入图片说明

回答by ancajic

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.

这适用于 textField 的文本。但是如果你想使用占位符文本(文本字段为空时会出现的文本),在 iOS 7 上你会遇到问题。

I solved it by overriding TextField class and

我通过覆盖 TextField 类和

- (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.

适用于 iOS7 和更早版本。