【发布时间】:2015-04-07 08:55:56
【问题描述】:
所以我正在开发一个应用程序来向用户显示一些信息。
我正在尝试获取一个地址以显示为用户的链接,并允许用户在单击该位置时导航到某个位置。最简单的方法是为此使用 UITextView,并且使用这种方法我已经能够让 UITextView 检测电话号码和 URL,但不是位置/地址。
我是这样设置文字的:
locationLabel.scrollEnabled = true
locationLabel.text = est.getAddressString()
locationLabel.makeAddress()
locationLabel.sizeToFit()
locationLabel.scrollEnabled = false
makeAddress 方法执行以下操作:
internal func makeAddress() {
// see https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIKitDataTypesReference/#//apple_ref/c/tdef/UIDataDetectorTypes
dataDetectorTypes = UIDataDetectorTypes.Address
var mutString: NSMutableAttributedString = NSMutableAttributedString(string: self.text!)
mutString.addAttribute(NSUnderlineStyleAttributeName, value: NSUnderlineStyle.StyleSingle.rawValue, range: NSMakeRange(0, mutString.length))
self.attributedText = mutString
}
我必须检测的地址例如:Albinusdreef 2 2333ZA Leiden Netherlands。但是 UITextView 似乎无法做到这一点。
编辑
是不是我必须调整的地址格式?还是就像Android一样,它只能检测美国地址? (刚刚测试,美国地址有效)
【问题讨论】:
标签: swift uitextview