• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

[复制链接]
菜鸟教程小白 发表于 2022-12-12 09:45:01 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

输入字符串看起来像:

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

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

- (NSAttributedString*)ansicodeParserNSString *)str
{
    // init string
    NSMutableString           *mutableStr    = [[NSMutableString alloc] initWithString:str];
    NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc]initWithString:str];

    // set default text attributes
    [attributedStr addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(0,[attributedStr length])];
    [attributedStr addAttribute:NSBackgroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0,[attributedStr length])];


    // init ansiColors dict
    NSDictionary *ansiColors = @{
        // Resets
        @"\\[H"     : @{ NSForegroundColorAttributeName : [UIColor whiteColor] },    // Text Reset
        @"\\[J"     : @{ NSForegroundColorAttributeName : [UIColor whiteColor] },    // Text Reset
        @"\\[0m"    : @{ NSForegroundColorAttributeName : [UIColor whiteColor] },    // Text Reset

        // Regular Text + Colors
        @"\\[0;30m" : @{ NSForegroundColorAttributeName : [UIColor blackColor] },    // Black
        @"\\[0;31m" : @{ NSForegroundColorAttributeName : [UIColor redColor] },      // Red
        @"\\[0;32m" : @{ NSForegroundColorAttributeName : [UIColor greenColor] },    // Green
        @"\\[0;33m" : @{ NSForegroundColorAttributeName : [UIColor yellowColor] },   // Yellow
        @"\\[0;34m" : @{ NSForegroundColorAttributeName : [UIColor blueColor] },     // Blue
        @"\\[0;35m" : @{ NSForegroundColorAttributeName : [UIColor purpleColor] },   // Purple
        @"\\[0;36m" : @{ NSForegroundColorAttributeName : [UIColor cyanColor] },     // Cyan
        @"\\[0;37m" : @{ NSForegroundColorAttributeName : [UIColor whiteColor] },    // White

        // Bold Text + Colors
        @"\\[1;30m" : @{ NSForegroundColorAttributeName : [UIColor blackColor] },    // Black
        @"\\[1;31m" : @{ NSForegroundColorAttributeName : [UIColor redColor] },      // Red
        @"\\[1;32m" : @{ NSForegroundColorAttributeName : [UIColor greenColor] },    // Green
        @"\\[1;33m" : @{ NSForegroundColorAttributeName : [UIColor yellowColor] },   // Yellow
        @"\\[1;34m" : @{ NSForegroundColorAttributeName : [UIColor blueColor] },     // Blue
        @"\\[1;35m" : @{ NSForegroundColorAttributeName : [UIColor purpleColor] },   // Purple
        @"\\[1;36m" : @{ NSForegroundColorAttributeName : [UIColor cyanColor] },     // Cyan
        @"\\[1;37m" : @{ NSForegroundColorAttributeName : [UIColor whiteColor] },    // White

        // Underlined Text + Colors
        @"\\[4;30m" : @{ NSForegroundColorAttributeName : [UIColor blackColor] },    // Black
        @"\\[4;31m" : @{ NSForegroundColorAttributeName : [UIColor redColor] },      // Red
        @"\\[4;32m" : @{ NSForegroundColorAttributeName : [UIColor greenColor] },    // Green
        @"\\[4;33m" : @{ NSForegroundColorAttributeName : [UIColor yellowColor] },   // Yellow
        @"\\[4;34m" : @{ NSForegroundColorAttributeName : [UIColor blueColor] },     // Blue
        @"\\[4;35m" : @{ NSForegroundColorAttributeName : [UIColor purpleColor] },   // Purple
        @"\\[4;36m" : @{ NSForegroundColorAttributeName : [UIColor cyanColor] },     // Cyan
        @"\\[4;37m" : @{ NSForegroundColorAttributeName : [UIColor whiteColor] },    // White

        // Background Colors
        @"\\[40m"   : @{ NSBackgroundColorAttributeName : [UIColor blackColor] },    // Black
        @"\\[41m"   : @{ NSBackgroundColorAttributeName : [UIColor redColor] },      // Red
        @"\\[42m"   : @{ NSBackgroundColorAttributeName : [UIColor greenColor] },    // Green
        @"\\[43m"   : @{ NSBackgroundColorAttributeName : [UIColor yellowColor] },   // Yellow
        @"\\[44m"   : @{ NSBackgroundColorAttributeName : [UIColor blueColor] },     // Blue
        @"\\[45m"   : @{ NSBackgroundColorAttributeName : [UIColor purpleColor] },   // Purple
        @"\\[46m"   : @{ NSBackgroundColorAttributeName : [UIColor cyanColor] },     // Cyan
        @"\\[47m"   : @{ NSBackgroundColorAttributeName : [UIColor whiteColor] },    // White
    };

    // set color attribute
    for(NSString *ansiStr in ansiColors)
    {
        // search ansicode in output
        NSRegularExpression *regex    = [NSRegularExpression regularExpressionWithPattern:ansiStr options:NSRegularExpressionCaseInsensitive error:nil];
        NSMutableArray      *matches  = (NSMutableArray*)[regex matchesInString:mutableStr options:0 range:NSMakeRange(0, mutableStr.length)];

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

            // set foreground color
            if(fontColor != nil)
                [attributedStr addAttribute:NSForegroundColorAttributeName value:fontColor range:NSMakeRange(wordRange.location+wordRange.length, [[[attributedStr mutableString] substringFromIndex:wordRange.location] length] - wordRange.length)];

            // set background color
            if(backColor != nil)
                [attributedStr addAttribute:NSBackgroundColorAttributeName value:backColor range:NSMakeRange(wordRange.location+wordRange.length, [[[attributedStr mutableString] substringFromIndex:wordRange.location] length] - wordRange.length)];
        }
    }

    // remove ansicodes from output
    for(NSString *ansiStr in ansiColors)
    {
        [[attributedStr mutableString] replaceOccurrencesOfString:[ansiStr substringFromIndex:1] withString"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, attributedStr.length)];
    }

    // return formatted console output
    return (NSAttributedString*)attributedStr;
}



Best Answer-推荐答案


您需要以不同的方式处理它。 您不能设置属性范围,然后以这种方式修改字符串。这会改变属性的范围。 有很多方法可以做到这一点。 在不混淆的情况下,一种更简单的方法是首先根据匹配将字符串拆分为一个数组。 然后从数组中的每个字符串中删除 ANSI 颜色前缀并应用颜色。 然后将数组连接成一个字符串。

另一种方法是首先将未归属的字符串转换为另一种格式。 例如,它可以是 HTML 或 RTF。然后您要做的就是将 ANSI 颜色标签转换为 cocoa 文本系统已经可以为您处理的格式。

关于ios - 解析 ANSI 颜色代码并为 NSAttributedString 设置相应的颜色属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23598549/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap