ios – 如何让UITextView检测主题标签?

ios – 如何让UITextView检测主题标签?,第1张

概述我知道UITextView默认可以检测到URL,但是如何让它检测到hashtags(#)? 它不需要在键入时检测主题标签,但是然后viewDidLoad文本在UITextView中设置,所以我想将主题标签检测为颜色或其他东西. 我一直在使用ActiveLabel,但这只适用于UILabel,我需要UITextView具有的滚动功能. 这应该适合你 >使用任何名称创建一个新的swift文件(UITe 我知道UITextVIEw默认可以检测到URL,但是如何让它检测到hashTags(#)?

它不需要在键入时检测主题标签,但是然后vIEwDIDLoad文本在UITextVIEw中设置,所以我想将主题标签检测为颜色或其他东西.

我一直在使用ActiveLabel,但这只适用于UILabel,我需要UITextVIEw具有的滚动功能.

解决方法 这应该适合你

>使用任何名称创建一个新的swift文件(UITextVIEwHashtagExtension.swift)
>在下面插入以下代码:

import UIKitextension 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}}

要使用它,您可以执行以下 *** 作:

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

针对Swift 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.unicodeSca@R_419_6639@.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,range: matchRange)                }            }        }        // we're used to textVIEw.text        // but here we use textVIEw.attributedText        // again,// so remember to re-add them in the attrs dictionary above        self.attributedText = attrString    }}
总结

以上是内存溢出为你收集整理的ios – 如何让UITextView检测主题标签?全部内容,希望文章能够帮你解决ios – 如何让UITextView检测主题标签?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1109626.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存