UITextField中的静态字符前缀 [英] A static character prefix in UITextField
问题描述
有一个内置的方法来添加一个字符前缀到 UITextField
,如下面的屏幕截图?
Is there a built-in way to add a character prefix to a UITextField
like the screenshot below?
如果没有,是最好的,最直接的方法来完成这个?我认为背景
属性可能能够做到这一点。
If no, what is the best, most straight-forward way to accomplish this? I am thinking the background
property might be able to do this.
推荐答案
只需在 UITextField
之上添加一个灰色 UILabel
即可:
Simply adding a gray UILabel
on top of the UITextField
does the trick:
请注意, UILabel
文字停留在顶部:
Note that the UILabel
behaves like a background image, the entered text stays on top:
UPDATE
此外,您还可以在 UITextField
使用以下代码:
Additionally you can add some padding to the UITextField
using the following code:
UIView *thePadding = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 20)];
theTextField.leftView = thePadding;
theTextField.leftViewMode = UITextFieldViewModeAlways;
这样文本就不能覆盖前缀字符。
将rect使其在您的情况下正常工作。
This way the text cannot cover the prefix character.
Adjust the rect to make it working properly in your situation.
这篇关于UITextField中的静态字符前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!