3

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

我当前的代码:

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 定位?

4

5 回答 5

2

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

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

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

您也可以将此方法与 UITextField 的子类一起使用。

于 2013-05-08T14:18:17.870 回答
0

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

于 2013-05-08T14:55:48.597 回答
0

它非常简单,您只需要使用“UIEdgeInsets”在您想要的方向上进行文本字段填充

例如: UIEdgeInsets insets = UIEdgeInsetsMake(0, 10, 0, 0); [textView setContentInset:insets]; 它会在 10 pxls 之后向您显示文本字段......您可以这样提供。

快乐的编码...

于 2013-12-19T13:53:32.300 回答
0
UITextField *textFieldNote = [[UITextField alloc] initWithFrame:CGRectMake(5, 5, 310, 110)];
textFieldNote.placeholder = @"Ghi chú";
textFieldNote.textAlignment = NSTextAlignmentLeft;
textFieldNote.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
于 2014-04-09T09:47:17.130 回答
0

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

    let padding = UIView(frame: CGRect(x: 0, y: 0, width: 15, height: phoneNumberTextfield.frame.height))
    textFieldIndustry.leftView = paddingView
    textFieldIndustry.leftViewMode = .always

这样可以确保光标不在边缘

于 2018-10-25T17:36:25.753 回答