OHAttributedLabel 在 iPad 上工作得很好,但在 iPhone 上却不行
我正在使用 NSTextCheckingResult 类将文本显示为链接。我正在使用这个 OHAttributedLabels 来使用它。
我只想检测电话号码和电子邮件 ID。也就是说,我只希望电话号码和电子邮件 ID 作为链接突出显示。
现在的问题是,下面的代码对于 iPad 工作得非常好,但对于 iPhone 则不行:
self.automaticallyAddLinksForType = NSTextCheckingTypeDate|NSTextCheckingTypeLink|NSTextCheckingTypePhoneNumber;
但是相同的代码甚至在 iPhone 中将 5 位数字的邮政编码显示为链接。它在 iPad 上运行得非常好。可能出什么问题了?
I am using NSTextCheckingResult class to show text as links. I am using this OHAttributedLabels to work with it.
I want detection only for phone numbers and email id. That is I only want Phone numbers and email ids to get highlighted as links
Now the problem is that the below code works perfectly fine for iPad but not for iPhone:
self.automaticallyAddLinksForType = NSTextCheckingTypeDate|NSTextCheckingTypeLink|NSTextCheckingTypePhoneNumber;
But the same code shows even 5 digit ZipCodes as as a link in iPhone. It works perfectly fine in iPad. What could be wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我是 OHAttributedLabel 类的创建者。感谢您使用它!
正如我们已经通过电子邮件直接回复您的那样,
OHAttributedLabel
使用 Apple 的NSDataDetector
类来自动检测文本上的链接。因此,如果存在误识别的链接,尤其是误报,这是由于 Apple 的
NSDataDetector
实现(不幸的是,OHAttributedLabel
对此无能为力) )。 唯一看起来奇怪的是NSDataDetector
没有检测到 iPad 和 iPhone 上的相同链接......您可以实施的唯一解决方法,如果如果您对Apple的automaticallyAddLinksForType中删除
NSDataDetector
找到的链接不满意,请从NSTextCheckingTypePhoneNumber
值并自行查找链接,例如使用NSRegularExpression
的类。无论如何,如果您打算这样做,请小心,因为手动检测电话号码并不简单,而且有点棘手。特别是,电话号码的格式取决于电话号码所在的国家/地区(美国电话的格式与法国或英国电话的格式不同),它们的格式可以使用空格或破折号(或不使用),可以采用国际格式(+336 07...) 等等...
实际上,这可能是由于这种复杂性,Apple 的
NSDataDetector
无法避免误报匹配...HTH
I'm the creator of the OHAttributedLabel class. Thanks for using it!
As already answered directly to you by email,
OHAttributedLabel
uses Apple'sNSDataDetector
class to automatically detect links on a text.So if there are misrecognised links, especially false positives, that's due to Apple's
NSDataDetector
implementation (andOHAttributedLabel
can't do much for this, unfortunately). The only thing that seems strange is that theNSDataDetector
does not detect the same links on iPad and iPhone…The only workaround you can implement, if you are not satisfied with links found by Apple's
NSDataDetector
, is to remove theNSTextCheckingTypePhoneNumber
value fromautomaticallyAddLinksForType
and find the links on your own, for example using theNSRegularExpression
's class.Anyway if you intend to, be careful as detecting the phone numbers manually is not trivial and kinda tricky. Especially, the format of phone numbers depends on the country the phone number is for (US phones are not formatted the same as French or UK ones), they may be formatted using spaces or dashes (or not), they can be in international format (+336 07...) and so on…
Actually, that's probably because of this complexity that Apple's
NSDataDetector
can't avoid false positive matches…HTH
尝试在下面一行,因为我成功运行了代码并且在 iPhone 中也可以工作。
Try just below line because I run code successfully and also works in iPhone.