-2

I need to write some logs in a .txt file and i want, for more clarity, colorize some text.

How can i do this trick?

Here my function

+(void)writeInLogFile:(NSString *)strLog inFolder:(NSString *)folder fileName:(NSString *)fileName extension:(NSString *)extension
{
[[NSUserDefaults standardUserDefaults] synchronize];
BOOL LogFileOnIpad = [[NSUserDefaults standardUserDefaults] boolForKey:PREF_LOG_FILE_ON_IPAD];

if(LogFileOnIpad)
{
    //Create Directory
    NSString *path;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    path = [[paths objectAtIndex:0] stringByAppendingPathComponent:folder];
    NSError *error;
    if (![[NSFileManager defaultManager] fileExistsAtPath:path])    //Does directory already exists?
    {
        if (![[NSFileManager defaultManager] createDirectoryAtPath:path
                                       withIntermediateDirectories:YES
                                                        attributes:nil
                                                             error:&error])
        {
            NSLog(@"Create directory error: %@", error);
        }
    }

    NSString* filePath = [NSString stringWithFormat:@"%@/%@.%@",path,fileName,extension];

    if(![[NSFileManager defaultManager] fileExistsAtPath:filePath])
        [[NSFileManager defaultManager] createFileAtPath:filePath contents:nil attributes:nil];

    NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:filePath];
    [fileHandle seekToEndOfFile];
    [fileHandle writeData:[strLog dataUsingEncoding:NSUTF8StringEncoding]];
    [fileHandle closeFile];
}
}

This is how i use my function :

NSString* str = @"SOME STRING TO WRITE IN LOG FILE";
[HandleString writeInLogFile:str inFolder:LOGS_FOLDER fileName:[NSString stringWithFormat:@"%@_%@",LOGS_IPAD,[HandleString convertDayDateForLog]] extension:@"txt"];

I tried to declare a label with a UIColor and pass its text to my function but without success.

johnnyBeGood
  • 69
  • 1
  • 8

2 Answers2

1

The file you are creating is a plain text file, plain as in no formatting (bold, italic, coloured text etc). You would need to write a rich text file .rtf to be able to colour some text. Although I have no idea how you would go about it.

Christian Fox
  • 390
  • 2
  • 15
1

Try to write file with .html extension instead example below :

<p style="color:red" /> Error Message </p>

<p style="color:green" /> Success Message </p>

and open this file in any browser so that you can clearly see your log messages.

techierishi
  • 413
  • 3
  • 14