I have various crashes showing up in my reports that are reporting the following reason:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'failed to generate newParagraphStyle'
The app that this is happening on does set a lot of attributed text programmatically (fonts, colors, sometimes paragraph styles), but I can not find any pattern directly related to this. What I do see is almost all of the reports include a reference in the stack trace to some form of setLineBreakMode:
-[_UICascadingTextStorage(Accessors) setLineBreakMode:]
I have never been able to recreate this myself, but it does show up at a fairly regular basis on my crash reporting. I am not specifically setting the line break mode in any of these places. Any ideas?
EDIT: I spent more time looking into this and was able to find a pattern to reproduce. So I have a UITextField, and when you hit return on the keyboard the "/n" character is detected and the textfield resigns its first responder status. If at any point after that, you try to modify the text (or attributed text) of the field, it crashes. There is one key element, it only happens if you have typingAttributes set. It does not matter what they are set to.
I tried everything, setting typingAttributes to nil before setting the text, setting a fixed string to make sure it was not what we were setting it to - no luck. Keeps crashing. It seems once you get in this state, the textfield is just broken. A hack to avoid it is to blow away the textfield and create a new one - but its just that. A hack.
Some more interesting info, the way that the code detects the return key was not well written - it detected the "/n" and returned NO in:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
It did not use the appropriate method:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
In one place in the app this actually fixed the crash, in other places it was always using the correct method and the crash is still happening. The only work around is to blow away the textfield and create a new one. To me, this seems like a bug in iOS itself.
I had this issue just now and was able to fix it by setting the typingAttributes
of the problematic textField
to nil
directly before resignFirstResponder
is called by that same textField
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With