Reputation: 2220
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.
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
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
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