iOS复制粘贴电话从联系人到UITextField会添加奇怪的Unicode字符 [英] iOS Copy Paste phone from contacts to UITextField adds strange unicode characters

查看:128
本文介绍了iOS复制粘贴电话从联系人到UITextField会添加奇怪的Unicode字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是简化的情况.

  • 使用Single View App模板的新项目.
  • 将UITextField添加到ViewController.
  • 运行该应用程序,然后复制并粘贴联系人"电话号码[ej. John Applese将一个(888) 555-5512)]插入了UITextField.
  • New project using Single View App template.
  • Add a UITextField to the ViewController.
  • Run the app and copy and paste a Contacts phone number [ej. John Appleseed one (888) 555-5512) ] to the UITextField.

该数字将在开头和结尾添加Unicode字符,在调试时浏览变量时会像\u{e2}(888) 555-5512\u{e2}一样.

The number will be added with a Unicode character at the beginning and at the end, getting like \u{e2}(888) 555-5512\u{e2} when exploring the variable while debugging.

这真的很奇怪,我认为这不是预期的行为. 这是bug还是故意以这种方式起作用的东西?

This is really weird and in my opinion, not the intended behaviour. Is this a bug or something that works intentionally this way?

代码:

这里没有什么复杂的.如前所述,新建项目,添加UITextField,添加Button,如果按钮触发,则打印结果. 印刷品将显示电话正常,只需在print行中放置一个断点,然后查看电话var的值即可了解我的意思.

Nothing complicated here. As described before, brand new project, add UITextField, add Button, and if button triggered print the result. The print will show the phone just fine, just put a breakpoint in the print line and see the value of the phone var to see what I mean.

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var phoneLabel: UITextField!

    @IBAction func goButton(_ sender: UIButton) {

        let text = phoneLabel.text ?? ""
        print(text)
    }
}

测试于:

  • iOS 11.1-iPhone X
  • Xcode 9.1

包含图片的步骤:

这是我在断点处得到的.

This is what I got at the breakpoint line.

推荐答案

我最近遇到了完全相同的问题.有趣的是,不幸的是,您在调试器中看到的实际上不是实际粘贴的内容. 例如,如果将号码复制到其他位置并使用控制台进行调查,则会得到以下输出:

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.

为了解决这个问题,我过滤了cF

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)

这篇关于iOS复制粘贴电话从联系人到UITextField会添加奇怪的Unicode字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆