iOS UTF-8 Label String

2019-04-30 22:13发布

问题:

I have a UTF-8 encoding string that I want to display in a label.

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.

I have tried almost every suggestion on SO and beyond, but I just cannot get the string to display properly.

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 *  0x006797b0 @"鄧樂愚..."
    NSObject    NSObject    
    isa Class   0x3bddd8f4
    [0] Class   

I am using the iOS 6.1 SDK.

回答1:

Ok, if m_value is a const char contained UTF-8 string you have to use this method:

- (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



回答2:

When I want to show khmer on label, I use font '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 !



回答3:

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. Even though the value of this variable was being decoded correctly in the (I.e. displaying the Chinese characters) in the debug panel, the string was actually encoded with 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.

Then to fix in Objective-C, I first translated the NSString to a const char:

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

Then converted back to an NSString:

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

+1 to @Vitaly_S and @iphonic whose answers eventually led me to this solution. 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.



回答4:

Considering your variable m_value NSData, you can try the following

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 */