【发布时间】:2021-02-09 09:34:35
【问题描述】:
我正在尝试实现一个可以在打字时处理主题标签的编辑器。
extension UITextView {
func resolveHashTags() {
if self.text.isEmpty {
let emptyString = NSMutableAttributedString(string: " ", attributes: [NSAttributedString.Key.foregroundColor: UIColor.black,
NSAttributedString.Key.font: self.font!])
self.attributedText = emptyString
self.textColor = .black
self.text = ""
return
}
let cursorRange = selectedRange
let nsText = NSString(string: self.text)
let words = nsText.components(separatedBy: CharacterSet(charactersIn: "@#ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_").inverted).filter({!$0.isEmpty})
self.textColor = .black
let attrString = NSMutableAttributedString()
attrString.setAttributedString(self.attributedText)
attrString.addAttributes([NSAttributedString.Key.foregroundColor : UIColor.black], range: nsText.range(of: self.text))
var anchor: Int = 0
for word in words {
// found a word that is prepended by a hashtag!
// homework for you: implement @mentions here too.
let matchRange:NSRange = nsText.range(of: word as String, range: NSRange(location: anchor, length: nsText.length - anchor))
anchor = matchRange.location + matchRange.length
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.
// 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(NSAttributedString.Key.link, value: "hash:\(stringifiedWord)", range: matchRange)
}
} else if !word.hasPrefix("@") {
}
}
self.attributedText = attrString
self.selectedRange = cursorRange
}
}
这是我用来在 UITextView 中创建超链接的扩展。致电func textViewDidChange(_ textView: UITextView)
所以在输入任何以# 开头的单词时。它会打开超链接并将颜色变为蓝色。输入预期的单词后,如果按空格键,它会返回黑色文本。这是预期的行为。
仅保留指向该单词的超链接的任何解决方案。在主题标签之后输入的任何内容都应该是普通文本
【问题讨论】:
-
// so don't make it clickable:然后去掉.link属性,以防万一?还是在else后面} else if !word.hasPrefix("@") { ... } else { removeLinkIfThereWasOne }?如果您的代码在哪里分析“asd”,它在哪里通过?那么它的属性是什么? -
这是唯一生成play textview的属性字符串
-
我需要将其设为链接。因为我在其中存储了一些额外的属性,一旦用户键入所有内容,我就需要这些属性。
-
在某些情况下,我需要将多个单词作为链接。我唯一的问题是,在第一次输入空格时,文本会恢复正常。但是回到链接的词,它会扩展它。
-
我的意思是,当您开始重新输入时,它会将
.link保留为新文本,因此您需要将其删除。在所有else案例中,删除.link属性。所以在 else 中做,或者就在attrString.setAttributedString(self.attributedText):attrString.removeAttribute(.link, range: _NSRange(location: 0, length: attrString.length))之后。
标签: ios swift uitextview nsattributedstring