如何使UITextView标签链接打开另一个视图控制器



我正在使用此代码将我的应用程序上的主题标签设置为链接:

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
}
}
我想这样做,以便在单击主题标签

时,它将打开我为搜索页面准备的视图控制器,并在整个应用程序中搜索该主题标签。

我怎样才能建立这种联系?

您可以做的是在视图控制器中搜索主题标签,有一个可以存储所需信息的变量。

var tag:String = ""

然后在您的文本视图中

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)

现在,新呈现的视图控制器将包含您存储在标签变量中的数据

使用 UITextviewDelegate 方法检测对主题标签的点击:

// 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
    }
}

最新更新