我目前有一个带有地址检测功能的 UITextView,它会检测此字段中的任何地址并将它们变成一个可点击的链接,该链接会以地址为焦点打开 Apple 地图。我真的很喜欢这个功能,但我的用户更愿意点击按钮而不是内联链接。考虑到这一点,我研究了 NSDataDetector 并设法使此功能适用于电子邮件和电话号码,但我被困在地址上。我用来检测地址的代码如下:
let types: NSTextCheckingType = [.Address]
let detector = try! NSDataDetector(types: types.rawValue)
let matches = detector.matchesInString(input, options: [], range: NSMakeRange(0, input.characters.count))
for match in matches {
return NSURL(string: "\((input as NSString).substringWithRange(match.range))")
}
但是 NSURL 无法创建,我相信语法一定是错误的,它适用于电话号码,如下所示:
let types: NSTextCheckingType = [.PhoneNumber]
let detector = try! NSDataDetector(types: types.rawValue)
let matches = detector.matchesInString(input, options: [], range: NSMakeRange(0, input.characters.count))
for match in matches {
return NSURL(string: "telprompt://\((input as NSString).substringWithRange(match.range))")
}
如果地址有效,则返回匹配对象中地址的组成部分(街道、城市等)。我希望 NSURL 可以像电话号码一样与地址一起使用,但我可能错了,所以有没有另一种方法可以使用检测器的结果并手动在 Apple Maps 上显示它们?
或者作为旁注,我可以隐藏 UITextView 并基于按钮触摸以编程方式触发链接触摸吗?
可以根据需要提供更多信息,提前感谢您的帮助。