Reputation: 19469
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?
Upvotes: 1
Views: 1108
Reputation: 32681
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
Upvotes: 2
Reputation: 3402
Try just below line because I run code successfully and also works in iPhone.
label2.automaticallyAddLinksForType = NSTextCheckingTypeDate|NSTextCheckingTypeAddress|NSTextCheckingTypeLink|NSTextCheckingTypePhoneNumber;
Upvotes: 1