Reputation:
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.
Is there a different way to print to the console (log) in color using NSLog?
Thanks.
Upvotes: 48
Views: 23863
Reputation: 7493
You can use Apple Color Emoji to add some color to your Log output like this:
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:
Upvotes: 101
Reputation: 19641
You can colorize your NSLog
output using this Xcode plugin: 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.
Upvotes: 7
Reputation: 22939
Ayoy's approach seems to work in general in a command line based app with printf
:
However, I don't think this works with the built-in Xcode console:
This:
NSLog(@"\e[1;31mRed text here\e[m normal text here");
... results in this:
Upvotes: 6