I am trying to get my textfield(s) to expand horizontally while the user types. I have the code:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
CGFloat textWidth = [[NSString stringWithFormat:@"%@%@",[textField text], string] sizeWithFont:[textField font]].width;
textWidth += 15;
CGRect tFFrame = [textField frame];
tFFrame.size.width = textWidth;
textField.frame = tFFrame;
[textField setNeedsDisplay];
return YES;
}
Which works for most cases, but at first backspace the frame doesn't update, and when I paste when text is already selected, it acts like replaced text was still there. How can I fix these issues, and also, I know that the sizeWithFont:
method is not for iOS 7, so it anyone has any ideas on how to make backwards compatibility, that would also be great.
Thanks in advance.