IOS text free Copy Hyperlink listener

Source: Internet
Author: User

The best scenario for a label that needs to support replication and hyperlink snooping is to use Uitextview instead of a label

Set up TextView support hyperlinks

  

TextView default Hyperlink Click is applied outside the jump to implement in-app jump, you need to follow the Uitextviewdelegate implementation Uitextviewdelegate Proxy method, intercept to hyperlink URL yourself in the app to do jump
-(BOOL) TextView: (Uitextview *) TextView Shouldinteractwithurl: (Nsurl *) URL InRange: (nsrange) CharacterRange {    if ( [Self.delegate respondstoselector: @selector (clickurl:)]) {        [self.delegate clickurl:[url.absolutestring stringbyreplacingpercentescapesusingencoding: Nsutf8stringencoding]];    }    NSLog (@ "%@", url.absolutestring);    return NO;  }

  

-(BOOL) TextView: (Uitextview *) TextView Shouldinteractwithurl: (Nsurl *) URL InRange: (nsrange) CharacterRange return YES In addition to listening to tap events or listening to long press the event long press the pop-up copy Share button, but click Open will jump to the outside of the app opens the link, return no only listen to the linked click event does not listen to other events such as long press the pop-up Copy share button, so that we can customize Pop-up button, then do the copy and in-app open links and other actions
-(BOOL) TextView: (Uitextview *) TextView Shouldinteractwithurl: (Nsurl *) URL InRange: (nsrange) CharacterRange {    if ( [Self.delegate respondstoselector: @selector (clickurl:)]) {        [self.delegate clickurl:[url.absolutestring stringbyreplacingpercentescapesusingencoding: Nsutf8stringencoding]];    }    return NO;}

  

Controller inside the implementation of the agent

-(void) Clickurl: (NSString *) URL {uialertcontrollerstyle style = Uialertcontrollerstyleactionsheet;    if ([Phoneutil Ispaddevice]) {//fit ipad style = Uialertcontrollerstylealert;    } Uialertcontroller *ALERTVC = [Uialertcontroller alertcontrollerwithtitle:url message:nil PreferredStyle:style]; Processing replication Uialertaction *copyaction = [uialertaction Actionwithtitle:getstringbykey (@ "string_key_616") style: Uialertactionstyledefault handler:^ (uialertaction * _nonnull action) {Uipasteboard *pasteboard = [UIPasteboard gen        Eralpasteboard];    pasteboard.string = URL;    }]; Handle link Click in App jump Uialertaction *openaction = [uialertaction Actionwithtitle:getstringbykey (@ "string_key_1370") style: Uialertactionstyledefault handler:^ (uialertaction * _nonnull action) {Uistoryboard *sb=[uistoryboard storyboardWit        hname:@ "Setting" Bundle:[nsbundle Mainbundle];        WEBVIEWVC *vc=[sb instantiateviewcontrollerwithidentifier:@ "WEBVIEWVC"];  Vc.url = URL;      [Self.navigationcontroller PUSHVIEWCONTROLLER:VC Animated:yes];    }]; Uialertaction *cancelaction = [uialertaction Actionwithtitle:getstringbykey (@ "string_key_219") style:        Uialertactionstylecancel Handler:nil];    [ALERTVC addaction:copyaction];    [ALERTVC addaction:openaction];    [ALERTVC addaction:cancelaction]; [Self.navigationcontroller PRESENTVIEWCONTROLLER:ALERTVC Animated:yes completion:nil];}

  

IOS text free Copy Hyperlink listener

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.