格兰中心循环派遣 [英] Grand Center Dispatch For Loop

查看:79
本文介绍了格兰中心循环派遣的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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:),但是它崩溃了,我不明白.

I iterate through every NSAttributed char and I'm checking its background color. If it is clear ok, or any other color not defined in the if statements. It works fine, but it's kinda slow when I enter larger text. I tried using DispatchQueue.concurrentPerform(iterations:execute:), but it crashes, I don't why.

推荐答案

没有外部for循环的原因.只需枚举属性字符串以查找NSBackgroundColorAttributeName.只需用新颜色替换给定的范围即可.

There's no reason for the outer for loop. Just enumerate the attributed string looking for NSBackgroundColorAttributeName. Simply replace whatever range you are given with the new color.

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作为键,则将其值用作替换色.

Once that is done you can improve this even further. Create a dictionary of color mappings. If exValue is found as a key, use its value as the replacement color.

这篇关于格兰中心循环派遣的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆