Grand Center Dispatch For Loop

Grand Center Dispatch For Loop

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:),但它崩溃了,我不知道为什么。

没有理由使用外部 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 作为键,则使用其值作为替换颜色。