Not showing smily ( Emoji ) in in UITextView in iOS? - objective-c

I have stored all uni-codes(emoji characters) in plist supported by iphone. When i write directly as
- (IBAction)sendButtonSelected:(id)sender {
NSMutableArray *emoticonsArray = [[NSMutableArray alloc]initWithObjects:#"\ue415",nil];
NSString *imageNameToPass = [NSString stringWithFormat:#"%#",[emoticonsArray objectAtIndex:0]];
NSLog(#"imageNameToPass1...%#",imageNameToPass);
messageTextView.text =imageNameToPass;
}
it show emoji in textview but as soon as i fetch from plist
NSString *plistPath1 = [[NSBundle mainBundle] pathForResource:#"unicodes" ofType:#"plist"];
NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:plistPath1];
activeArray= [dictionary objectForKey:categoryString];
NSLog(#"activeArray...%#",activeArray);
emoticonsArrayForHomeEmoji = [[NSMutableArray alloc]initWithCapacity:[activeArray count]];
for(int i=0; i<[activeArray count]; i++)
{
id objects = (id)[activeArray objectAtIndex:i];
[emoticonsArrayForHomeEmoji insertObject:objects atIndex:i];
}
NSString *imageNameToPass = [NSString stringWithFormat:#"%#",[emoticonsArrayForHomeEmoji
objectAtIndex:0]];
NSLog(#"imageNameToPass1...%#",imageNameToPass);
messageTextView.text =imageNameToPass;
then it shows unicode as text \ue415 in text view instead of emoji.
What i am doing wrong?. Please help me out!

Wel said by #AliSoftware, the Plist data will be read as-it is, so you can add the emojis to your plist by following this steps:
1) Go to your top bar, and click on Edit.
2) Now select Special Characters
3) Now drag and drop emoji to plist.
For more details I am adding snap shots. take a look at it.

The \uxxxx notation is only interpreted by the compiler (as the source code is usually in ASCII or MacRoman or whatever but not often UTF8)
Plist files uses the characters directly, and are encoded in UTF8.
So you should insert the emoji character itself into the plist directly, instead of using the \uxxxx notation, as the Plist data will be read as-is.
Lion and Mountain Lion Keyboard palettes contains emoji characters directly, so that should not be difficult to insert the characters when editing the PLIST anyway.

Related

How to save text from textview?

I have a textview where you can write multiple lines of text. How do you save that text in a file or variables?
I used the multiline text field but it doesn't let me go to next line unless I hit control enter.
What I'm thinking is like a text editor, after you type everything you save that in a file. Or I can get each line from the text view into a variable.
What's the best way to do this?
NSString can save text up to 4.2 billion characters. \n denotes a line break, so no need to save into multiple parameters.
NSString *text = textView.text;
OSX
NString *text = [[textView textStorage] string];
If you're looking for each individual line for whatever reason, you could use componentsSeparatedByString
NSArray *linesArray = [textView.text componentsSeparatedByString:#"\n"];
Each line will be available at linesArray[0], linesArray[1] etc...
[linesArray count] will give you the total number of lines... with linesArray[[linesArray count]-1] being the last line in the string.
The textView.text property is an NSString also... so when you say saved.. do you mean intra app session? If so you can use NSUserDefaults
Save object
[[NSUserDefaults standardUserDefaults]setObject:textView.text forKey:#"TheKeyForMyText"];
Get object
NSString *text = [[NSUserDefaults standardUserDefaults]objectForKey:#"TheKeyForMyText"];
Assign
In swift
let var_name = textfield.text
Or in objective C
NSString *string_name = textfield.text;
And use the variable where you want to.

How can I handle this in cocoa? Retrieve an entry from a plist file?

I have this small excel file given by a brazilian government authority, it contains records for each city in Brazil, a ZIP Code range for each city and its "city code" .
I need to retrieve for a given city its "city code".
I imagine the best way would be to parse a given zip code for the city and return its "city code", based on the first two columns that display the zip code range.
I am confident that using AppleScript I can compile a plist file for the given Excel file. But can someone point me for a few lines of objectiveC code to retrieve the given entry from a plist file once I parse the ZIP code?
Please see excel file at http://www.idanfe.com/dl/codes.xls.zip
Thanks.
I have uploaded a sample plist file to http://www.idanfe.com/dl/cityCodes.plist
Further explanation:
I will parse a ZIP CODE value like: 01123010 which is in the range of 01001000 and 05895490, so my routines should return me City Code = 3550308 and City Name = São Paulo.
I have no idea how to achieve this, I might have built the sample plist wrong.
I am confident I can build a plist file using AppleScript, reading from the Excel sheet.
But retrieving the City code for a given ZIP CODE range is a puzzle.
+++ EDIT: +++
I think I have solved it, but it looks kind of clumsy, as almost everything I write.
This AppleScript reads the Excel sheet and writes the plist file : http://www.idanfe.com/dl/creating.scpt.zip
Here you find the 1 MB plist file: http://www.idanfe.com/dl/cityCodes.plist.zip
This is the code I wrote to get the City Code I need:
NSString *zipCodeString;
zipCodeString = #"99990000";
NSString* plistPath = [[NSBundle mainBundle] pathForResource:#"cityCodes" ofType:#"plist"];
NSDictionary *cityCodes_dictionary = [NSDictionary dictionaryWithContentsOfFile:plistPath];
NSArray *allKeys = [cityCodes_dictionary allKeys];
int i = 0;
for (i = 0 ; i <= [allKeys count]; i++) {
NSString *someKey = [allKeys objectAtIndex:i];
NSRange range08 = NSMakeRange (0, 8);
NSRange range88 = NSMakeRange (8, 8);
NSString *startZipCode = [someKey substringWithRange:range08];
NSString *finalZipCode = [someKey substringWithRange:range88];
int startZipCodeInt = [startZipCode intValue];
int finalZipCodeInt = [finalZipCode intValue];
if(startZipCodeInt <= [zipCodeString intValue] && finalZipCodeInt >= [zipCodeString intValue]){
NSLog(#"we found a winner");
NSString *cityCode = [NSString stringWithFormat: #"%#",[[cityCodes_dictionary objectForKey:someKey]objectForKey:#"City Code"]];
[cityCodeIBGEField setStringValue:cityCode];
NSLog(#"cityCode = %#",cityCode);
break;
} else {
// NSLog(#"no winners");
}
}
Basically I append the start zipCode and finalZip Code into one string of 16 digits, so I create one single record in the plist file.
Then when searching for the City Code I break the long key (2 zip codes) in 2 (back to normal zipCode) and search to see which record fits the given zipCode I need a cityCode for.
Some how it doesn't look the best for me, but for my own surprise the code is very fast, although in a loop.
I would appreciate comments...
Thanks,
I would use indexOfObjectPassingTest: to do this kind of search. Something like this:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
NSString* plistPath = [[NSBundle mainBundle] pathForResource:#"cityCodes" ofType:#"plist"];
self.cityCodes_dictionary = [NSDictionary dictionaryWithContentsOfFile:plistPath];
[self findCityWithZip:#"01123010"];
}
-(void)findCityWithZip:(NSString *) searchZip {
NSUInteger indx = [self.cityCodes_dictionary.allKeys indexOfObjectPassingTest:^BOOL(NSString *zip, NSUInteger idx, BOOL *stop) {
NSString *startZipCode = [zip substringWithRange:NSMakeRange(0, 8)];
NSString *finalZipCode = [zip substringWithRange:NSMakeRange(8, 8)];
return (searchZip.integerValue < finalZipCode.integerValue && searchZip.integerValue > startZipCode.integerValue);
}];
NSLog(#"%#",[self.cityCodes_dictionary valueForKey:[self.cityCodes_dictionary.allKeys objectAtIndex:indx]]);
}
Reading the plist shouldn't be a problem at all if it is structured as a dictionary:
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile: #"cities.plist"];
//NSString *zipStr = [dict valueForKey: #"CityNme"]; //this was an example
That's the easy part. The harder part is parsing and structuring the plist file from an xls file.
Or did I misunderstand your question?
PS: to get a quick look at the dictionary's plist structure, create a mock dictionary and NSLog it's description to see how it's supposed to be (or writeToFile to see the file contents or of course refer to the docs)
edit
You load the supplied plist file in a dictionary using the above code. Then you retrieve another dictionary from within it using
NSDictionary *city = [dict valueForKey : #"yourZipCodeHere"];
From that dictionary you get the cityCode like this
NSString *cityCode = [city valueOrKey: #"City Code"];
As for your range problem, I'm not sure I understand it completely. But you can get an array of all the zip codes using
NSArray *zipArray = [dict allKeys];
And then you can simply iterate over that to get the correct zip code.
PS: I don't know much apple script and would be interested in how you converted the xls to plist using it.

Parser over txt document in xcode

I would like to go through .txt document and store text blocks in NSStrings. My problem is that this document contains linebreaks, and i don't know how to rid of those. It would be nice, if i could put each individual word into an ordered NSArray and then just go through that array and get information out from that. I would something like this:
// txt file
This is just a test.
End of the text file.
// NSArray and NSStrings
NSArray *wholeDocument =#"This","is","just","a","test","Foo","bar.", "End", "of", "the","text","file.";
NSString *beginDocument =#"This is just a test";
NSString *endDocument =#"End of the text file.";
Try this:
NSString *str = [NSString stringWithContentsOfFile:#"file.txt"];
NSArray *arr = [str componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
Here, arr will contain all the words which are separated either by spaces or by line breaks.

objective-c how to encode NSArray with UTF8?

I have scandinavian alphabets in my array like æ, ø, å. With NSLog the output showed these alphabets as scrambled codes. How to encode NSArray with UTF8? Any help is appreciated.
I tried only:
NSArray *nnoWords = [[NSArray arrayWithArray:newNoWords] retain];
NSLog (#"nnoWords: %# ", nnoWords);
newNoWords is a NSMutableArray. nnoWords containt normal objects like NSString hello, pear, apple, etc. taken from a txt file.
EDIT 29 august 2011:
nnoWords comes from this, converted to NSMutable and then back to NSArray, thus called nnoWords. And words.txt is encoded in UTF8.
NSArray *noWords = [[NSArray alloc] initWithArray:
[[NSString stringWithContentsOfFile:[[NSBundle mainBundle]
pathForResource:#"words" ofType:#"txt"]
encoding:NSUTF8StringEncoding error:NULL] componentsSeparatedByString:#"\n"]];
I have also tried:
NSString *norsk = #"æ ø å";
NSLog (#"%#", norsk);
And the output is correct:
2011-08-29 13:15:23.302 scanner2[29776:207] æ ø å
The problem is the Xcode console. If you view the output from Terminal.app, you will see it is as expected.
Test case:
//clang -framework Foundation -o log_utf8 log_utf8.m
#import <Foundation/Foundation.h>
int
main(void)
{
NSString *word = #"en ord på Svenska";
NSLog(#"%#", word);
}
Sample output:
StackOverflow$ clang -framework Foundation log_utf8.m -o log_utf8
StackOverflow$ ./log_utf8
2011-08-28 20:10:09.268 log_utf8[65105:707] en ord på Svenska
If you are still seeing gibberish when you view the output from something other than Xcode's questionable built-in console, then you need to examine how you're getting your string data:
How does the text with non-ASCII characters enter your application?
What encoding does NSString think that text is in?
What encoding is it actually in? If the text comes from a file, then the file command might be able to answer this for you.
Try this:
NSLog(#"%S", [[theArray description] cStringUsingEncoding:NSUTF16LittleEndianStringEncoding]);
from this question: NSLog incorrect encoding

Load coordinates from text-file into NSMutableArray

Im generating a plain txt file from Physics editor. It contains all the vertices for my polygon. Because I want my polygon to be textured, im running it through a triangulation method located at: https://github.com/asinesio/cocos2d-PRKit/
And I need my data to come from an NSMutableArray to doso for it to work.
Physics editor can export .plist and .txt files, but for simplicity sake, I just want to get the vertices from the .txt file and turn them into CGPoints and then add them into a NSMutableArray
The txt file looks like this:
(53.4011993408203, -44.4011993408203)
, (74.4011993408203,
-38.4011993408203) , (-0.598802387714386,
0.598802387714386) , (-0.598802387714386,
-39.4011993408203) , ...
I think the method would be to:
Load the data from its source.
Scan the data excluding parenthesis and alphbetical characters.
Take all the data upto the comma and add it into the CGPoint (x(1),y(0)).
Then scan all the data upto the next comma and insert it into the CGPoint (x(1),y(1)).
Then add this CGPoint to a NSMutableArray.
Continue scanning the document until all coordinates have been added.
This method could then be used with different text-files to create simplicity. Etc:
Level1ground.txt, Level2ground.txt.. It would be fantastic if I could get it running.
Could someone please help me with this?
Much Appreciated,
Oliver.
This solution assumes that you have loded the file already into a string.
You can make use of pathForResource:ofType:inDirectory: of NSBundle class to load a file.
NSArray * rawPoints = [#"(53.4011993408203, -44.4011993408203) , (74.4011993408203, -38.4011993408203) , (-0.598802387714386, 0.598802387714386) , (-0.598802387714386, -39.4011993408203)" componentsSeparatedByString:#" , "];
for (NSString * rawPoint in rawPoints) {
NSString *tmp = [rawPoint stringByReplacingOccurrencesOfString:#"(" withString:#""];
tmp = [tmp stringByReplacingOccurrencesOfString:#"(" withString:#""];
NSArray * coordinates = [tmp componentsSeparatedByString:#", "];
CGPoint point;
for (NSString * coordinate in coordinates) {
point = CGPointMake([[coordinates objectAtIndex:0] floatValue],
[[coordinates objectAtIndex:1] floatValue]);
}
NSLog(#"x:%f, y:%f", point.x, point.y);
}
In this case, you probably want to use an NSScanner instance. See -scanUpToCharactersInSet: and -scanFloat.