【问题标题】:Padding Placeholder text ios填充占位符文本 ios
【发布时间】:2013-12-23 21:15:41
【问题描述】:

我想让占位符文本显示在文本字段的中间(填充占位符文本)。占位符文本的大小也需要增加。我的代码如下,我该如何解决?

UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(30, 220,250,55)];

textField.placeholder=@"iiiiiii";

UIView *padView = [[UIView alloc] initWithFrame:CGRectMake(10, 110, 10, 0)];
textField.leftView = padView;
    textField.leftViewMode = UITextFieldViewModeAlways;


[self.view addSubview:textField];

更新

我希望占位符文本的字体大小增加your name,并且它应该有一个左填充。

【问题讨论】:

  • 您是否要问如何让 placeholder 文本居中在 UITextField 中,但在用户开始向其中添加 text 时左对齐?
  • 我已经更新了我的帖子,我还添加了一张图片来描述我的问题。

标签: ios uitextfield


【解决方案1】:

你可以继承你的 UITextFiled 并覆盖方法:

MyTextField.m

- (CGRect)textRectForBounds:(CGRect)bounds {
    return [self rectForBounds:bounds];
}

- (CGRect)editingRectForBounds:(CGRect)bounds {
    return [self rectForBounds:bounds];
}

- (CGRect)placeholderRectForBounds:(CGRect)bounds {
    return [self rectForBounds:bounds];
}

//here 40 - is your x offset
- (CGRect)rectForBounds:(CGRect)bounds {
    return CGRectInset(bounds, 40, 3);
}

更新: 也设置了

textFiled.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

因为 io6 与 ios 7 垂直定位可能会出现一些问题

【讨论】:

  • 问题是什么意思?
  • a 有一个在 ios6 和 ios7 垂直对齐方式与相同的代码不同,并设置 contentVerticalAlignment 帮助。
【解决方案2】:

同样的问题被问及回答如下Set padding for UITextField with UITextBorderStyleNone

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

【讨论】:

    【解决方案3】:

    您可以将起始文本对齐方式(来自 xib 或通过代码的textField)设置为居中对齐。
    然后在-textFieldShouldBeginEditing中,可以设置textField左对齐。
    同样,在-textFieldDidEndEditing 上,检查textField 是否为空,然后将textField 设置回居中对齐。

    基本上:

    -(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
    {
        [textField setTextAlignment:NSTextAlignmentLeft];
        return YES;
    }
    
    -(void)textFieldDidEndEditing:(UITextField *)textField
    {
        if(textField.text.length == 0) {
            [textField setTextAlignment:NSTextAlignmentCenter];
        }
    }
    

    编辑:

    ViewController 类的 .h 应如下所示:

    @interface ViewController : UIViewController <UITextFieldDelegate> 
    {
        UITextField *myTextField;
    }
    

    现在,用这个替换你的其他代码:

    myTextField = [[UITextField alloc] initWithFrame:CGRectMake(30, 220,250,55)];
    
    myTextField.placeholder=@"iiiiiii";
    
    //important
    [myTextField setDelegate: self];
    
    //commented lines not really needed
    //UIView *padView = [[UIView alloc] initWithFrame:CGRectMake(10, 110, 10, 0)];
    //textField.leftView = padView;
    //textField.leftViewMode = UITextFieldViewModeAlways;
    
    [self.view addSubview:textField];
    

    【讨论】:

    • 所以我需要这个[textField setDelegate:self];,当我添加该行时,它显示`将'ViewController *const __strong'发送到不兼容类型'id'的参数`
    • @sharonHwk :是的,需要委托,但,在您的ViewController.h 中,您需要声明您的类是使用委托(只需 &lt;UITextFieldDelegate&gt;)...例如:@interface ViewController : UIViewController &lt;UITextFieldDelegate&gt;
    • @sharonHwk :但是...但是...它不完全是[textField setDelegate:self]; ...在您的-viewDidLoad 或您创建UITextField 对象的任何位置,然后说类似UITextField *txtFSomething = [UITextField alloc] init];您通过 [txtFSomething setDelegate:self]; 设置委托,或者通过 xib 连接进行。
    • 我仍然收到同样的警告。
    • @sharonHwk : 在哪里添加行 [textField setDelegate:self]; ?另外,将ou UITextField 对象的名称从textField 更改为myTextField 之类的其他名称,然后执行[myTextField setDelegate:self];
    【解决方案4】:
    sampleTextfield.leftView = UIView(frame: CGRectMake(0, 0, 20, self.sampleTextfield.frame.height))
    sampleTextfield.leftViewMode = UITextFieldViewMode.Always
    

    【讨论】:

      【解决方案5】:

      我发现在 swift 2 和 Xcode 7 上执行此任务的最简单方法:

      import UIKit
      
      class ViewController: UIViewController {
      
          @IBOutlet weak var emailTextField: UITextField!
      
          override func viewDidLoad() {
              super.viewDidLoad()
      
              let paddingView = UIView(frame: CGRectMake(0, 0, 25, self.emailTextField.frame.height))
              emailTextField.leftView = paddingView
              emailTextField.leftViewMode = UITextFieldViewMode.Always
          }
      }
      

      【讨论】:

        【解决方案6】:
        func placeholderPadding(textField:UITextField, leftPadding:CGFloat) {
                    textField.leftView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: leftPadding, height: textField.frame.height))
                    textField.leftViewMode = UITextFieldViewMode.always
                }
        

        【讨论】:

          【解决方案7】:

          虽然我没有专门测试过,但这应该可以工作:

          self.YourTextField.attributedPlaceholder = NSAttributedString(string: "    YourText")
          

          在字符串中添加空格(填充)的数量

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2012-03-27
            • 2017-07-19
            • 2011-06-22
            • 2016-10-29
            • 2013-11-09
            • 2016-05-22
            • 1970-01-01
            相关资源
            最近更新 更多