[英]OHAttributedLabel works perfectly fine on iPad but not on iPhone
I am using NSTextCheckingResult class to show text as links. 我正在使用NSTextCheckingResult类将文本显示为链接。 I am using this OHAttributedLabels to work with it.
我正在使用此OHAttributedLabels与它一起使用。
I want detection only for phone numbers and email id. 我只想检测电话号码和电子邮件ID。 That is I only want Phone numbers and email ids to get highlighted as links
那就是我只希望电话号码和电子邮件ID突出显示为链接
Now the problem is that the below code works perfectly fine for iPad but not for iPhone: 现在的问题是,以下代码对于iPad而言可以正常使用,但对于iPhone而言则不能:
self.automaticallyAddLinksForType = NSTextCheckingTypeDate|NSTextCheckingTypeLink|NSTextCheckingTypePhoneNumber;
But the same code shows even 5 digit ZipCodes as as a link in iPhone. 但是相同的代码甚至显示5位ZipCode作为iPhone中的链接。 It works perfectly fine in iPad.
它在iPad上运行良好。 What could be wrong?
可能有什么不对?
I'm the creator of the OHAttributedLabel class. 我是OHAttributedLabel类的创建者。 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. 正如已经通过电子邮件直接答复您一样,
OHAttributedLabel
使用Apple的NSDataDetector
类自动检测文本上的链接。
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). 因此,如果存在错误识别的链接,尤其是误报, 那是由于Apple的
NSDataDetector
实现 (不幸的是, OHAttributedLabel
对此无能为力)。 The only thing that seems strange is that the NSDataDetector
does not detect the same links on iPad and iPhone… 唯一奇怪的是,
NSDataDetector
不能在iPad和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. 如果您对Apple的
NSDataDetector
找到的链接不满意,则可以实现的唯一解决方法是从automaticallyAddLinksForType
删除NSTextCheckingTypePhoneNumber
值并自行查找链接,例如使用NSRegularExpression
的类。
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…
特别是,电话号码的格式取决于电话号码所在的国家/地区(美国电话的格式与法国或英国的电话格式不同),它们的格式可以使用空格或破折号(或不使用破折号),也可以采用国际格式(+336 07 ...)等...
Actually, that's probably because of this complexity that Apple's NSDataDetector
can't avoid false positive matches… 实际上,这可能是因为Apple
NSDataDetector
无法避免误报而导致的复杂性……
HTH HTH
请尝试在下面的行中进行操作,因为我可以成功运行代码并且也可以在iPhone中运行。
label2.automaticallyAddLinksForType = NSTextCheckingTypeDate|NSTextCheckingTypeAddress|NSTextCheckingTypeLink|NSTextCheckingTypePhoneNumber;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.