2017-05-13 6 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 

모든 NSAttributed 문자를 반복하고 배경색을 확인하고 있습니다. 그것이 분명하다면, 또는 if 문에 정의되지 않은 다른 색상. 그것은 잘 작동하지만 큰 텍스트를 입력하면 다소 느립니다. 나는 DispatchQueue.concurrentPerform(iterations:execute:)을 사용해 보았습니다.하지만 충돌합니다. 이유가 없습니다.루프를위한 그랜드 센터 파견

답변

1

바깥 쪽은 for 루프가 아닙니다. NSBackgroundColorAttributeName을 찾는 속성 문자열을 열거하십시오. 주어진 범위를 새 색상으로 바꾸기 만하면됩니다.

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 
     } 
    } 
} 

이렇게하면 더 향상시킬 수 있습니다. 색상 매핑 사전을 만듭니다. exValue이 키로 발견되면 해당 값을 대체 색상으로 사용하십시오.

+0

고마워! 지금은 매우 빠릅니다. 어떻게 할 수 있는지 보여줄 수 있습니까? – Elita