NSAttributedString 中的一个单词有两种不同的颜色
Two different colors in one word in an NSAttributedString
我能否以某种方式为 NSAttributedString
中的单个单词添加多种颜色?换句话说,我想为特定的单词添加颜色,颜色之间有或没有 space,例如 <red>app</red><blue>ple</blue>
。这样苹果就有两种颜色。我附上了从此代码中删除 space 之前和之后发生的事情的照片:
这是我正在使用的代码。
import UIKit
import Foundation
class ViewController: UIViewController {
@IBOutlet var textView: UITextField!
@IBOutlet var textBox: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.textView.attributedText = self.a("the only <number>number</number>one which has no space")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func a(text:String) -> NSMutableAttributedString{
var string:NSMutableAttributedString = NSMutableAttributedString(string: text)
var words:[NSString] = text.componentsSeparatedByString(" ")
for (var word:NSString) in words {
if (word.hasPrefix("<number>") && word.hasSuffix("</number>")) {
var range: NSRange = (string.string as NSString).rangeOfString(word as String)
string.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: range)
word = word.stringByReplacingOccurrencesOfString("<number>", withString: "")
word = word.stringByReplacingOccurrencesOfString("</number>", withString: "")
string.replaceCharactersInRange(range, withString: word as String)
}
}
return string
}
}
我也在这个线程中找到了代码Is it possible to change color of single word in UITextView and UITextField
我琢磨了一天,不过\0是诀窍。
NSMutableAttributedString(string: text) var words:[NSString] = text.componentsSeparatedByString("[=10=]")
我能否以某种方式为 NSAttributedString
中的单个单词添加多种颜色?换句话说,我想为特定的单词添加颜色,颜色之间有或没有 space,例如 <red>app</red><blue>ple</blue>
。这样苹果就有两种颜色。我附上了从此代码中删除 space 之前和之后发生的事情的照片:
这是我正在使用的代码。
import UIKit
import Foundation
class ViewController: UIViewController {
@IBOutlet var textView: UITextField!
@IBOutlet var textBox: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.textView.attributedText = self.a("the only <number>number</number>one which has no space")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func a(text:String) -> NSMutableAttributedString{
var string:NSMutableAttributedString = NSMutableAttributedString(string: text)
var words:[NSString] = text.componentsSeparatedByString(" ")
for (var word:NSString) in words {
if (word.hasPrefix("<number>") && word.hasSuffix("</number>")) {
var range: NSRange = (string.string as NSString).rangeOfString(word as String)
string.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: range)
word = word.stringByReplacingOccurrencesOfString("<number>", withString: "")
word = word.stringByReplacingOccurrencesOfString("</number>", withString: "")
string.replaceCharactersInRange(range, withString: word as String)
}
}
return string
}
}
我也在这个线程中找到了代码Is it possible to change color of single word in UITextView and UITextField
我琢磨了一天,不过\0是诀窍。
NSMutableAttributedString(string: text) var words:[NSString] = text.componentsSeparatedByString("[=10=]")