Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS UTF-8 Label String

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.

Xcode Screenshot

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.

like image 860
PassKit Avatar asked Apr 04 '13 10:04

PassKit


2 Answers

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

like image 168
Vitaly S. Avatar answered Sep 20 '22 21:09

Vitaly S.


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;`

set font

result

I don't know this can help you or not , but this is what I did before !

like image 39
malinchhan Avatar answered Sep 19 '22 21:09

malinchhan