I have a situation where users, only on iOS 10 (10.0 thru 10.3 at the time of this post) encounter a complete app hang when trying to clear text from certain UITextFields by tapping the built-in clear button. This problem does not happen in all view controllers, yet all share the exact same delegate implementation. The fields are only used for US monetary input (digits and decimal) and are constrained by delegate to a certain number of characters.
I have reproduced this hang in the simulator and painstakingly confirmed it does not reproduce on iOS 9.3.5 or earlier, only iOS 10.0 and later. I have narrowed down that this problem has to do with setting attributed text set within a UITextField delegate. Again it only reproduces with some view controllers in the app, not all. But once a repro situation is identified, the above iOS version test results hold true.
When pressing the clear button, only after once having gone thru the editing done delegate path, the app goes into a complete runaway loop somewhere within the iOS SDK itself. I can reproduce it with the below example delegate alone:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
// apply our desired text attributes
//
// ensure right justification within the field
NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
paragraph.alignment = NSTextAlignmentRight;
// set desired fontface, fontsize, color, and field alignment
NSMutableAttributedString *attributedString = [textField.attributedText mutableCopy];
[attributedString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Bradley Hand" size:17.0f] range:NSMakeRange(0, [attributedString length])];
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor darkGrayColor] range:NSMakeRange(0, [attributedString length])];
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraph range:NSMakeRange(0, [attributedString length])];
// apply the changes in font, size, and color to be visible
textField.attributedText = attributedString;
return YES;
}
The oddest thing is that this does not happen in all UITextFields using this same delegate implementation, only some view controllers are affected. Others with this same code work just fine!
This implies there are some additional conditions for UIKit to get into this situation that I am unable to identify just yet.