for i in 0...attString.length-1
{
attString.enumerateAttribute(NSBackgroundColorAttributeName, in: NSMakeRange(i,1), options: NSAttributedString.EnumerationOptions(rawValue: 0)) { (value, range, stop) -> Void in
if let exValue = value as? UIColor
{
if self.compareColors(c1: exValue, c2: (UIColor(hex: 0x2e4270))) ||
self.compareColors(c1: exValue, c2: (UIColor(hex: 0xc0e1ff))) ||
self.compareColors(c1: exValue, c2: (UIColor(hex: 0x7a99b8))) ||
self.compareColors(c1: exValue, c2: (UIColor(hex: 0xaad5fb)))
{
aRange = NSMakeRange(i, 1)
myValue = self.styles.blueHighlightColor()
//DispatchQueue.main.async {
mutableAttString.addAttribute(attributeName, value: myValue, range: aRange)
//}
self.saveChanges = true
}
else if self.compareColors(c1: exValue, c2: (UIColor(hex: 0x385324))) ||
self.compareColors(c1: exValue, c2: (UIColor(hex: 0xbde970))) ||
self.compareColors(c1: exValue, c2: (UIColor(hex: 0x88a752)))
{
aRange = NSMakeRange(i, 1)
myValue = self.styles.greenHighlightColor()
//DispatchQueue.main.async {
mutableAttString.addAttribute(attributeName, value: myValue, range: aRange)
// }
self.saveChanges = true
}
else if self.compareColors(c1: exValue, c2: (UIColor(hex: 0x624b85))) ||
self.compareColors(c1: exValue, c2: (UIColor(hex: 0xd6affb))) ||
self.compareColors(c1: exValue, c2: (UIColor(hex: 0x997eb8))) ||
self.compareColors(c1: exValue, c2: (UIColor(hex: 0xd6affb)))
{
aRange = NSMakeRange(i, 1)
myValue = self.styles.pinkHighlightColor()
// DispatchQueue.main.async {
mutableAttString.addAttribute(attributeName, value: myValue, range: aRange)
//}
self.saveChanges = true
}
else if self.compareColors(c1: exValue, c2: (UIColor(hex: 0x7b5b15))) ||
self.compareColors(c1: exValue, c2: (UIColor(hex: 0xfbe769))) ||
self.compareColors(c1: exValue, c2: (UIColor(hex: 0xb4a64d)))
{
aRange = NSMakeRange(i, 1)
myValue = self.styles.yellowHighlightColor()
// DispatchQueue.main.async {
mutableAttString.addAttribute(attributeName, value: myValue, range: aRange)
//}
self.saveChanges = true
}
}
}
} // For loop end
Ich iteriere durch jeden NSAttributed char und ich überprüfe seine Hintergrundfarbe. Wenn es OK ist oder eine andere Farbe nicht in den if-Anweisungen definiert ist. Es funktioniert gut, aber es ist ein bisschen langsam, wenn ich größeren Text eingeben. Ich habe versucht mit DispatchQueue.concurrentPerform(iterations:execute:)
, aber es stürzt ab, ich nicht warum.Grand Center Dispatch For Schleife
danke dude! Es ist jetzt sehr schnell. Können Sie zeigen, wie man das macht? – Elita