I have the following code, in which I pass a string with of the type "Hello|r World|g"
and the following function converts it into attributedString
with "Hello"
Being red
in color and "World"
being green
in color. I used this as I am passing each of the strings in an array. The function only colors the text until it find a special character as shown in the condition during the end and then colors the text.
Code :
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))
}
}
I get the following error :
'NSMutableRLEArray objectAtIndex:effectiveRange:: Out of bounds'
As I have added println
s , |
and r
get printed.
Please help,Thanks in advance.
It's not a duplicate of this question as |
and r
are getting printed.
I have tried to meet your function signature with another implementation using Swift's anonymous tuple and higher ordered functions. I did this as an exercise for myself and at the end thought it best to share.
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
}