如何让UITextView检测主题标签? [英] How to make UITextView detect hashtags?

查看:86
本文介绍了如何让UITextView检测主题标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 UITextView 默认可以检测到网址,但是如何让它检测到主题标签(#)?

I know that the UITextView default can detect URL, but how can i make it detect hashtags(#)?

输入时不需要检测主题标签,但是 viewDidLoad 文本设置在 UITextView ,所以我想将主题标签检测为颜色或其他东西。

It doesn't needs to detect hashtags while typing, but then viewDidLoad the text is set in the UITextView, so i want to detect hashtags as a color or something.

我一直在使用 ActiveLabel ,但这仅适用于 UILabel ,我需要 UITextView的滚动功能 has。

I have been using ActiveLabel, but that is only for UILabel, and i need the scroll function that the UITextView has.

推荐答案

这应该对你有用



This should work for you


  1. 使用任何名称创建一个新的swift文件( UITextViewHashtagExtension.swift

  2. 插入以下代码:

  1. Create a new swift file with any name(UITextViewHashtagExtension.swift)
  2. Insert this code below:

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: "hash:\(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
}

}







要使用此功能,您可以执行某些操作像这样:


To use this you can do something like this:

self.textView.text = "This is an #example test"
self.textView.resolveHashTags()






针对Swif更新t 4.0:

extension UITextView {

    func resolveHashTags() {

        // turn string in to NSString
        let nsText = NSString(string: self.text)

        // this needs to be an array of NSString.  String does not work.
        let words = nsText.components(separatedBy: CharacterSet(charactersIn: "#ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_").inverted)

        // you can staple URLs onto attributed strings
        let attrString = NSMutableAttributedString()
        attrString.setAttributedString(self.attributedText)

        // tag each word if it has a hashtag
        for word in words {
            if word.count < 3 {
                continue
            }

            // 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.range(of: word as String)

                // drop the hashtag
                let stringifiedWord = word.dropFirst()
                if let firstChar = stringifiedWord.unicodeScalars.first, NSCharacterSet.decimalDigits.contains(firstChar) {
                    // 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(NSAttributedStringKey.link, value: "hash:\(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
    }
}

这篇关于如何让UITextView检测主题标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆