我有以下代码,其中我传递的字符串类型为"Hello|r World|g",并且以下函数将其转换为attributedString,其中"Hello"为彩色red,而"World"为彩色green。我在将每个字符串传递到数组中时使用了它。该功能仅对文本着色,直到找到末尾条件中所示的特殊字符,然后对文本着色。

代码:

func formatAttributedString(string:String)->NSMutableAttributedString {
        var strCopy=string as NSString
        var color:UIColor=UIColor()
        var attributedString:NSMutableAttributedString!

        for var i:Int=0;i<strCopy.length-2;i++ {
            if (string[i] == "|") {
                println("|")
                var j:Int
                if string[i+1] == "r" {
                    color=UIColor(red: 249, green: 39, blue: 14, alpha: 1)
                    strCopy = strCopy.stringByReplacingOccurrencesOfString("|r", withString: "", options: NSStringCompareOptions.LiteralSearch, range: NSMakeRange(0, i + 2))
                    println("r")

                }
               else  if string[i+1] == "v" {
                    color=UIColor(red: 161, green: 153, blue: 249, alpha: 1)
                    strCopy = strCopy.stringByReplacingOccurrencesOfString("|v", withString: "", options: NSStringCompareOptions.LiteralSearch, range: NSMakeRange(0, i + 2))
                    println("v")

                }
                else if string[i+1] == "y" {
                    color=UIColor(red: 235, green: 223, blue: 145, alpha: 1)
                    strCopy = strCopy.stringByReplacingOccurrencesOfString("|y", withString: "", options: NSStringCompareOptions.LiteralSearch, range: NSMakeRange(0, i + 2))
                    println("y")
                }
                else if string[i+1] == "g" {
                    color=UIColor(red: 174, green: 227, blue: 79, alpha: 1)
                    strCopy = strCopy.stringByReplacingOccurrencesOfString("|y", withString: "", options: NSStringCompareOptions.LiteralSearch, range: NSMakeRange(0, i + 2))
                    println("g")
                }
                else if string[i+1] == "b" {
                    color=UIColor(red: 107, green: 224, blue: 240, alpha: 1)
                    strCopy = strCopy.stringByReplacingOccurrencesOfString("|b", withString: "", options: NSStringCompareOptions.LiteralSearch, range: NSMakeRange(0, i + 2))
                    println("b")
                }


                for j=i; j>=0;j-- {
                    if string[j] == " " || string[j] == "/" || string[j] == "." || string[j] == "\"" || string[j] == "\n" || string[j] == "<" || string[j] == "\t" || string[j] == "("{
                        println("/")
                        break

                    }
                }
                attributedString=NSMutableAttributedString(string: strCopy)
                attributedString.addAttribute("NSForegroundColorAttributeName", value: color, range: NSMakeRange(j, i-j))

            }
        }

我收到以下错误:

'NSMutableRLEArray objectAtIndex:effectiveRange::越界'

当我添加了println时,就打印了|r
请帮助,谢谢。

这不是this question的副本,因为|r正在打印。

最佳答案

我试图用Swift的匿名元组和高阶函数的另一个实现来满足您的函数签名。我这样做是为了给自己做练习,最后认为最好与他人分享。

func formatAttributedString(string: String) -> NSMutableAttributedString {
    // create a mapping between the attribute token and the corresponding UIColor
    let colors = [
        "|r": UIColor(red: 249/255, green:  39/255, blue:  14/255, alpha: 1.0),
        "|v": UIColor(red: 161/255, green: 153/255, blue: 249/255, alpha: 1.0),
        "|y": UIColor(red: 235/255, green: 223/255, blue: 145/255, alpha: 1.0),
        "|g": UIColor(red: 174/255, green: 227/255, blue:  79/255, alpha: 1.0),
        "|b": UIColor(red: 107/255, green: 224/255, blue: 240/255, alpha: 1.0)]

    // split argument into an array of (String, UIColor) tuples

    // default the array to the entire argument string with a black color
    var substrings = [(string, UIColor.blackColor())]

    for (token, color) in colors {
        substrings = substrings.flatMap {
            var substrings = $0.0.componentsSeparatedByString(token)
            let tail = (substrings.removeLast(), $0.1)   // tuple for trailing string at old color
            var result = substrings.map{($0, color)}     // array of tuples for strings at new color
            result.append(tail)
            return result
        }
    }
    // because we default the original string to black, there may be an empty string tuple at the end
    substrings = substrings.filter{(string, _) in return !string.isEmpty}

    // convert array of (String, UIColor) tuples into a single attributed string
    var result = reduce(substrings, NSMutableAttributedString(string: "")) {
        var string = NSAttributedString(string: $1.0, attributes: [NSForegroundColorAttributeName: $1.1])
        $0.appendAttributedString(string)
        return $0
    }
    return result
}

关于ios - 错误“NSMutableRLEArray objectAtIndex:effectiveRange::超出范围”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29625452/

10-10 21:14