textViewDidChange: crashes in iOS 7

后端 未结 1 1254
醉梦人生
醉梦人生 2021-02-09 11:19

As we know the UITextView is more powerful in iOS 7, but here it performs more fragile. The following code works fine when inputting \"rr\" with Chinese input keybo

1条回答
  •  [愿得一人]
    2021-02-09 12:17

    What's happening is that you're typing what is referred to as multistage text input, i.e. the input has to be confirmed from the user before it's actually committed into the underlying text. You get the crash because the text is only kind-of inputted, and until the text has been committed, it could easily change to something else.

    To detect this situation, you're looking for the markedTextRange property of the UITextView, which indicates if you're in this complex input mode.

    If the property is non-nil, then you're in this special input mode, so you should guard your modification code with something like:

    if (self.tv.markedTextRange == nil && self.tv.text.length > maxLength) {
        // Perform change
    }
    

    Near as I can tell the crash is triggered by the special multistage text input mode, so you should avoid changing the text while this mode is active.

    0 讨论(0)
提交回复
热议问题