ios - How to make UITextView hashtag link open another viewcontroller -
i using code make hashtags on app link:
import uikit extension uitextview { func resolvehashtags(){ // turn string in nsstring let nstext:nsstring = self.text // needs array of nsstring. string not work. let words:[nsstring] = nstext.componentsseparatedbystring(" ") // can't set font size in storyboard anymore, since gets overridden here. let attrs = [ nsfontattributename : uifont.systemfontofsize(17.0) ] // can staple urls onto attributed strings let attrstring = nsmutableattributedstring(string: nstext string, attributes:attrs) // tag each word if has hashtag word in words { // found word prepended hashtag! // homework you: implement @mentions here too. if word.hasprefix("#") { // range character position, followed how many characters in word. // need because staple "href" range. let matchrange:nsrange = nstext.rangeofstring(word string) // convert word nsstring string // allows call "dropfirst" remove hashtag var stringifiedword:string = word string // drop hashtag stringifiedword = string(stringifiedword.characters.dropfirst()) // check see if hashtag has numbers. // ribl "#1" shouldn't considered hashtag. let digits = nscharacterset.decimaldigitcharacterset() if let numbersexist = stringifiedword.rangeofcharacterfromset(digits) { // hashtag contains number, "#1" // don't make clickable } else { // set link when user clicks on word. // it's not enough use word "hash", need url scheme syntax "hash://" // note: since it's url now, color set project's tint color attrstring.addattribute(nslinkattributename, value: "myapplink://\(stringifiedword)", range: matchrange) } } } // we're used textview.text // here use textview.attributedtext // again, wipe out fonts , colors storyboard, // remember re-add them in attrs dictionary above self.attributedtext = attrstring } }
i want make when hashtag clicked, open view controller have search page , search hashtag throughout app.
how can make connection?
what can in view controller in search hashtag, have variable can store information need.
var tag:string = ""
then in textview
let storyboard = uistoryboard(name: "yourstoryboard", bundle: nil) let vc:yourviewcontroller = storyboard.instantiateviewcontrollerwithidentifier("yourviewcontrollersid") as! yourviewcontroller vc.tag = attrstring self.presentviewcontroller(vc, animated: true, completion: nil)
now newly presented view controller have data stored tag variable