17

このコード

var textSearch="hi"
var textToShow="hi hihi hi" 
var rangeToColor = (textToShow as NSString).rangeOfString(textSearch)
var attributedString = NSMutableAttributedString(string:textToShow)
attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.yellowColor() , range: rangeToColor)
TextView.attributedText=attributedString

NSRange で TextView 内の文字列に色を付けることができます。問題は、最初に出現したものだけを返すことです。単語に「ひひひひ」が含まれている場合、最初の「ひ」だけが色付けされます。文字列のすべての出現を取得するにはどうすればよいですか?

4

6 に答える 6

28

スイフト5

let attrStr = NSMutableAttributedString(string: "hi hihi hey")
let inputLength = attrStr.string.count
let searchString = "hi"
let searchLength = searchString.characters.count
var range = NSRange(location: 0, length: attrStr.length)

while (range.location != NSNotFound) {
    range = (attrStr.string as NSString).range(of: searchString, options: [], range: range)
    if (range.location != NSNotFound) {
        attrStr.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.yellow, range: NSRange(location: range.location, length: searchLength))
        range = NSRange(location: range.location + range.length, length: inputLength - (range.location + range.length))
    }
}

スイフト3

let attrStr = NSMutableAttributedString(string: "hi hihi hey")
let inputLength = attrStr.string.characters.count
let searchString = "hi"
let searchLength = searchString.characters.count
var range = NSRange(location: 0, length: attrStr.length)

while (range.location != NSNotFound) {
    range = (attrStr.string as NSString).range(of: searchString, options: [], range: range)
    if (range.location != NSNotFound) {
        attrStr.addAttribute(NSForegroundColorAttributeName, value: UIColor.yellow(), range: NSRange(location: range.location, length: searchLength))
        range = NSRange(location: range.location + range.length, length: inputLength - (range.location + range.length))
    }
}

スイフト2

let attrStr = NSMutableAttributedString(string: "hi hihi hey")
let inputLength = attrStr.string.characters.count
let searchString = "hi"
let searchLength = searchString.characters.count
var range = NSRange(location: 0, length: attrStr.length)

while (range.location != NSNotFound) {
    range = (attrStr.string as NSString).rangeOfString(searchString, options: [], range: range)
    if (range.location != NSNotFound) {
        attrStr.addAttribute(NSForegroundColorAttributeName, value: UIColor.yellowColor(), range: NSRange(location: range.location, length: searchLength))
        range = NSRange(location: range.location + range.length, length: inputLength - (range.location + range.length))
    }
}
于 2014-11-28T01:16:00.013 に答える
0

Swift 4.2で拡張機能を作成しました

extension NSMutableAttributedString {
// Adds attributes EVERY TIME the text to change appears
func addAttributes(_ attributes: [NSAttributedString.Key: NSObject], forText text: String) {
    var range = NSRange(location: 0, length: self.length)
    while (range.location != NSNotFound) {
        range = (self.string as NSString).range(of: text, options: [], range: range)
        if (range.location != NSNotFound) {
            self.addAttributes(attributes, range: NSRange(location: range.location, length: text.count))
            range = NSRange(location: range.location + range.length, length: self.string.count - (range.location + range.length))
        }
    }
}

これで、次のように呼び出すことができます。

let attributedString = NSMutableAttributedString(attributedString: textView.attributedText)
let myAttributes = [/* your attributes here */]
attributedString.addAttributes(myAttributes, forText: /* your text here */)
于 2018-07-14T04:07:40.020 に答える