我需要在 .txt 文件中编写一些日志,并且为了更清楚起见,我希望对一些文本进行着色。
我该怎么做这个把戏?
这是我的功能
+(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];
}
}
这就是我使用我的功能的方式:
NSString* str = @"SOME STRING TO WRITE IN LOG FILE";
[HandleString writeInLogFile:str inFolder:LOGS_FOLDER fileName:[NSString stringWithFormat:@"%@_%@",LOGS_IPAD,[HandleString convertDayDateForLog]] extension:@"txt"];
我试图用 UIColor 声明一个标签并将其文本传递给我的函数,但没有成功。