Reputation: 761
I am using this code to make my hashtags on my app a link:
import UIKit
extension UITextView {
func resolveHashTags(){
// turn string in to NSString
let nsText:NSString = self.text
// this needs to be an array of NSString. String does not work.
let words:[NSString] = nsText.componentsSeparatedByString(" ")
// you can't set the font size in the storyboard anymore, since it gets overridden here.
let attrs = [
NSFontAttributeName : UIFont.systemFontOfSize(17.0)
]
// you can staple URLs onto attributed strings
let attrString = NSMutableAttributedString(string: nsText as String, attributes:attrs)
// tag each word if it has a hashtag
for word in words {
// found a word that is prepended by a hashtag!
// homework for you: implement @mentions here too.
if word.hasPrefix("#") {
// a range is the character position, followed by how many characters are in the word.
// we need this because we staple the "href" to this range.
let matchRange:NSRange = nsText.rangeOfString(word as String)
// convert the word from NSString to String
// this allows us to call "dropFirst" to remove the hashtag
var stringifiedWord:String = word as String
// drop the hashtag
stringifiedWord = String(stringifiedWord.characters.dropFirst())
// check to see if the hashtag has numbers.
// ribl is "#1" shouldn't be considered a hashtag.
let digits = NSCharacterSet.decimalDigitCharacterSet()
if let numbersExist = stringifiedWord.rangeOfCharacterFromSet(digits) {
// hashtag contains a number, like "#1"
// so don't make it clickable
} else {
// set a link for when the user clicks on this word.
// it's not enough to use the word "hash", but you need the url scheme syntax "hash://"
// note: since it's a URL now, the color is set to the project's tint color
attrString.addAttribute(NSLinkAttributeName, value: "myapplink://\(stringifiedWord)", range: matchRange)
}
}
}
// we're used to textView.text
// but here we use textView.attributedText
// again, this will also wipe out any fonts and colors from the storyboard,
// so remember to re-add them in the attrs dictionary above
self.attributedText = attrString
}
}
I want to make it so that when the hashtag is clicked, it will open the view controller I have for my search page and search that hashtag throughout my app.
How can I do make that connection?
Upvotes: 2
Views: 1275
Reputation: 1794
Use UITextviewDelegate Method to detect the click on hashtag:
// MARK: UITextViewDelegate
func textView(textView: UITextView, shouldInteractWithURL URL: NSURL, inRange characterRange: NSRange) -> Bool {
NSLog("Click on URL\(URL)")
fileName = URL.absoluteString
self.performSegueWithIdentifier("show_doc", sender: self)
return true
}
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "show_doc" {
let vc = segue.destinationViewController as! DocumentVC
vc.fileName = self.fileName as String
}
}
Upvotes: 0
Reputation: 2237
What you can do is in your view controller in which you will search the hashtag, have a variable that can store the information you need.
var tag:String = ""
Then in the your 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 your newly presented view controller will have the data you stored into the tag variable
Upvotes: 1