Condition:
XCode: 15.0
Version: iOS 17.0
iPhone / Simulator's device settings:
- Language Preference: Selecting Traditional Chinese (繁体中文) or TraditionalChinese (Hongkong) 繁体中文(香港) ( ) as the first preferernce language
- Keyboard: Traditional Chiniese Caontonese with CangJie (倉頡) or Accelerated ( 速成) standard or engligsh keyboard
Issue:
When UITextView been very simplified customized by providing its delegation function shouldChangeTextIn
and assign this customized text view as its delegation:
extension CustomizedTextView: UITextViewDelegate { func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool { // make calculation of the replacementText and insert dot character based on the current cusor positon etc.condition checks return true } }
To observe and control characters appearance for each one of the input stroke, this delegation function would be found been called twice when each number or special characters eg: dot charact .
enters under such case.
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool
But with the same conditions only to switch keyboard to others out of the listed above eg: English keyboard, then each one of the all alphabets, numbers, special characters on the keyboard gets the delegation function called only once.
Impacts
With this unexpected function triggered twice on iOS 17.0 or above which also been verified still exists on the physical device iOS 17.1, our project to utilize the related result of the function to make inputed number formatting and re-calculate the input cursor position, then the cursor would be inserted into wrong position.
For example:
User input -> 123 / 1234
Lead to wrong alculation shows -> 132 / 1,432
which the new cursor offset calculation based on the current selectedCursor
when the function gets called, together with some checks of the number input whether needs insert comma ,
to format the number. Now the issue makes our checking logic unexpected calculated twice and the result displayed on the text view not aligned with customer inputed which makes it much confused.
Anyone here hits the issue also please provide your view on it, would be much appreciated.
Tip:
- The is couldn't reproducible under iOS 16
- The issue also applicable for UITextField on the same condition