【问题标题】:iOS: Extract detected numbers and links from UITextView?iOS:从 UITextView 中提取检测到的数字和链接?
【发布时间】:2018-12-10 20:50:27
【问题描述】:

This question 和类似的解决了如何检测UITextView 内的电话号码和链接。

一旦检测到,有没有办法提取检测到的数字和链接?

目标是使用UITextView 来检测数字和链接,而不是重建这个逻辑。

澄清一下,理想的工作流程是:

  1. 用户在UITextView中输入文本。
  2. UITextView 检测数字和链接。
  3. 从文本中提取数字和链接。

【问题讨论】:

    标签: ios url uitextview detection phone-number


    【解决方案1】:

    UITextView 确实只是让用户可点击链接的数据检测过程更容易实现。如果您尝试提取和使用该数据,我可能会建议实施它来处理您的特定用例。幸运的是,数据检测非常简单。

    let string = "www.google.com / (555) 123-4567"
    
    let types: NSTextCheckingResult.CheckingType = [.phoneNumber, .link]
    let detector = try NSDataDetector(types: types.rawValue)
    let range = NSMakeRange(0, string.count)
    detector.enumerateMatches(in: string, options: [], range: range) { result, _, _ in
       print(result)
    }
    

    输出:

    Optional(<NSLinkCheckingResult: 0x6000031264c0>{0, 14}{http://www.google.com})
    Optional(<NSPhoneNumberCheckingResult: 0x600003f384b0>{17, 14}{(555) 123-4567})
    

    【讨论】:

      猜你喜欢
      • 2016-07-07
      • 2013-09-28
      • 1970-01-01
      • 2013-10-29
      • 1970-01-01
      • 2012-11-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-24
      相关资源
      最近更新 更多