Ios 如何使UITextView检测哈希标记?
我知道,Ios 如何使UITextView检测哈希标记?,ios,swift,uitextview,Ios,Swift,Uitextview,我知道,UITextViewdefault可以检测URL,但如何让它检测hashtags(#) 它不需要在键入时检测hashtags,但是viewdiload文本是在UITextView中设置的,因此我想将hashtags检测为颜色或其他东西 我一直在使用,但这仅适用于UILabel,我需要UITextView所具有的滚动功能。一个选项是使用nsattributedtingstring,类似这样的功能 func convertHashtags(text:String) -> NSAttri
UITextView
default可以检测URL,但如何让它检测hashtags(#)
它不需要在键入时检测hashtags,但是viewdiload
文本是在UITextView
中设置的,因此我想将hashtags检测为颜色或其他东西
我一直在使用,但这仅适用于
UILabel
,我需要UITextView
所具有的滚动功能。一个选项是使用nsattributedtingstring
,类似这样的功能
func convertHashtags(text:String) -> NSAttributedString {
let attrString = NSMutableAttributedString(string: text)
attrString.beginEditing()
// match all hashtags
do {
// Find all the hashtags in our string
let regex = try NSRegularExpression(pattern: "(?:\\s|^)(#(?:[a-zA-Z].*?|\\d+[a-zA-Z]+.*?))\\b", options: NSRegularExpressionOptions.AnchorsMatchLines)
let results = regex.matchesInString(text,
options: NSMatchingOptions.WithoutAnchoringBounds, range: NSMakeRange(0, text.characters.count))
let array = results.map { (text as NSString).substringWithRange($0.range) }
for hashtag in array {
// get range of the hashtag in the main string
let range = (attrString.string as NSString).rangeOfString(hashtag)
// add a colour to the hashtag
attrString.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor() , range: range)
}
attrString.endEditing()
}
catch {
attrString.endEditing()
}
return attrString
}
let myText = "some text with a #hashtag in side of it #itsnoteasy"
self.textView.attributedText = convertHashtags(myText)
然后像这样分配您的attributeText
func convertHashtags(text:String) -> NSAttributedString {
let attrString = NSMutableAttributedString(string: text)
attrString.beginEditing()
// match all hashtags
do {
// Find all the hashtags in our string
let regex = try NSRegularExpression(pattern: "(?:\\s|^)(#(?:[a-zA-Z].*?|\\d+[a-zA-Z]+.*?))\\b", options: NSRegularExpressionOptions.AnchorsMatchLines)
let results = regex.matchesInString(text,
options: NSMatchingOptions.WithoutAnchoringBounds, range: NSMakeRange(0, text.characters.count))
let array = results.map { (text as NSString).substringWithRange($0.range) }
for hashtag in array {
// get range of the hashtag in the main string
let range = (attrString.string as NSString).rangeOfString(hashtag)
// add a colour to the hashtag
attrString.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor() , range: range)
}
attrString.endEditing()
}
catch {
attrString.endEditing()
}
return attrString
}
let myText = "some text with a #hashtag in side of it #itsnoteasy"
self.textView.attributedText = convertHashtags(myText)
这应该对你有用
UITextViewHashtagExtension.swift
)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
}
}
要使用此功能,您可以执行以下操作:
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.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{
func resolveHashTags(){
//将字符串转换为NSString
设nsText=NSString(字符串:self.text)
//这需要是NSString的数组。字符串不起作用。
让words=nsText.components(由以下字符分隔:CharacterSet(charactersIn:#abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz012456789)。倒置)
//您可以将URL装订到属性字符串上
让attrString=nsmutableAttributeString()
attrString.setAttributedString(self.attributedText)
//如果每个单词都有hashtag,则标记它
一字不差{
如果word.count<3{
持续
}
//找到一个以标签开头的单词!
//你的家庭作业:在这里也实现@提及。
if word.hasPrefix(“#”){
//范围是字符位置,后跟单词中的字符数。
//我们需要这个,因为我们将“href”装订到这个范围。
让matchRange:NSRange=nsText.range(of:word as String)
//放下标签
让stringifiedWord=word.dropFirst()
如果let firstChar=stringifiedWord.unicodeScalars.first,则NSCharacterSet.decimalDigits.contains(firstChar){
//hashtag包含一个数字,如“#1”
//所以不要让它点击
}否则{
//设置用户单击此单词时的链接。
//仅使用“hash”一词是不够的,但您需要url方案语法“hash://”
//注意:由于它现在是URL,因此颜色设置为项目的着色颜色
attrString.addAttribute(NSAttributedStringKey.link,值:“哈希:\(stringifiedWord)”,范围:matchRange)
}
}
}
//我们习惯于textView.text
//但这里我们使用textView.attributedText
//同样,这也会删除故事板上的所有字体和颜色,
//因此,请记住在上面的attrs字典中重新添加它们
self.attributedText=attrString
}
}
适用于Swift 3+
extension UITextView {
func convertHashtags(text:String) -> NSAttributedString {
let attr = [
NSFontAttributeName : UIFont.systemFont(ofSize: 17.0),
NSForegroundColorAttributeName : clr_golden,
NSLinkAttributeName : "https://Laitkor.com"
] as [String : Any]
let attrString = NSMutableAttributedString(string: text)
attrString.beginEditing()
// match all hashtags
do {
// Find all the hashtags in our string
let regex = try NSRegularExpression(pattern: "(?:\\s|^)(#(?:[a-zA-Z].*?|\\d+[a-zA-Z]+.*?))\\b", options: NSRegularExpression.Options.anchorsMatchLines)
let results = regex.matches(in: text,
options: NSRegularExpression.MatchingOptions.withoutAnchoringBounds, range: NSMakeRange(0, text.characters.count))
let array = results.map { (text as NSString).substring(with: $0.range) }
for hashtag in array {
// get range of the hashtag in the main string
let range = (attrString.string as NSString).range(of: hashtag)
// add a colour to the hashtag
//attrString.addAttribute(NSForegroundColorAttributeName, value: clr_golden , range: range)
attrString.addAttributes(attr, range: range)
}
attrString.endEditing()
}
catch {
attrString.endEditing()
}
return attrString
}
}
在类中添加UITextViewDelegate并像这样使用
self.tv_yourTextView.delegate = self
self.tv_yourTextView.attributedText = tv_yourTextView.convertHashtags(text: "This is an #museer test")
代表职能
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange) -> Bool {
print("hastag Selected!")
return true
}
->修改了swift 4.0的@Wez应答
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{
func resolveHashTags(){
//将字符串转换为NSString
设nsText=NSString(字符串:self.text)
//这需要是NSString的数组。字符串不起作用。
让words=nsText.components(由以下字符分隔:CharacterSet(charactersIn:#abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz012456789)。倒置)
//您可以将URL装订到属性字符串上
让attrString=nsmutableAttributeString()
attrString.setAttributedString(self.attributedText)
//如果每个单词都有hashtag,则标记它
一字不差{
如果word.count<3{
持续
}
//找到一个以标签开头的单词!
//你的家庭作业:在这里也实现@提及。
if word.hasPrefix(“#”){
//范围是字符位置,后跟单词中的字符数。
//我们需要这个,因为我们将“href”装订到这个范围。
让matchRange:NSRange=nsText.range(of:word as String)
//放下标签
让stringifiedWord=word.dropFirst()
如果let firstChar=stringifiedWord.unicodeScalars.first,则NSCharacterSet.decimalDigits.contains(firstChar){
//hashtag包含一个数字,如“#1”
//所以不要让它点击
}否则{
//设置用户单击此单词时的链接。
//仅使用“hash”一词是不够的,但您需要url方案语法“hash://”
//注意:由于它现在是URL,因此颜色设置为项目的着色颜色
attrString.addAttribute(NSAttributedStringKey.link,值:“哈希:\(stringifiedWord)”,范围:matchRange)
}
}
}
//我们习惯于textView.text
//但这里我们使用textView.attributedText
//同样,这也会删除故事板上的所有字体和颜色,
//因此,请记住在上面的attrs字典中重新添加它们
self.attributedText=attrString
}
}
你可以在
UIScrollView
@rckoenes中扭曲ActiveLabel
,你的意思是在这里吗?:-这不起作用..检查一下可能会有帮助谢谢!另一个快速问题:如何检测标签上的按压?单击hashtag时执行操作。我完全不知道。hashtag已经可以单击了,因为我们将其添加到链接hash:hashtag中。我们只需要添加textView.delegate=self并侦听func textView(textView:UITextView,应与URL:NSURL交互,在范围字符范围内:NSRange)