I'm getting a string from a server call, and Trying to set that string to the value of the text in a UITextField. However, if the text has any special characters, the encoding gets screwed up on those characters.
-(void)contentDidLoad:(NSString *)content
{
NSLog(@" content being put into textfield is : %@", content);
self.textField.text = content;
NSLog(@" content in the textfield is : %@", self.textField.text);
}
If the string is "töä", this is the output of the Logs:
content being put into textfield is : töä
content in the textfield is : töä
Anyone know whats going on?
UPDATE:
Actually, I've run into a bit of a problem with Pugal's solution. If the string contains any %'s, the string returned by calling stringByReplacingPercentEscapesUsingEncoding will be nil. To get around this, I tried calling stringByReplacingOccurancesOfString, replacing every % with %25. However, after calling that method, the encoding is messed up already! This bug is driving me crazy!
NSLog(@"Original content is: %@", contentPage.content);
NSString *text= [contentPage.content stringByReplacingOccurrencesOfString:@"%" withString:@"%25"];
NSLog(@"after % replacement, content is: %@", text);
self.textField.text = [text stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"textfield text is: %@", self.textField.text);
2010-08-04 09:43:31.742 Confluence[49621:207] Original content is: 90% of all statistics are made up always. Åöä코셔
2010-08-04 09:43:31.744 Confluence[49621:207] after replacement, content is: 90%25 of all statistics are made up always. Ãöäì½ì
2010-08-04 09:43:31.747 Confluence[49621:207] textfield text is: 90% of all statistics are made up always. Ãöäì½ì