iOS Copy Paste phone from contacts to UITextField adds strange unicode characters

前端 未结 1 1702
孤独总比滥情好
孤独总比滥情好 2020-12-14 10:23

The simplified scenario is the following.

  • New project using Single View App template.
  • Add a UITextField to the ViewController.
  • Run the app an
相关标签:
1条回答
  • 2020-12-14 10:38

    I had exactly the same issue recently. Interestingly enough what you see in the debugger is unfortunately not what is actually pasted. If you copy the number to a different place and investigate it with your console for example you will get the following output:

    >>> u"\U+202D(888) 5555-5512\U+202C"
    u'\u202d(888) 5555-5512\u202c'
    >>> name(u"\U+202D")
    'LEFT-TO-RIGHT OVERRIDE'
    >>> name(u"\U+202C")
    'POP DIRECTIONAL FORMATTING'
    

    So as you can see it is really two different invisible characters controlling the flow of the text.

    In order to solve that I filtered out all unicode characters of the cF category. So you could do:

    phoneLabel.text?.replacingOccurrences(of: "\\p{Cf}", with: "", options: .regularExpression)
    
    0 讨论(0)
提交回复
热议问题