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.
After finding this workaround I must conconclude this is a bug in the iOS 10 SDK, related in some way to other interactions performed by some view controllers within my app.
In reading about a UITextField hang for an unrelated scenario, I tried the workaround identified in this post TextField input hangs inputting too many characters
Changing the clear delegate to set the property mentioned also works around this clearing of attributed text bug scenario I have encountered.
- (BOOL)textFieldShouldClear:(UITextField *)textField {
// work around iOS 10 bug
textField.adjustsFontSizeToFitWidth = NO;
return YES;
}
I am not sure why this has a positive impact on the problem, as the text entry in this apps' fields is delegate constrained to no more than 5 characters always and the width of these fields in storyboard is 6X of that.
I post this as my own answer as a verified workaround, but am looking for other answers that may avoid the situation being encountered altogether.