A drop-in replacement for UITextView link interaction actions
UITextView has the ability to interact with various types of links in text. In addition to opening a responsible app for handling a custom URL, it provides a set of actions for standard link types including web URLs, phone numbers, addresses and dates.
Unfortunately, there is no way to customize it’s behavior other than disable it completely via -textView:shouldInteractWithURL:inRange:
And since all the trickery is done inside a private framework without any public API exposed to programmer, there’s no way to use these outside of UITextView as well.
MYLinkInteraction provides a full set of actions available in UITextView, but doesn’t tie itself to any particular UI. It can be used with UILabel, TTTAttributedLabel or even with UITextView itself.
- iOS 8.0+
- ARC
From CocoaPods:
pod 'MYLinkInteraction'
Import all the things:
#import <MYLinkInteraction/MYLinkInteraction.h>
Assuming you already have a code which parses a link and gets NSTextCheckingResult. The usage is really simple then.
In case with TTTAttributedLabel:
- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithTextCheckingResult:(NSTextCheckingResult *)result {
MYLinkInteractionHandler *handler = [MYLinkInteractionHandler new];
MYLinkData *linkData = [[MYLinkData alloc] initWithLinkText:label.text textCheckingResult:result];
[handler handlePressWithLinkData:linkData popoverContext:nil];
}
- (void)attributedLabel:(TTTAttributedLabel *)label didLongPressLinkWithTextCheckingResult:(NSTextCheckingResult *)result atPoint:(CGPoint)point {
MYLinkInteractionHandler *handler = [MYLinkInteractionHandler new];
MYLinkData *linkData = [[MYLinkData alloc] initWithLinkText:label.text textCheckingResult:result];
[handler handleLongPressWithLinkData:linkData popoverContext:nil];
}
See example app for details.
MYLinkInteraction
is released under an MIT License. See LICENSE file for details.