如何更改 textView 中预选单词的样式? [英] how can I change style of pre-selected words in my textView?
问题描述
这是对这个问题的跟进 如何在 Swift 中一一更改 UITextView 中某些单词的样式?
感谢@Josh 的帮助,我能够编写一段代码,突出显示以 #
开头的每个单词 - 并且一个一个地完成.我的最终代码是:
func highlight (to index: Int) {让正则表达式 = 试试?NSRegularExpression(pattern: "#(\\w+)", options: [])让matches = regex!.matches(in: hashtagExplanationTextView.text, options: [], range: NSMakeRange(0, (hashtagExplanationTextView.text.characters.count)))让 titleDict: NSDictionary = [NSForegroundColorAttributeName: orangeColor]让 titleDict2: NSDictionary = [NSForegroundColorAttributeName: UIColor.red]let storedAttributedString = NSMutableAttributedString(string: hashtagExplanationTextView.text!, attributes: titleDict as! [String : AnyObject])让attributedString = NSMutableAttributedString(attributedString:storedAttributedString)保护指数<匹配.计数其他{返回}对于 i 在 0..
这很好用,但我想更改逻辑,使其不突出显示 #
单词,而是突出显示(一个接一个)这些单词的预选数组中的单词.
所以我有这个数组 var myArray:[String] = ["these","words","are","highlighted"]
以及如何将它而不是正则表达式匹配放入我的代码?
我相信您正在使用正则表达式来获取 NSRange
的数组.在这里,您需要一个稍微不同的数据结构,例如 [String : [NSRange]]
.然后你可以使用rangeOfString
函数来检测单词所在的NSRange
.您可以按照下面给出的示例进行操作:
let wordMatchArray:[String] = ["these", "words", "are", "highlighted"]让 labelText:NSString = NSString(string: "那些单词,那些 ldsnvldnvsdnds,是,突出显示,单词被突出显示")让 textLength:Int = labelText.lengthvar dictionaryForEachWord:[String : [NSRange]] = [:]对于每个字:wordMatchArray 中的字符串{var prevRange:NSRange = NSMakeRange(0, 0)var rangeArray:[NSRange] = []而 ((prevRange.location + prevRange.length)
既然您有一个 NSRange
数组,即字典中存储的每个单词的 [NSRange],您可以在 UITextView
中相应地突出显示每个单词.>
如果您对实施有任何疑问,请随时发表评论:)
This is a follow up to this question How can I change style of some words in my UITextView one by one in Swift?
Thanks to @Josh's help I was able to write a piece of code that highlights each word that begins with #
- and do it one by one. My final code for that was:
func highlight (to index: Int) {
let regex = try? NSRegularExpression(pattern: "#(\\w+)", options: [])
let matches = regex!.matches(in: hashtagExplanationTextView.text, options: [], range: NSMakeRange(0, (hashtagExplanationTextView.text.characters.count)))
let titleDict: NSDictionary = [NSForegroundColorAttributeName: orangeColor]
let titleDict2: NSDictionary = [NSForegroundColorAttributeName: UIColor.red]
let storedAttributedString = NSMutableAttributedString(string: hashtagExplanationTextView.text!, attributes: titleDict as! [String : AnyObject])
let attributedString = NSMutableAttributedString(attributedString: storedAttributedString)
guard index < matches.count else {
return
}
for i in 0..<index{
let matchRange = matches[i].rangeAt(0)
attributedString.addAttributes(titleDict2 as! [String : AnyObject], range: matchRange)
}
hashtagExplanationTextView.attributedText = attributedString
if #available(iOS 10.0, *) {
let _ = Timer.scheduledTimer(withTimeInterval: 1, repeats: false) { _ in
self.highlight(to: index + 1)
}
} else {
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
self.highlight(to: index + 1)
}
}
}
This works fine, but I would like to change the logic so that it does not highlight the #
words, but highlights (one by one) words from preselected array of those words.
So I have this array var myArray:[String] = ["those","words","are","highlighted"]
and how can I put it instead of regex match in my code?
I believe you are using regex to get an array of NSRange
. Here, you need a slightly different datastructure like [String : [NSRange]]
. Then you can use rangeOfString
function to detect the NSRange
where the word is located. You can follow the example given below for that:
let wordMatchArray:[String] = ["those", "words", "are", "highlighted"]
let labelText:NSString = NSString(string: "those words, those ldsnvldnvsdnds, are, highlighted,words are highlighted")
let textLength:Int = labelText.length
var dictionaryForEachWord:[String : [NSRange]] = [:]
for eachWord:String in wordMatchArray {
var prevRange:NSRange = NSMakeRange(0, 0)
var rangeArray:[NSRange] = []
while ((prevRange.location + prevRange.length) < textLength) {
let start:Int = (prevRange.location + prevRange.length)
let rangeEach:NSRange = labelText.range(of: eachWord, options: NSString.CompareOptions.literal, range: NSMakeRange(start, textLength-start))
if rangeEach.length == 0 {
break
}
rangeArray.append(rangeEach)
prevRange = rangeEach
}
dictionaryForEachWord[eachWord] = rangeArray
}
Now that you have an array of NSRange
i.e, [NSRange] for each word stored in a dictionary, you can highlight each word accordingly in your UITextView
.
Feel free to comment if you have any doubts regarding the implementation :)
这篇关于如何更改 textView 中预选单词的样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!