【问题标题】:How to add space at start of UITextField如何在 UITextField 的开头添加空格
【发布时间】:2013-06-13 07:27:06
【问题描述】:

我正在创建一个包含多个UITextField 的应用程序。在 textField 中,我已将边框类型设置为无和背景图像。显示很好,但是现在我的文字是从最不好看的边框开始的,像这样

如何在文本字段的开头添加空格?

【问题讨论】:

标签: iphone ios uitextfield


【解决方案1】:

试试这个

UILabel * leftView = [[UILabel alloc] initWithFrame:CGRectMake(10,0,7,26)];
leftView.backgroundColor = [UIColor clearColor];

textField.leftView = leftView;

textField.leftViewMode = UITextFieldViewModeAlways;
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

【讨论】:

    【解决方案2】:

    这是@Kalpesh 在 Swift 3.x 中的回答:

    let leftView = UILabel(frame: CGRectMake(10, 0.0, 7, 26))
    leftView.backgroundColor = .clearColor()
    
    customField.leftView = leftView
    customField.leftViewMode = .Always
    customField.contentVerticalAlignment = .Center
    

    Swift 4.x

    let leftView = UILabel(frame: CGRect(x: 10, y: 0, width: 7, height: 26))
    leftView.backgroundColor =.clear
    
    customField.leftView = leftView
    customField.leftViewMode = .always
    customField.contentVerticalAlignment = .center
    

    【讨论】:

      【解决方案3】:

      您应该继承 UITextField 并覆盖 drawText 函数。

      查看this 寻求帮助 要么 您可以执行以下操作:

      UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 15, height_of_textfiled)];
      textField.leftView = paddingView;
      textField.leftViewMode = UITextFieldViewModeAlways;
      

      【讨论】:

      • 这会影响我的文字吗?意味着我正在将数据发送到网络服务,那么这段代码是否会将任何恶意数据添加到我的实际文本中?
      • 现在代码可以正常工作了。你忘了说leftViewMode
      【解决方案4】:

      目标 c

      对于仅填充占位符,此代码将起作用

      usernameTF.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"    Your text"];
      

      为了填充 UITextField 的占位符和文本,下面的代码将起作用

      usernameTF.layer.sublayerTransform = CATransform3DMakeTranslation(40, 0, 30);
      

      Swift 3.0

      对于仅填充占位符,此代码将起作用

      yourTextField.attributedPlaceholder = NSAttributedString(string: "    YourText")
      

      为了填充 UITextField 的占位符和文本,下面的代码将起作用

      usernameTF.layer.sublayerTransform = CATransform3DMakeTranslation(40, 0, 30)
      

      【讨论】:

        【解决方案5】:

        您可以继承 UITextField 并覆盖 textRectForBoundseditingRectForBounds

        例如,

        - (CGRect)textRectForBounds:(CGRect)bounds {
            return CGRectMake(bounds.origin.x + 10, bounds.origin.y + 5, bounds.size.width - 20, bounds.size.height - 10);
        }
        
        - (CGRect)editingRectForBounds:(CGRect)bounds {
            return CGRectMake(bounds.origin.x + 10, bounds.origin.y + 5, bounds.size.width - 20, bounds.size.height - 10);
        }
        

        【讨论】:

        【解决方案6】:

        斯威夫特 4

        yourTextField.layer.sublayerTransform = CATransform3DMakeTranslation(10, 0, 10)
        

        【讨论】:

          【解决方案7】:

          您可以将文本字段背景图像放在图像视图中,在图像视图上方将文本字段留一个空格。

          【讨论】:

            【解决方案8】:
            -(void)textField:(UITextField *) textField didBeginEditing {
                if ([textField.text isEqualToString:@""] || textField.text == NULL) {
                   textField.text = @" ";
                }
            }
            

            【讨论】:

              猜你喜欢
              • 2017-07-12
              • 1970-01-01
              • 1970-01-01
              • 2016-04-08
              • 2016-09-16
              • 2014-11-05
              • 2021-10-22
              • 1970-01-01
              相关资源
              最近更新 更多