Nassif
Nassif

Reputation: 1223

UITextField placeholder shifts when subclassed

I am using a subclassed UITextField to use a custom font. When I set a placeholder for that textfield it gets shifted up a little, but the when I start entering the text, the frame of text has no problem. Is there a way to fix the placeholder shifting issue.

Upvotes: 0

Views: 633

Answers (2)

βhargavḯ
βhargavḯ

Reputation: 9836

I had same issue and I tried setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter which solved my issue.

            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];

Hope this helps.

Upvotes: 0

iPatel
iPatel

Reputation: 47099

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

Upvotes: 1

Related Questions