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