如何使UITextView hashtag链接打开另一个viewcontroller

时间:2016-06-02 03:48:18

标签: ios swift 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
}

}

我想这样做,以便在点击主题标签时,它会打开我的搜索页面的视图控制器,并在我的应用程序中搜索该主题标签。

我该如何建立连接?

2 个答案:

答案 0 :(得分:1)

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

var tag:String = ""

然后在你的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)

现在,您新呈现的视图控制器将把您存储的数据存储到标记变量

答案 1 :(得分:0)

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

## scale test
set.seed(1L); NR <- 2L*1e5L; ND <- 8L; probG <- 0.25; X1 <- character(NR); cns.grp <- c('DocumentID','Check#'); NG <- length(cns.grp); cns.dat <- c(LETTERS[seq_len(ND-1L)],'Investment$'); X1[seq_len(NG)] <- cns.grp; i <- NG+1L; while (i<=NR-ND+1L) { if (runif(1L)<probG) { X1[seq(i,len=NG)] <- cns.grp; i <- i+NG; } else { X1[seq(i,len=ND)] <- cns.dat; i <- i+ND; }; }; if (i<=NR) { X1[seq(i,NR)] <- cns.grp; }; df <- data.frame(X1=X1,X2=seq_len(NR));

ex <- lapply(bgoldst(df),as.character); o <- names(ex);
identical(ex,lapply(alistaire(df)[o],as.character));
## [1] TRUE

microbenchmark(bgoldst(df),alistaire(df));
## Unit: milliseconds
##           expr       min        lq      mean    median        uq      max neval
##    bgoldst(df)  34.20791  35.90591  47.60333  44.02403  46.78709 119.4467   100
##  alistaire(df) 482.73097 540.84550 568.00577 557.26885 572.44025 741.9781   100
相关问题