简体   繁体   English

OHAttributedLabel在iPad上可以正常使用,但在iPhone上则不能

[英]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.

相关问题 iPhone应用程序可以在iPad上正常运行,但不能在iPhone上运行 - iPhone app works fine on iPad, but not on iPhone UICollectionView布局在iPad上崩溃,但在iPhone上运行良好 - UICollectionView layout crashes on iPad, but works fine on iPhone iPad返回上一视图可终止该应用程序,同时可在iPhone上完美运行 - iPad back to previous view terminates the application while works perfectly on iPhone ios,ipad,MBProgressHUD在iphone上运行正常,但在ipad中无法正常运行 - ios, ipad , MBProgressHUD works fine iphone, but not working properly in ipad iphone ipad xib无法完美加载 - iphone ipad xib not loading perfectly 自定义“可滑动” UIScrollView在iPad上无法正常运行-在iPhone上可以正常运行 - Custom “Swipeable” UIScrollView does not work properly on iPad - works fine on iPhone “可执行文件在iPad上使用无效授权签署”,在iPhone上运行正常 - 'The executable was signed with invalid entitlements' on iPad, works fine on iPhone AVAudioPlayer在iPhone上可以正常使用,但不能在iPad上播放吗? - AVAudioPlayer works fine on iPhone, but doesn't play on iPad? 状态栏可以旋转,但应用程序不能在iPhone上运行-在iPad上可以正常使用吗? - Status bar rotates but application does not, on iPhone- works fine on iPad? 应用程序在iPhone / iPod上运行良好,但在iPad(甚至模拟器)上崩溃 - App works fine on iPhone/ iPod but crashes on iPad (even simulator)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM