In a typical color terminal, there are escape sequences that one can use to print text in different colors. Typically there are 8 colors available. I tried using the standard, ANSI escape sequences for this in NSLog, but no dice. It does not support by that mechanism.
在典型的彩色终端中,存在可用于以不同颜色打印文本的转义序列。通常有8种颜色可供选择。我尝试在NSLog中使用标准的ANSI转义序列,但没有骰子。它不支持该机制。
Is there a different way to print to the console (log) in color using NSLog?
是否有不同的方法使用NSLog以彩色打印到控制台(日志)?
Thanks.
谢谢。
4 个解决方案
#1
90
You can use Apple Color Emoji to add some color to your Log output like this:
您可以使用Apple Color Emoji为日志输出添加一些颜色,如下所示:
if ([self isKindOfClass:[UITableViewController class]])
NSLog(@"???? Table View controller Will appear: %@", NSStringFromClass([self class]));
else if ([self isKindOfClass:[UINavigationController class]])
NSLog(@"???? Navigation controller Will appear: %@", NSStringFromClass([self class]));
else
NSLog(@"???? View controller Will appear: %@", NSStringFromClass([self class]));
Because the above code might by displayed incorrectly on non-OS-X-platforms, I attached a screenshot of XCode, showing the code and log output:
因为上面的代码可能在非OS-X平台上显示不正确,所以我附上了XCode的截图,显示了代码和日志输出:
#2
8
A line like this works for me:
像这样的一条线对我有用:
NSLog(@"\e[1;31mRed text here\e[m normal text here");
(More colors available here)
(这里有更多颜色)
Note that you have to actually watch your output in regular console, as Xcode debugging console doesn't seem to support coloring.
请注意,您必须在常规控制台中实际观察输出,因为Xcode调试控制台似乎不支持着色。
#3
6
Ayoy's approach seems to work in general in a command line based app with printf
:
Ayoy的方法似乎在基于命令行的应用程序中使用printf:
- http://i.minus.com/iT5kYgSscw9fy.png
- http://i.minus.com/iT5kYgSscw9fy.png
However, I don't think this works with the built-in Xcode console:
但是,我不认为这适用于内置的Xcode控制台:
This:
这个:
NSLog(@"\e[1;31mRed text here\e[m normal text here");
... results in this:
...结果如下:
- http://i.minus.com/j4hu1lr03eWsP.png
- http://i.minus.com/j4hu1lr03eWsP.png
#4
6
You can colorize your NSLog
output using this Xcode plugin: https://github.com/DeepIT/XcodeColors
您可以使用此Xcode插件为您的NSLog输出着色:https://github.com/DeepIT/XcodeColors
I wrote a few lines with my quick setup instructions and simple custom macros.
我用快速设置说明和简单的自定义宏写了几行。
Update: I'm using now NSLogger with color support. Much powerful.
更新:我现在正在使用带有颜色支持的NSLogger。功能强大。
#1
90
You can use Apple Color Emoji to add some color to your Log output like this:
您可以使用Apple Color Emoji为日志输出添加一些颜色,如下所示:
if ([self isKindOfClass:[UITableViewController class]])
NSLog(@"???? Table View controller Will appear: %@", NSStringFromClass([self class]));
else if ([self isKindOfClass:[UINavigationController class]])
NSLog(@"???? Navigation controller Will appear: %@", NSStringFromClass([self class]));
else
NSLog(@"???? View controller Will appear: %@", NSStringFromClass([self class]));
Because the above code might by displayed incorrectly on non-OS-X-platforms, I attached a screenshot of XCode, showing the code and log output:
因为上面的代码可能在非OS-X平台上显示不正确,所以我附上了XCode的截图,显示了代码和日志输出:
#2
8
A line like this works for me:
像这样的一条线对我有用:
NSLog(@"\e[1;31mRed text here\e[m normal text here");
(More colors available here)
(这里有更多颜色)
Note that you have to actually watch your output in regular console, as Xcode debugging console doesn't seem to support coloring.
请注意,您必须在常规控制台中实际观察输出,因为Xcode调试控制台似乎不支持着色。
#3
6
Ayoy's approach seems to work in general in a command line based app with printf
:
Ayoy的方法似乎在基于命令行的应用程序中使用printf:
- http://i.minus.com/iT5kYgSscw9fy.png
- http://i.minus.com/iT5kYgSscw9fy.png
However, I don't think this works with the built-in Xcode console:
但是,我不认为这适用于内置的Xcode控制台:
This:
这个:
NSLog(@"\e[1;31mRed text here\e[m normal text here");
... results in this:
...结果如下:
- http://i.minus.com/j4hu1lr03eWsP.png
- http://i.minus.com/j4hu1lr03eWsP.png
#4
6
You can colorize your NSLog
output using this Xcode plugin: https://github.com/DeepIT/XcodeColors
您可以使用此Xcode插件为您的NSLog输出着色:https://github.com/DeepIT/XcodeColors
I wrote a few lines with my quick setup instructions and simple custom macros.
我用快速设置说明和简单的自定义宏写了几行。
Update: I'm using now NSLogger with color support. Much powerful.
更新:我现在正在使用带有颜色支持的NSLogger。功能强大。