Change the encoding of the textfield - ios

I am trying to type in arabic into a textfield in Titanium(creating an iOS app).
I want to change the encoding settings of the textfield to UTF-8. Need help with that.

Something like this? You can convert easily one way or the other.
NSString *foo = someTextField.text;
NSData *data = [foo dataUsingEncoding:NSUTF8StringEncoding];
NSString *fooString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
For specific appcelerator encoding types you need to refer to their documentation.
Appcelerator Encoding Spec

Related

Converting NSData to NSString without knowing the encoding type

Disclaimer:
this previous question does not contain a valid answer.
this other instead does contain the technique I used to convert it but does not answer the question I have.
Question:
I am reading some NSData from a BLE characteristic which corresponds to a string, however I have no idea what encoding format it uses.
This is the view from the debugger on Xcode:
How can I convert it to a string? I'd like to extract this value:
"<951c7c1d e0fbdcc3 7b8b0b97 5c522036>"
I tried the following with with no meanigful conversion. However XCode seems to do the job. Why?
NSData * valueAsData = characteristic.value.mutableCopy;
NSString* newStr = [[NSString alloc] initWithData:characteristic.value encoding:NSUTF8StringEncoding];
NSString* newStr2 = [[NSString alloc] initWithData:characteristic.value encoding:NSASCIIStringEncoding];
Get the descriptors associated with the characteristic. One of them should be CBUUIDCharacteristicFormatString. This will indicate the presentation format, which will indicate the data type of the characteristic value.

Get unicode chars from webservice and display them in ios app

I need some help:
i get from WebService only a part from the uunicode value, and after this I append the prefix \u to finish the value. The .ttf is good, i tested with some hardcoded values.
NSString *cuvant = [[self.catData objectAtIndex:indexPath.row]objectAtIndex:9]; //Get data
//apend prefix (double \ to escape the \u command)
cuvant = [NSString stringWithFormat:#"\\u%#",cuvant];
// cell.catChar.text = [NSString stringWithUTF8String:"\ue674"]; --->this works very well
cell.catChar.text = [NSString stringWithUTF8String:[cuvant UTF8String]]; //---> this doesn't work
i searched the documentation, and other sites but i didn't found nothing usefull, all the hints are with hardcoded data... i need tot take the codes dinamically
Thanks!
All you need is just to feed this unicoded string as data first. So make a C-String then
NSData *dataFromUnicodedString = [NSData dataWithBytes:yourCUnicodedString length:strlen(yourCUnicodedString)];
and afterwards the resulting string will be
NSString *unicodedString = [[NSString alloc] initWithData:dataFromUnicodedString encoding:NSUTF8StringEncoding];

How to show my Keyboard extension's Emojis in other device

I am looking for the solution for iOS Keyboard extensions.
My problem is if I will type My Emojis using my device, so how the another device (who does not have installed my app) will be able to show that in app like Whatsapp.?
You can use Emoji Unicode
link
Example
NSString *str = #"Hi \U0001F431";
NSData *data = [str dataUsingEncoding:NSNonLossyASCIIStringEncoding];
NSString *valueUnicode = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSData *newData = [valueUnicode dataUsingEncoding:NSUTF8StringEncoding];
NSString *valueEmoj = [[NSString alloc] initWithData:newData encoding:NSNonLossyASCIIStringEncoding];
lbl.text = valueEmoj;
After doing many research I concluded that what I was expecting is currently not possible.
But this is possible that,
If I will create my own emoji keyboard, then clicking on that emoji in Keyboard extension will copy that image.
After paste that image to any textfield of apps like Whatsapp, I will be only able to share that emoji as Image.(i.e. Image sharing).
Making your own emojis like native emojis is currently not possible to be available in all public apps.
We can do this for only our apps, like Skype and Hike does.

unable to show emoji's properly in UIlabel

I am trying to save the emojis to server and on later time receiving them.
I used the Following Code before
NSData *data = [strEmo dataUsingEncoding:NSNonLossyASCIIStringEncoding];
NSString *goodValue = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSData *data1 = [strEmo dataUsingEncoding:NSUTF8StringEncoding];
NSString *goodValue = [[NSString alloc] initWithData:data encoding:NSNonLossyASCIIStringEncoding];
It generates the Hashcode for the Emoji. But decoding doesn't work. So I skipped the Idea of using this Code.
Then I used a third Party NSString+HTML.h Class. Which Sends and recieve emoji easily. But
Now the Problem is when there are so many emoji's the UIlabel on which I am showing the data, the emojis are distorted and If I saved 20 emojis it shows 12-13 only.
I have added the Pic for reference
where Yellow part is UIlabel with back colored Yellow
Buddy why are you changing the string in Data two times in a row just simply use this
NSString *uniText = [NSString stringWithUTF8String:[strEmo UTF8String]];
NSData *msgData = [uniText dataUsingEncoding:NSNonLossyASCIIStringEncoding];
NSString *readyString = [[NSString alloc] initWithData:msgData encoding:NSUTF8StringEncoding];
First convert your string into constant C characters then convert it into string using UTF8 encoding, now convert it into NSData as (7-bit verbose ASCII to represent all Unicode characters) using NSNonLossyASCIIStringEncoding encoding and then again string ready to send with unicode characters. Hope this should work as it is working fine in my code.

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.

Resources