How to show special character in UILabel iOS - ios

I am trying to implement an app where I would like to show some text in Spanish format. For example I would like to show "España" but in my label it shows "Espa√ɬ±a" and also it changes the text for some of other text.
How to get rid of these. If anybody could help. Thanks.
Edit: When i am getting my response it logs that Below result
Message = (
"Espa\U221a\U00c9\U00ac\U00b1a:1.3\U221a\U00c7\U00ac\U00a2/min"
);
But when i extract the value according to key from Dictionary it shows
España:1.3¢/min
It means when i am getting the value from dictionary it cant do proper decoding.
how to resolve this. Any idea..?

First convert your response String to NSData using NSUTF8StringEncoding encoding, then again convert the same data to finalString like below.
NSString *string = #"España"; //Your response String goes here
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
NSString *finalString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
lblTemp.text = finalString;
UPDATE 1
I think there is some error from your response, Please see below
NSString *string = #"Nu\\u0161a Florjan\\u010di\\u010d";
NSString *finalString = [NSString
stringWithCString:[string cStringUsingEncoding:NSUTF8StringEncoding]
encoding:NSNonLossyASCIIStringEncoding];
NSLog(#"finalString = %#", finalString);
Output of above code is,
finalString = Nuša Florjančič
UPDATE 2
If you want output string like "España", your desired response should be "Espa\u00F1a", Find below,
NSString *string = #"Espa\\u00F1a";
NSString *finalString = [NSString
stringWithCString:[string cStringUsingEncoding:NSUTF8StringEncoding]
encoding:NSNonLossyASCIIStringEncoding];
NSLog(#"%#",finalString);
Output is España

Related

want to have this string "مرحبا 😀 Hello" save it to database and display back to uilabel

I am looking for solution where i want to store English + Arabic + Emoji Character to store to Database and retrieve it back while display.
Below is the code what i have used to support Emoji, after that Arabic text is not showing.
+(NSString *)emojiToSave:(NSString *)str
{
NSData *dataForEmoji = [str dataUsingEncoding:NSNonLossyASCIIStringEncoding];
NSString *encodevalue = [[NSString alloc]initWithData:dataForEmoji encoding:NSUTF8StringEncoding];
return encodevalue;
}
+(NSString *)emojiToDisplay:(NSString *)str
{
NSData *msgData = [str dataUsingEncoding:NSUTF8StringEncoding];
NSString *goodMsg = [[NSString alloc] initWithData:msgData encoding:NSNonLossyASCIIStringEncoding];
return goodMsg;
}
Can anyone pls suggest to give support for Arabic what change i should do?
Thanks in advance.
Try convert it into base64 code, then insert base64 code to database:
//Original string to base64 string
NSString *emojiString = #"مرحبا 😀 Hello";
NSData *emojiData = [emojiString dataUsingEncoding:NSUTF8StringEncoding];
NSString *base64String = [emojiData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
//Base64 string to original string
NSData *base64Data = [[NSData alloc] initWithBase64EncodedString:base64String options:NSDataBase64DecodingIgnoreUnknownCharacters];
NSString *originalString =[[NSString alloc] initWithData:base64Data encoding:NSUTF8StringEncoding];
NSLog(#"Result: %#",originalString);
Output:
You have to use an encoding that supports emoji and arabic characters. ASCII doesn't support that.
You should use NSUTF8StringEncoding everywhere, and you're fine.
Why are you using ASCII anyways? Why are you converting a string to an NSData and then back to NSString again? It doesn't make sense.

String encode in objective c

I am very new to Objective-C.
I want to get the encoded content for a NSString. In java I can do that as follows,
String str = "https://www.google.co.in/#q=ios+sqlite+crud+example";
String encodedParam = URLEncoder.encode(str, "UTF-8");
I am using http://www.tutorialspoint.com/compile_objective-c_online.php to test the codes posted in stackoverflow. There is no solution yet. I know its trivial one. Struggling to find a way though.
tried with following function, and it says following error while compile,
-(NSString *)urlEncodeUsingEncoding:(NSStringEncoding)encoding {
return (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL,
(CFStringRef)self,
NULL,
(CFStringRef)#"!*'\"();:#&=+$,/?%#[]% ",
CFStringConvertNSStringEncodingToEncoding(encoding));
}
Error,
sh-4.3$ gcc `gnustep-config --objc-flags` -L/usr/GNUstep/System/Library/Libraries -lgnustep-base -lobjc *.m -o main
main.m: In function 'main':
main.m:7:14: error: 'urlEncodeUsingEncoding' undeclared (first use in this function)
-(NSString *)urlEncodeUsingEncoding:(NSStringEncoding)encoding {
^
main.m:7:14: note: each undeclared identifier is reported only once for each function it appears in
main.m:7:36: error: expected ';' before ':' token
-(NSString *)urlEncodeUsingEncoding:(NSStringEncoding)encoding {
Edit as per the answers,
Suggested by Patrick, I used the code as follows,
NSString *storedURL = #"google.com/?search&q=this";
NSString *urlstring = [NSString stringWithFormat:#"http://%#/",storedURL];
NSURL *url = [NSURL URLWithString:urlstring];
NSError *error = nil;
NSStringEncoding encoding;
NSString *my_string = [[NSString alloc] initWithContentsOfURL:url
usedEncoding:&encoding
error:&error];
NSLog (my_string);
Nothing printed in console... Is it my NSLog is right?
Suggested by lightwolf, my code is looks like below,
NSString *str = #"https://www.google.co.in/#q=ios+sqlite+crud+example";
NSString *encodedParam = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog (encodedParam);
it prints the log, but value is same as the str..... not encoded... I want this str as
https%3A%2F%2Fwww.google.co.in%2F%23q%3Dios%2Bsqlite%2Bcrud%2Bexample
If you want to encode a specific range of characters you chould use
NSString *str = #"https://www.google.co.in/#q=ios+sqlite+crud+example";
NSString *encodedParam = [str stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet alphanumericCharacterSet]];
NSLog (#"%#", encodedParam);
Note the invertedSet; In that way, you are encoding all characters except the set specified (all alphanumeric ones)
The result is
https%3A%2F%2Fwww%2Egoogle%2Eco%2Ein%2F%23q%3Dios%2Bsqlite%2Bcrud%2Bexample
If you want to use a specific set of characters you should use
NSString *str = #"https://www.google.co.in/#q=ios+sqlite+crud+example";
NSCharacterSet* set = [NSCharacterSet characterSetWithCharactersInString:#"!*'();#&=+$,?%#[]"];
NSString *encodedParam = [str stringByAddingPercentEncodingWithAllowedCharacters:[set invertedSet]];
NSLog (#"%#", encodedParam);
In this case I intentionally missed / and : so the result is
https://www.google.co.in/%23q%3Dios%2Bsqlite%2Bcrud%2Bexample
Maybe this is what you want
NSString *str = #"<html><head><title>First</title></head><body><p>Parsed HTML into a doc.</p></body></html>";
NSString *encodedParam = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
You have to encode only the params, not the entire URL of course

How to covert std::string to NSString?Look at the last picture

How to covert std::string to NSString? ,why the result is garbled?I use lldb command po ,look at the console ,the red arrow,the _data display correct string?Why?
std::string resultString = getResult();
NSString *str= [NSString stringWithCString:resultString.c_str() encoding:NSUTF8StringEncoding];
but the str is garbled,like
May be here issue is with string encoding. I have done little test on this input string and found some result on that.
Here I have use complex string "\x18\xa4\tp\x01" which you shown in your log. From the result I conclude that NSUTF8StringEncoding encoding string is not working with above string.
Here is code:
+ (void) stringTest {
std::string *resultString = new std::string("\x18\xa4\tp\x01");
NSString *str= [NSString stringWithCString:resultString->c_str() encoding:NSUTF8StringEncoding];
NSString *str2= [NSString stringWithCString:resultString->c_str() encoding:NSASCIIStringEncoding];
NSString *str3= [NSString stringWithCString:resultString->c_str() encoding:[NSString defaultCStringEncoding]];
NSLog(#"str :%#",str);
NSLog(#"str2 :%#",str2);
NSLog(#"str3 :%#",str3);
}
And a reference image for log:
Here you can see that NSUTF8StringEncoding returns nil string and other encoding gives a result. I'm not sure which encoding scheme is valid for your string. If we know that encoding scheme for resultString string then we can get more accurate result here.

convert unicode characters while data loading in uiwebview

{ disclaimertxt = "<b>sample tex\U221a\U00a9t n\U221a\U00a9ewerv\U221a\U00a9e iew adults.</b>
\n<br/>
\n<br/>
\nthis is sample \U221a\U2020 an d\U221a\U00a9convertion of the language\U221a\U00a9reduction\U201a\U00c4\U00b6
\n ";
}
the above one is dictionary which contains above value with key disclaimertext
but in output the unicode characters is replaced with ?(C)" and "?A
and my code is :
NSData *messagedata = [dictionary[disclaimertxt] dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *string = [[NSString alloc]initWithData:messagedata encoding:NSUTF8StringEncoding];
so i want to display those unicode characters with respected text/value while loading in uiwebview.. plz give ur suggestions. in ios7 its working fine with desired output what i need. but when i run ios6 aim getting above outout.so plz find solution.
Follow these steps:
manually load the HTML from the page that doesn't include the meta tag, into a string.
Using string manipulation, insert your appropriate encoding meta tag into the manually loaded HTML
set the HTML in your web view to the modified string using loadHTMLString:
Use this to convert Unicode charaters to NSString:
char cString[] = "This isn\u2019t Test String";
NSData *data = [NSData dataWithBytes:cString length:strlen(cString)];
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
Now use this string in your Webview.

Encoding for converting between NSString to NSData and back

I'm trying to encrypt/decrypt an NSString and return the original string in the end. Here's how I convert the string to a data object:
NSData *string_data = [string dataUsingEncoding:NSUTF8StringEncoding];
And after that data has been encrypted/decrypted I want it back to the original string by doing:
NSString *to_string = [NSString stringWithCString:[decrypted_data bytes] encoding:NSUTF8StringEncoding];
The encoding seems to match, but I still get a null when I try to print out to_string to the console. I've tried all sorts of encoding settings. It doesn't seem to work.
Use:
NSString *to_string = [[NSString alloc] initWithData:string_data encoding:NSUTF8StringEncoding];
It is not safe to use stringWithCString because the bytes buffer you get from NSData is not guaranteed to be null-terminated.

Resources