IOS-->> Add links to Uitextview

Source: Internet
Author: User

Now it's easier to add your own Twitter account on iOS, now you can add a link to a nsattributedstring and then invoke a custom action when it's clicked. First, create a nsattributedstring and then add a nslinkattributename attribute to it, see the following:
nsmutableattributedstring *attributedstring = [[Nsmutableattributedstring alloc] initwithstring:@ "This is a example by @marcelofabri_ "]; [Attributedstring addattribute:nslinkattributename value:@"Username://marcelofabri_"range:[[attributedstring string] rangeofstring:@"@marcelofabri_"]]; Nsdictionary*linkattributes =@{nsforegroundcolorattributename: [Uicolor Greencolor], Nsunderlinecolorattributenam   E: [Uicolor Lightgraycolor], Nsunderlinestyleattributename: @ (Nsunderlinepatternsolid)}; //assume that TextView was a uitextview previously created (either by code or Interface Builder)Textview.linktextattributes = linkattributes;//customizes the appearance of linksTextview.attributedtext =attributedstring; Textview.delegate= self;

This allows the link to be displayed in the text. However, you can also control what happens when a link is clicked, Implementing a new Shouldinteractwithurl method that can use the Uitextviewdelegate protocol, like this:

-(BOOL) TextView: (Uitextview *) TextView Shouldinteractwithurl: (Nsurl *) URL InRange: (nsrange) CharacterRange {      if ([[[URL scheme] isequaltostring:@ "username"])         {*username = [url host]          ; // Do something with this username          //         return  NO;     }      return //

IOS-->> Add links to Uitextview

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.