OHAttributedLabel 在 iPad 上工作得很好,但在 iPhone 上却不行

发布于 2024-12-03 06:55:49 字数 484 浏览 4 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷迭香的记忆 2024-12-10 06:55:49

我是 OHAttributedLabel 类的创建者。感谢您使用它!

正如我们已经通过电子邮件直接回复您的那样,OHAttributedLabel 使用 Apple 的 NSDataDetector来自动检测文本上的链接。

因此,如果存在误识别的链接,尤其是误报,这是由于 Apple 的 NSDataDetector 实现(不幸的是,OHAttributedLabel 对此无能为力) )。 唯一看起来奇怪的是 NSDataDetector 没有检测到 iPad 和 iPhone 上的相同链接......


您可以实施的唯一解决方法,如果如果您对Apple的NSDataDetector找到的链接不满意,请从automaticallyAddLinksForType中删除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's NSDataDetector 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 (and OHAttributedLabel can't do much for this, unfortunately). The only thing that seems strange is that the NSDataDetector 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 the NSTextCheckingTypePhoneNumber value from automaticallyAddLinksForType and find the links on your own, for example using the NSRegularExpression'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

情深已缘浅 2024-12-10 06:55:49

尝试在下面一行,因为我成功运行了代码并且在 iPhone 中也可以工作。

label2.automaticallyAddLinksForType = NSTextCheckingTypeDate|NSTextCheckingTypeAddress|NSTextCheckingTypeLink|NSTextCheckingTypePhoneNumber;

Try just below line because I run code successfully and also works in iPhone.

label2.automaticallyAddLinksForType = NSTextCheckingTypeDate|NSTextCheckingTypeAddress|NSTextCheckingTypeLink|NSTextCheckingTypePhoneNumber;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文