显示Entypo字体的5位基本unicode字符

时间:2014-06-13 17:07:11

标签: ios objective-c unicode

我在我的iPhone应用程序中使用了Entypo字体,但它仅适用于某些角色。我无法使用五位数的unicode值显示图标。

我在网上发现了一些信息,说明这是因为iOS(以及其他语言)支持的UTF编码,5位unicode值应分为两个值。

但我找不到明确的操作说明或代码示例。

我显示Entypo符号的代码是这样的:

myLabel.text = [NSString stringWithUTF8String:"\u25B6"];
myLabel.font = [UIFont fontWithName:@"Entypo" size:200];

如果我用“\ u1F342”替换unicode值,它是Entypo字体中的图标叶,则显示无效字符。

如果您已经遇到过此问题,也许您可​​以帮我节省时间。

非常感谢

2 个答案:

答案 0 :(得分:1)

如果您查看unicode page for that character,则会看到其UTF-8编码为0xF0 0x9F 0x8D 0x82 - 您应该使用的是:

myLabel.text = [NSString stringWithUTF8String:"\uf0\u9f\u8d\u82"];

注意:完全未经测试。

答案 1 :(得分:0)

经过多次搜索后,我终于找到了一种易于在不同情况下使用的解决方案:符号编码最多4位数字和4位数以上。

我定义了一个NSString类别如下:

#import "NSString+Extension.h"

@implementation NSString (Extension)

/**
 * Convert a UTF8 symbol to a string which can directly be used as text in a label view for instance, for which the right font has been specified.
 *
 * The method can be used for both cases
 *  . the symbol is defined as a const char with a maximum of 4 digits. In this case the first parameter must be 0 and the second is used. Example: NSString *symbolString = [NSString symbolStringfromUnicode:0 orChar:"\uE766"]
 *  . the symbol is defined as an integer with hexadecimal notation. It can be have either less or more than 4 digits. In this case, only the first parameter is used. Example : NSString *prefixSymbol = [NSString symbolStringfromUnicode:0x1F464 orChar:nil];
 *
 * @param symbolUnicode     symbol to convert defined as int
 * @param symbolChar        symbol to convert defined as const char *
 *
 */

+ (NSString *)symbolStringfromUnicode:(int)symbolUnicode orChar:(const char *)symbolChar
{
    NSString *symbolString;
    if (symbolUnicode == 0) {
        symbolString = [NSString stringWithUTF8String:symbolChar];
    }
    else {
        int unicode = symbolUnicode;
        symbolString = [[NSString alloc] initWithBytes:&unicode length:sizeof(unicode) encoding:NSUTF32LittleEndianStringEncoding];
    }

    return symbolString;
}

@end