Pradumna Patil
Pradumna Patil

Reputation: 2220

UITextfield cursor moves

I am trying to change the UITextField font size dynamically when text changes. But when i do that my textfield cursor moves to up. So if anybody knows the solution please help.

enter image description here

Please check the image for reference. Thanks.

-(void)textChanged:(id)sender{

    UITextField *textFild = (UITextField*)sender;
    if ([textFild.text isEqualToString:@""]) {
        [textFild setFont:FONT_30];
    }
    else{
        [textFild setFont:FONT_15];
    }
    textFild.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
}

Please check the code..

Upvotes: 1

Views: 197

Answers (2)

Teja Nandamuri
Teja Nandamuri

Reputation: 11201

This is an expected behaviour. Set the text of the textfield along with the font.So the cursor will be reset to normal position.

-(void)textChanged:(id)sender {
    UITextField *textFild = (UITextField*)sender;
    NSString *tmpString = textFild.text; 
    if ([textFild.text isEqualToString:@""]) {
        textFild.text=tmpString;
        [textFild setFont:FONT_30];
    } else {
         textFild.text=tempString;
        [textFild setFont:FONT_15];
    }
    textFild.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
}

Upvotes: 0

Pushkraj Lanjekar
Pushkraj Lanjekar

Reputation: 2294

Add this line after setting font size dynamically. This will work for you.

txtFieldPassword.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

Instead of txtFieldPassword write IBOutlet name of your respective textfield.

Upvotes: 1

Related Questions