【问题标题】:Position text in a textfield在文本字段中定位文本
【发布时间】:2013-05-02 19:42:03
【问题描述】:

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

我当前的代码:

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

【问题讨论】:

    标签: ios objective-c uitextfield


    【解决方案1】:

    你不能在 UITextField 没有子类化的情况下隐式设置文本的位置。

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

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

    您也可以将this approach 用于子类化 UITextField。

    【讨论】:

    • 感谢您的帮助。我通过使用按钮而不是文本字段来解决我的问题。该按钮允许我更改插图,但仍然获得与使用文本字段相同的结果。
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

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

      愉快的编码...

      【讨论】:

        【解决方案4】:
        UITextField *textFieldNote = [[UITextField alloc] initWithFrame:CGRectMake(5, 5, 310, 110)];
        textFieldNote.placeholder = @"Ghi chú";
        textFieldNote.textAlignment = NSTextAlignmentLeft;
        textFieldNote.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
        

        【讨论】:

        • 您介意添加一些您所做的事情的描述吗?
        【解决方案5】:

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

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-06-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-02-22
          • 1970-01-01
          相关资源
          最近更新 更多