简体   繁体   English

iOS UTF-8标签字符串

[英]iOS UTF-8 Label String

I have a UTF-8 encoding string that I want to display in a label. 我有一个UTF-8编码字符串,我想在标签中显示。

When I set a break-point and examine the variable holding the string, all looks good. 当我设置一个断点并检查持有字符串的变量时,所有看起来都很好。 However, when I try to output to the log, or to the label, I get latin encoding. 但是,当我尝试输出到日志或标签时,我得到拉丁编码。

Xcode截图

I have tried almost every suggestion on SO and beyond, but I just cannot get the string to display properly. 我已经尝试了几乎所有关于SO及其他的建议,但我无法正确显示字符串。

Here is my code: 这是我的代码:

NSString *rawString = [NSString stringWithFormat:@"%@",m_value];

const char *utf8String = [rawString UTF8String];
NSLog (@"%@", [NSString stringWithUTF8String:utf8String]);
NSLog (@"%s", utf8String);
NSLog (@"%@", rawString);

self.resultText.text = [NSString stringWithUTF8String:utf8String];

m_value is an NSString, and in the debug window, it also displays the correct encoding. m_value是一个NSString,在调试窗口中,它还显示正确的编码。

m_value NSString *  0x006797b0 @"鄧樂愚..."
    NSObject    NSObject    
    isa Class   0x3bddd8f4
    [0] Class   

I am using the iOS 6.1 SDK. 我正在使用iOS 6.1 SDK。

Ok, if m_value is a const char contained UTF-8 string you have to use this method: 好吧,如果m_value是一个包含UTF-8字符串的const char,你必须使用这个方法:

- (id)initWithUTF8String:(const char *)bytes

NSString *correctString = [[NSString alloc] initWithUTF8String: m_value];

It's incorrect to pass const char* to @ formatter, because @ means NSObject , so it will be always incorrect and can lead to app crash const char*传递给@ formatter是不正确的,因为@表示NSObject ,因此它总是不正确并且可能导致应用程序崩溃

When I want to show khmer on label, I use font 'Hanuman.ttf'. 当我想在标签上显示高棉时,我使用字体'Hanuman.ttf'。 This is code I use: 这是我使用的代码:

`UIFont *font = [UIFont fontWithName:@"Hanuman" size:20.0f];

self.nameLabel.text = [NSString stringWithFormat:@"%@",itemName];
self.nameLabel.font = font;`

设置字体

结果

I don't know this can help you or not , but this is what I did before ! 我不知道这可以帮到你,但这就是我之前所做的!

So I finally managed to get to the bottom of this. 所以我终于成功了。

The m_value NSString was being set by a third party library to which I had no access to the source. m_value NSString由第三方库设置,我无权访问该源。 Even though the value of this variable was being decoded correctly in the (Ie displaying the Chinese characters) in the debug panel, the string was actually encoded with NSMacOSRomanStringEncoding . 即使在调试面板中(即显示中文字符)正确解码了此变量的值,该字符串实际上也是使用NSMacOSRomanStringEncoding编码的。

I was able to determine this by copying the output into TextWrangler , and flipping encodings until I found the one that translated correctly into UTF-8. 我能够通过将输出复制到TextWrangler并翻转编码直到找到正确转换为UTF-8的编码来确定这一点。

Then to fix in Objective-C, I first translated the NSString to a const char : 然后在Objective-C中修复,我首先将NSString转换为const char

const char *macString = [bxr.m_value cStringUsingEncoding:NSMacOSRomanStringEncoding];

Then converted back to an NSString : 然后转换回NSString

NSString *utf8String = [[NSString alloc]initWithCString:macString encoding:NSUTF8StringEncoding];

+1 to @Vitaly_S and @iphonic whose answers eventually led me to this solution. +1到@Vitaly_S和@iphonic,他们的答案最终促成了这个解决方案。 For anyone else that stumbles across this; 对于其他任何偶然发现的人; it seems that as of Xcode 4.6.1, the debug window cannot be trusted to render strings correctly, but you can rely on the NSLog output. 似乎从Xcode 4.6.1开始,调试窗口不能被信任以正确呈现字符串,但您可以依赖NSLog输出。

Considering your variable m_value NSData, you can try the following 考虑您的变量m_value NSData,您可以尝试以下方法

self.resultText.text = [[NSString alloc] initWithData:m_value encoding:NSISOLatin1StringEncoding]; 

There are many encoding available you can try them too 您可以尝试使用许多编码

NSASCIIStringEncoding       /* 0..127 only */
NSNEXTSTEPStringEncoding
NSJapaneseEUCStringEncoding
NSUTF8StringEncoding
NSISOLatin1StringEncoding
NSSymbolStringEncoding
NSNonLossyASCIIStringEncoding
NSShiftJISStringEncoding          /* kCFStringEncodingDOSJapanese */
NSISOLatin2StringEncoding
NSUnicodeStringEncoding
NSWindowsCP1251StringEncoding    /* Cyrillic; same as AdobeStandardCyrillic */
NSWindowsCP1252StringEncoding    /* WinLatin1 */
NSWindowsCP1253StringEncoding    /* Greek */
NSWindowsCP1254StringEncoding    /* Turkish */
NSWindowsCP1250StringEncoding   /* WinLatin2 */
NSISO2022JPStringEncoding        /* ISO 2022 Japanese encoding for e-mail */
NSMacOSRomanStringEncoding

NSUTF16StringEncoding      /* An alias for NSUnicodeStringEncoding */

NSUTF16BigEndianStringEncoding          /* NSUTF16StringEncoding encoding with explicit endianness specified */
NSUTF16LittleEndianStringEncoding      /* NSUTF16StringEncoding encoding with explicit endianness specified */

NSUTF32StringEncoding                  
NSUTF32BigEndianStringEncoding          /* NSUTF32StringEncoding encoding with explicit endianness specified */
NSUTF32LittleEndianStringEncoding        /* NSUTF32StringEncoding encoding with explicit endianness specified */

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM