公司項目使用的icon是阿里圖標(biāo)庫的圖標(biāo)字體。
將圖標(biāo)下載到了項目中,在使用中發(fā)現(xiàn)"\u"后面必須跟4位16進(jìn)制(否則編譯報錯),不能進(jìn)行拼接處理会喝。
所以需要將"\\ue670"(內(nèi)容為一個整體Unicode的UTF-8字符串)轉(zhuǎn)化為一個UTF-8的一個字符。
哈哈玩郊,也不知道說明白沒有。
總之下面方法實(shí)現(xiàn)了該功能枉阵。
//Unicode轉(zhuǎn)UTF-8
+ (NSString*) replaceUnicode:(NSString*)aUnicodeString
{
NSString *tempStr1 = [aUnicodeString stringByReplacingOccurrencesOfString:@"\\u"withString:@"\\U"];
NSString *tempStr2 = [tempStr1 stringByReplacingOccurrencesOfString:@"\""withString:@"\\\""];
NSString *tempStr3 = [[@"\"" stringByAppendingString:tempStr2] stringByAppendingString:@"\""];
//tempStr3現(xiàn)在的值為 @"\"\\ue670\""
NSData *tempData = [tempStr3 dataUsingEncoding:NSUTF8StringEncoding];
NSString* returnStr = [NSPropertyListSerialization propertyListFromData:tempData
mutabilityOption:NSPropertyListImmutable
format:NULL
errorDescription:NULL];
//通過NSPropertyListSerialization將NSData數(shù)據(jù)對象歸檔提取成String译红,這時候的returnStr就是 @"\ue670"了
return [returnStr stringByReplacingOccurrencesOfString:@"\\r\\n"withString:@"\n"];
}
其中使用到了NSPropertyListSerialization
官方解釋為
The NSPropertyListSerialization class provides methods that convert property list objects to and from several serialized formats. Property list objects include NSData, NSString, NSArray, NSDictionary, NSDate, and NSNumber objects. These objects are toll-free bridged with their respective Core Foundation types (CFData, CFString, and so on). For more about toll-free bridging, see “Interchangeable Data Types”.
翻譯為
NSPropertyListSerialization這個類提供了一些方法,專門用來轉(zhuǎn)換不同組織形式的list對象.list對象包括了NSData, NSString, NSArray, NSDictionary, NSDate, 以及 NSNumber.這些對象與衍生出他們的祖先對象有著一些聯(lián)系,對于toll-free bridging,請看“Interchangeable Data Types。