Ios 在文本字段中定位文本

Ios 在文本字段中定位文本,ios,objective-c,uitextfield,Ios,Objective C,Uitextfield,我想更具体地将文本定位在文本字段中 我当前的代码: UITextField * textFieldIndustry = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 465, 68)]; textFieldIndustry.font = [UIFont systemFontOfSize:17.0]; textFieldIndustry.placeholder = @"Tap to.."; textFieldIndustry.t

我想更具体地将文本定位在文本字段中

我当前的代码:

UITextField * textFieldIndustry = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 465, 68)];
textFieldIndustry.font = [UIFont systemFontOfSize:17.0];  
textFieldIndustry.placeholder = @"Tap to..";  
textFieldIndustry.textAlignment = NSTextAlignmentCenter;
此代码:

textFieldIndustry.textAlignment = NSTextAlignmentCenter; 

将文本居中,但不是我想要的位置。如何使用例如X&Y来定位?

如果不对子类化,则无法在UITextField中隐式设置文本的位置

您可以改为使用UITextView,并应使用相同的方法:

UITextView *textView;
UIEdgeInsets insets = UIEdgeInsetsMake(top, left, bottom, right);
[textView setContentInset:insets];

您还可以使用对UITextField进行子类化。

您还可以使用UITextView的leftView部分来执行您所要求的操作。这非常适合在文本字段中添加前缀,而无需在其前面放置标签。我不确定您希望如何使用它,但这可能是一个很好的解决方案。基本上,您只需创建一个宽度和高度都符合您要求的UILabel(或任何视图),然后将其添加为文本字段的左视图。

非常简单,您只需使用“UIEdgeInsets”在所需的方向上填充文本字段即可

例如:UIEdgeInsets insets=UIEdgeInsetsMake(0,10,0,0); [文本视图设置内容插入:插入]; 它将显示10 pxls之后的文本字段,就像您可以给出的那样


快乐编码…

对于
x
,您可以通过在文本字段的左侧添加视图来创建填充

UITextField *textFieldNote = [[UITextField alloc] initWithFrame:CGRectMake(5, 5, 310, 110)];
textFieldNote.placeholder = @"Ghi chú";
textFieldNote.textAlignment = NSTextAlignmentLeft;
textFieldNote.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
    let padding = UIView(frame: CGRect(x: 0, y: 0, width: 15, height: phoneNumberTextfield.frame.height))
    textFieldIndustry.leftView = paddingView
    textFieldIndustry.leftViewMode = .always

这可确保光标不在edg上

谢谢您的帮助。我通过使用按钮而不是文本字段解决了我的问题。该按钮允许我更改插图,但仍然得到与使用文本字段相同的结果。您介意添加一些您所做的描述吗?