I have a problem, when Right Aligning a UITextField on iOS7, when the user types "Space" it wont appear right away. If I type another character the spaces appears.
In iOS 6 does is not happening
Anyone know how to fix this?
I have a problem, when Right Aligning a UITextField on iOS7, when the user types "Space" it wont appear right away. If I type another character the spaces appears.
In iOS 6 does is not happening
Anyone know how to fix this?
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (range.location == textField.text.length && [string isEqualToString:@" "]) {
// ignore replacement string and add your own
textField.text = [textField.text stringByAppendingString:@"\u00a0"];
return NO;
}
// for all other cases, proceed with replacement
return YES;
}
To remove the code from text
self.txtFirstName.text = [self.txtFirstName.text stringByReplacingOccurrencesOfString:@"\u00a0" withString:@" "];
FROM this stackoverflow answer - Right aligned UITextField spacebar does not advance cursor in iOS 7
I don't know how to fix it but I have a suggestion, you could, in the mean time, replace all white spaces with a very similar unicode character (Like U+00A0), and then switch them back after another character has been typed?
Just include the <UITextFieldDelegate>
in your .h, set the UITextField.delegate = self;
in viewDidLoad
Then execute something like this:
//!!!!!*****!*!*!!!*!*** Make SURE you set the delegate code before doing this (as mentioned in the original SO answer)
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if ([string isEqualToString:@" "]) {
textField.text = [textField.text stringByReplacingCharactersInRange:range withString:@"\u00A0"];//U+00A0 is a unicode character that seems very similar to space but isn't treated as whitespace...
} else {
textField.text = [textField.text stringByReplacingOccurrencesOfString:@"\u00A0" withString:@" "];//switches our u+00A0 unicode character back to a white-space everytime a space is not typed.
}
return (![string isEqualToString:@" "]);//returns YES if it doesn't equal whitespace, else NO (because we did a manual replace)
}
*Note, I haven't had a chance to test this out yet, as I'm not near an xCodeProj. Let me know how it works :) If anyone sees any errors please feel free to make an edit O:) Thanks All!