Try using this option. But UITextField does not provide any option for creating a multi-line text field. So you have to go with a UITextView .
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { CGSize size = [txtView.text sizeWithFont:txtView.font]; CGRect f = txtView.frame; f.size.height = ceil(size.width/f.size.width)*size.height; txtView.frame = f; }
And for more information ..
Otherwise, if you do not want to do the next line, use this code.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { CGSize boundingSize = CGSizeMake(320, CGFLOAT_MAX); CGSize requiredSize = [textField.text sizeWithFont:[UIFont fontWithName:@"Helvetica" size:20.0f] constrainedToSize:boundingSize lineBreakMode:NSLineBreakByWordWrapping]; CGFloat requiredHeight = requiredSize.height; CGRect frame1; frame1 = txtFld.frame; frame1.size.height = requiredHeight; txtFld.frame = frame1; NSLog(@"%f", frame1.size.height); return YES; }
source share