UITextFieldDelegate function shouldChangeCharactersIn should have access to smart punctuation changes

Originator:kvstevns
Number:rdar://35833390 Date Originated:December 4, 2017
Status:Open Resolved:
Product:iOS + SDK Product Version:iOS 11.1
Classification:Suggestion Reproducible:Always
 
Area:
UIKit

Summary: 
Smart punctuation in iOS 11 allows user input into text fields to change from simple glyphs to region-specific glyphs. This change occurs without passing through the text field delegate method shouldChangeCharactersIn. That delegate method is notified about the change to add a simple character and then the change to a region-specific character occurs without another call. Text field delegates should have access to these smart punctuation changes so that they can implement proper bookkeeping and validation.

Steps to Reproduce:
1. Create a text field with smart punctuation enabled
2. Set a delegate on the text field with an implementation for shouldChangeCharactersInRange
3. Set a breakpoint in the shouldChangeCharactersInRange or print the unicode scalars for the string changes
4. Register an observer for the notification UITextFieldTextDidChange
5. Set a breakpoint in the method that responds to that notification or print the unicode scalars for the contents of the text field
6. Run and type a single quote into the text field

Expected Results:
There are two possible expectations. Either shouldChangeCharactersInRange is called after the smart punctuation replacement occurs (it sees only the region-specific character) and one notification is sent for UITextFieldTextDidChange. Or shouldChangeCharactersInRange is called again after smart punctuation replacement occurs (it sees both simple and region-specific changes) and two notifications are sent for UITextFieldTextDidChange.

Actual Results:
shouldChangeCharactersInRange is called once and includes the single quote for simple punctuation, unicode scalar value 39. Only one notification is sent for UITextFieldTextDidChange at which point the text field contains a curved single quote, unicode scalar value 8216.

Version/Build:
Base SDK iOS 11.1

Comments

I am seeing the same thing happen for the "." shortcut where two spaces are replaced with a period and a space. In iOS 9.3 and iOS 10.3 I see two calls to shouldChangeCharactersInRange, one for "space space" and another for "period space". That is followed by one notification for UITextFieldTextDidChangeNotification at which point the text field is "period space". This behavior has changed iOS 11. I only get one call to shouldChangeCharactersInRange with "space space" followed by one notification for UITextFieldTextDidChangeNotification at which point the text field is "period space". I believe this is a bug in iOS 11.


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!