【问题标题】:UITextField placeholder shifts when subclassedUITextField 占位符在子类化时发生变化
【发布时间】:2013-02-11 10:47:30
【问题描述】:

我正在使用子类UITextField 来使用自定义字体。当我为 textfield 设置占位符时,它会向上移动一点,但是当我开始输入文本时,文本的 frame 没有问题。有没有办法解决占位符移位问题。

【问题讨论】:

标签: iphone objective-c uitextfield subclass placeholder


【解决方案1】:
self.txtFirstName.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
self.txtFirstName.autocapitalizationType = UITextAutocapitalizationTypeSentences;
self.txtFirstName.placeholder = @"Enter First Name here";
self.txtFirstName.textAlignment = UITextAlignmentLeft;

【讨论】:

    【解决方案2】:

    我遇到了同样的问题,我尝试了 setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter 解决了我的问题。

                UITextField *txtPassword = [[UITextField alloc] initWithFrame:CGRectMake(15,55, 255, 25)];
                [txtPassword setPlaceholder:@"Password..."];
                [txtPassword setBackgroundColor:[UIColor whiteColor]];
                [txtPassword setFont:[UIFont fontWithName:@"Helvetica" size:15]];
                [txtPassword setSecureTextEntry:YES];
                [txtPassword setTextAlignment:NSTextAlignmentCenter];
                [txtPassword setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
                [txtPassword becomeFirstResponder];
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-31
      • 2011-09-03
      • 1970-01-01
      • 1970-01-01
      • 2012-07-17
      • 2014-03-01
      • 1970-01-01
      • 2022-07-29
      相关资源
      最近更新 更多