Swift UITextView更改特定文本的文本颜色
我想更改UITextView中与数组索引匹配的特定文本的文本颜色。我可以稍微修改一下,但不幸的是,每个匹配短语的文本颜色只改变了一次Swift UITextView更改特定文本的文本颜色,swift,uitextview,swift2.1,Swift,Uitextview,Swift2.1,我想更改UITextView中与数组索引匹配的特定文本的文本颜色。我可以稍微修改一下,但不幸的是,每个匹配短语的文本颜色只改变了一次 var chordsArray = ["Cmaj", "Bbmaj7"] func getColoredText(textView: UITextView) -> NSMutableAttributedString { let text = textView.text let string:NSMutableAttributedString
var chordsArray = ["Cmaj", "Bbmaj7"]
func getColoredText(textView: UITextView) -> NSMutableAttributedString {
let text = textView.text
let string:NSMutableAttributedString = NSMutableAttributedString(string: text)
let words:[String] = text.componentsSeparatedByString(" ")
for word in words {
if (chordsArray.contains(word)) {
let range:NSRange = (string.string as NSString).rangeOfString(word)
string.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: range)
}
}
chords.attributedText = string
return string
}
结果对不起,我刚注意到你的留言。下面是一个工作示例(在操场上测试):
万一有人在swift 4中需要它。这是我从我的Xcode 9游乐场得到的:) Swift 4.2和5
let string = "* Your receipt photo was not clear or did not capture the entire receipt details. See our tips here.\n* Your receipt is not from an eligible grocery, convenience or club store."
let attributedString = NSMutableAttributedString.init(string: string)
let range = (string as NSString).range(of: "See our tips")
attributedString.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.blue, range: range)
txtView.attributedText = attributedString
txtView.isUserInteractionEnabled = true
txtView.isEditable = false
输出
NSAttributedString
是您的答案。对要着色的零件使用着色属性,并使其成为一个字符串。将其余部分放在纯属性字符串中。现在将这两个AttributedString组合起来。@NSNoob不想打扰您,但您能提供一个示例吗?我很难理解如何将包含的文本和纯文本拆分为两个不同的NSAttRubttedString。您正在编辑textView吗?我能看到下面有一个键盘吗?所以我假设您希望在用户输入和弦数组中定义的关键字时为文本着色?我注意到了键盘,所以我想你真正想要的就是我刚才描述的?在数据输入时重新调用?是的,我是,这是正确的@NSNoobYes我是,函数在TextDidChange上被调用我无法使此示例在Swift 2/Xcode 7中工作。如果可以,我只需要使用此代码并按原样使用它。对于那些对NSAttributedString.Key.foregroundColor部分代码有问题的人,改用NSForegroundColorAttributeName
import UIKit
import PlaygroundSupport
class MyViewController : UIViewController
{
override func loadView()
{
let view = UIView()
view.backgroundColor = .white
let textView = UITextView()
textView.frame = CGRect(x: 150, y: 200, width: 200, height: 20)
textView.text = "@Kam @Jam @Tam @Ham"
textView.textColor = .black
view.addSubview(textView)
self.view = view
let query = "@"
if let str = textView.text {
let text = NSMutableAttributedString(string: str)
var searchRange = str.startIndex..<str.endIndex
while let range = str.range(of: query, options: NSString.CompareOptions.caseInsensitive, range: searchRange) {
text.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.gray, range: NSRange(range, in: str))
searchRange = range.upperBound..<searchRange.upperBound
}
textView.attributedText = text
}
}
}
PlaygroundPage.current.liveView = MyViewController()
let start = str.distance(from: str.startIndex, to: range.lowerBound)
let len = str.distance(from: range.lowerBound, to: range.upperBound)
let nsrange = NSMakeRange(start, len)
text.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.gray, range: nsrange)
let string = "* Your receipt photo was not clear or did not capture the entire receipt details. See our tips here.\n* Your receipt is not from an eligible grocery, convenience or club store."
let attributedString = NSMutableAttributedString.init(string: string)
let range = (string as NSString).range(of: "See our tips")
attributedString.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.blue, range: range)
txtView.attributedText = attributedString
txtView.isUserInteractionEnabled = true
txtView.isEditable = false