菜鸟教程小白 发表于 2022-12-12 09:45:01

ios - 解析 ANSI 颜色代码并为 NSAttributedString 设置相应的颜色属性


                                            <p><p>我需要一种方法来解析来自 shell 输出的 ANSI 颜色代码,以便为 NSAttributedString 设置相应的颜色属性。 </p>

<p>输入字符串看起来像:</p>

<p>[0;31m 这应该是红色的。[0;34m 这应该是蓝色的。 [0;32m这应该是绿色的。[0;31m这也应该是红色的。</p>

<p>从输出中删除 Ansi 代码后,我的代码与颜色不匹配。我当前的代码如下:</p>

<pre><code>- (NSAttributedString*)ansicodeParser:(NSString *)str
{
    // init string
    NSMutableString         *mutableStr    = [ initWithString:str];
    NSMutableAttributedString *attributedStr = [initWithString:str];

    // set default text attributes
    range:NSMakeRange(0,)];
    range:NSMakeRange(0,)];


    // init ansiColors dict
    NSDictionary *ansiColors = @{
      // Resets
      @&#34;\\ },    // Text Reset
      @&#34;\\ },    // Text Reset
      @&#34;\\ },    // Text Reset

      // Regular Text + Colors
      @&#34;\\ },    // Black
      @&#34;\\ },      // Red
      @&#34;\\ },    // Green
      @&#34;\\ },   // Yellow
      @&#34;\\ },   // Blue
      @&#34;\\ },   // Purple
      @&#34;\\ },   // Cyan
      @&#34;\\ },    // White

      // Bold Text + Colors
      @&#34;\\ },    // Black
      @&#34;\\ },      // Red
      @&#34;\\ },    // Green
      @&#34;\\ },   // Yellow
      @&#34;\\ },   // Blue
      @&#34;\\ },   // Purple
      @&#34;\\ },   // Cyan
      @&#34;\\ },    // White

      // Underlined Text + Colors
      @&#34;\\ },    // Black
      @&#34;\\ },      // Red
      @&#34;\\ },    // Green
      @&#34;\\ },   // Yellow
      @&#34;\\ },   // Blue
      @&#34;\\ },   // Purple
      @&#34;\\ },   // Cyan
      @&#34;\\ },    // White

      // Background Colors
      @&#34;\\ },    // Black
      @&#34;\\ },      // Red
      @&#34;\\ },    // Green
      @&#34;\\ },   // Yellow
      @&#34;\\ },   // Blue
      @&#34;\\ },   // Purple
      @&#34;\\ },   // Cyan
      @&#34;\\ },    // White
    };

    // set color attribute
    for(NSString *ansiStr in ansiColors)
    {
      // search ansicode in output
      NSRegularExpression *regex    = ;
      NSMutableArray      *matches= (NSMutableArray*);

      for (NSTextCheckingResult *match in matches)
      {
            // gather values
            NSRange wordRange   = ;
            UIColor*fontColor = [ objectForKey:NSForegroundColorAttributeName];
            UIColor*backColor = [ objectForKey:NSBackgroundColorAttributeName];

            // set foreground color
            if(fontColor != nil)
                substringFromIndex:wordRange.location] length] - wordRange.length)];

            // set background color
            if(backColor != nil)
                substringFromIndex:wordRange.location] length] - wordRange.length)];
      }
    }

    // remove ansicodes from output
    for(NSString *ansiStr in ansiColors)
    {
      [ replaceOccurrencesOfString: withString:@&#34;&#34; options:NSCaseInsensitiveSearch range:NSMakeRange(0, attributedStr.length)];
    }

    // return formatted console output
    return (NSAttributedString*)attributedStr;
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您需要以不同的方式处理它。
您不能设置属性范围,然后以这种方式修改字符串。这会改变属性的范围。
有很多方法可以做到这一点。
在不混淆的情况下,一种更简单的方法是首先根据匹配将字符串拆分为一个数组。
然后从数组中的每个字符串中删除 ANSI 颜色前缀并应用颜色。
然后将数组连接成一个字符串。 </p>

<p>另一种方法是首先将未归属的字符串转换为另一种格式。
例如,它可以是 HTML 或 RTF。然后您要做的就是将 ANSI 颜色标签转换为 cocoa 文本系统已经可以为您处理的格式。 </p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 解析 ANSI 颜色代码并为 NSAttributedString 设置相应的颜色属性,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/23598549/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/23598549/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 解析 ANSI 颜色代码并为 NSAttributedString 设置相应的颜色属性