2017-05-13 2 views
0
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

Antwort

1

Es gibt keinen Grund für die äußere for Schleife. Zählen Sie einfach die attributierte Zeichenfolge auf, die nach NSBackgroundColorAttributeName sucht. Ersetzen Sie einfach den Bereich, den Sie erhalten, mit der neuen Farbe.

attString.enumerateAttribute(NSBackgroundColorAttributeName, in: NSMakeRange(0, attString.length), 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))) 
     { 
      myValue = self.styles.blueHighlightColor() 
      //DispatchQueue.main.async { 
      mutableAttString.addAttribute(attributeName, value: myValue, range: range) 
      //} 
      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))) 
     { 
      myValue = self.styles.greenHighlightColor() 
      //DispatchQueue.main.async { 
      mutableAttString.addAttribute(attributeName, value: myValue, range: range) 
      // } 
      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))) 
     { 
      myValue = self.styles.pinkHighlightColor() 
      // DispatchQueue.main.async { 
      mutableAttString.addAttribute(attributeName, value: myValue, range: range) 
      //} 
      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))) 
     { 
      myValue = self.styles.yellowHighlightColor() 
      // DispatchQueue.main.async { 
      mutableAttString.addAttribute(attributeName, value: myValue, range: range) 
      //} 
      self.saveChanges = true 
     } 
    } 
} 

Sobald dies erledigt ist, können Sie dies noch weiter verbessern. Erstellen Sie ein Wörterbuch mit Farbzuordnungen. Wenn exValue als Schlüssel gefunden wird, verwenden Sie dessen Wert als Ersatzfarbe.

+0

danke dude! Es ist jetzt sehr schnell. Können Sie zeigen, wie man das macht? – Elita

Verwandte Themen