Im trying to fetch a random Emoji character from an array. how can i convert these values into unicode format without XCode having a fit? :
label.text = [NSString stringWithFormat:#"\U%#",[emojis randomObject]];
I get : \U used with no following hex digits every time.
can you not construct a unicode format like that?
Here was my solution:
NSString *code = emojArray[i];
NSScanner *hexScan = [NSScanner scannerWithString:code];
unsigned int hexNum;
[hexScan scanHexInt:&hexNum];
UTF32Char inputChar = hexNum;
NSString *res = [[NSString alloc] initWithBytes:&inputChar length:4 encoding:NSUTF32LittleEndianStringEncoding];
res, when displayed in a UILabel is now an emoji.
Related
So, I know how to convert a unicode hex code into an NSString consisting of the unicode character referenced by that code:
NSString *ucStr = #"\\u004A"; // hex code for capital J
NSString *theLetter = [ucStr mutableCopy];
CFStringRef transform = CFSTR("Any-Hex/Java");
CFStringTransform((__bridge CFMutableStringRef)theLetter, NULL, transform, YES);
// theLetter is now #"J"
...However, I don't seem to understand how to go in the other direction, i.e. starting with an NSString #"J", output the NSString #"004A".
Simply extract each character and format it using the format string #"%04x", as below:
NSString *input = #"How now brown cow";
for (NSUInteger i = 0; i < [input length]; i++) {
unichar c = [input characterAtIndex:i];
NSLog(#"%04x", (unsigned)c);
// or NSString *s = [NSString stringWithFormat:#"%04x", (unsigned)c];
}
BTW I don't understand the code you have posted, but as that wasn't the question, it doesn't matter.
I have a label where I have to put a string in Chinese extracted from a database, but nothing comes out. I noticed that the string is not pulled from database, while all other work correctly. What can I do?
char *subTitle= (char*)sqlite3_column_text(statement,13);
NSLog(#" The sutitle is %s", subTitle);
//The sutitle is
rowTable.sottotitolo = [[NSString alloc]initWithUTF8String: subTitle];
NSLog(#"The subtitle is %#", rowTable.sottotitolo);
//The subtitle is
Using methods other than Western alphabet?
NSLog(#"The string in chinese is %#", self.chinaTable.subtitle);
//The string in chinese is
//is not printed to the screen,but the database is written correctly
self.labelTitle.text = self.chinaTable.subtitle;
//empty out
Thanks in advance
While you retrieving your data from sqlite, instead of specifying the encoding schema, use this:
NSString *myChineseText = [NSString stringWithFormat:#"%s",(const char*)sqlite3_column_text(statement, index)];
NSLog(#"%#",myChineseText);
Hope, it'll solved your problem. :)
Try CFStringConvertEncodingToNSStringEncoding and kCFStringEncodingBig5_E.
Also see apple doc and for international
or for creating own encoding see
and this
unichar ellipsis = 0x2026;
NSString *theString = [NSString stringWithFormat:#"To be continued%C", ellipsis];
// custom encoding
NSStringEncoding encoding = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingDOSChineseTrad);
NSData *asciiData = [theString dataUsingEncoding:encoding
allowLossyConversion:YES];
NSString *asciiString = [[NSString alloc] initWithData:asciiData
encoding:encoding];
I have a project where I am trying to grab text from a uilabel that has been populated from a web service. I can grab and manipulate the text just fine but I need to send certain characters from the string to another web service call. I can not figure out how to grab the first, second and last characters in my string. I am both new to Objective-C as well as programming so any help would be much appreciated.
You can do it like this:
UILabel * l = [[UILabel alloc] init];
l.text = #"abcdef"; //set text to uilabel
[self.view addSubview:l];
NSString * text = l.text; //get text from uilabel
unichar first = [text characterAtIndex:0]; //get first char
unichar second = [text characterAtIndex:1];
unichar last = [text characterAtIndex:text.length -1];
If you need results as strings you can use:
NSString * firstAsString = [text substringWithRange:NSMakeRange(0, 1)]; //first character as string
or you can convert the unichar to string like this:
NSString * x = [NSString stringWithFormat:#"%C", last];
Per this it should be fairly easy:
http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UILabel_Class/Reference/UILabel.html
textLabel.text = #"Foo";
Where textLabel is instance of UILabel
Hi i am reading a book by aaron hillegass on cococa programming and I doing one of the mini tasks he asks us to do.
the task is to create an application that has one window open and has 1 input text field, a button and a label.
when a user inputs some text and presses the button, the label displays the text and the length of the text inputted.
Here is what I have got so far
//retrieve text from textfield
NSString *string = [textFieldInput stringValue];
//retrieve length of text and store in NSInteger called length
NSInteger length = [string length];
//store length in string format
NSString *string_length = [NSString stringWithFormat:#"%d", length];
//join strings
NSString *full_string = [string stringByAppendingString:(#"has ",string_length,#" characters")];
//set label text
[textField setStringValue:full_string];
however the actual string is shown and the characters string is shown, just not the string_length. any suggestions and am i going about this in the right way? Thanks.
NSString *fullString = [string stringByAppendingFormat:#"has %# characters", string_length];
//retrieve text from textfield
NSString *string = [textFieldInput stringValue];
NSString *fullString = [string stringByAppendingFormat:#" has %d characters", [string length]];
//set label text
[textField setStringValue:fullString];
Your usage of stringByAppendingString: is wrong.
If I understand you correctly, you are trying to pass a list of strings that should be appended to string but that method only takes a single string argument.
You can try the following:
NSString* fullString = [string stringByAppendingString:[NSString stringWithFormat:#"%# %# %#", #"has ", string_length, #" characters"]];
I have a ascii code, for the letter 'a', and I want to get a string by its ascii code, is it possible with NSString?
This could also work:
NSString *foo = [NSString stringWithFormat:#"%c", 97];
Didn’t test it.
If you mean you have a byte that represents an ASCII-encoded character and you want to make a string out of it, NSString has an initializer just for that.
char characterCodeInASCII = 97;
NSString *stringWithAInIt = [[NSString alloc] initWithBytes:&characterCodeInASCII length:1 encoding:NSASCIIStringEncoding];