UITextField占位符在子类化时移位

时间:2013-02-26 04:35:43

标签: iphone objective-c uitextfield subclass placeholder

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

2 个答案:

答案 0 :(得分:1)

self.txtFirstName.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
self.txtFirstName.autocapitalizationType = UITextAutocapitalizationTypeSentences;
self.txtFirstName.placeholder = @"Enter First Name here";
self.txtFirstName.textAlignment = UITextAlignmentLeft;

答案 1 :(得分:0)

我有同样的问题,我尝试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];

希望这有帮助。