iphone - 如何让外语文本在 UITextView 中正确显示

标签 iphone objective-c ios uitextview

我有一些文本未在 UITextView 中正确显示。 “é”、“ç”和“ñ”等都显示如下:

enter image description here

文本来自服务器,在网络浏览器中查看时可以正常显示。

想知道如何解决这个问题。感谢您的帮助。

代码如下:

  // the deal id is passed from the offersvc table selected item and into the url
  // dealID is 1020414

    NSString *jsonDealString = [NSString stringWithFormat:@"http://api.**********.net/v1/public/details?id=%@&yd.key=******", dealId];

    NSLog(@"jsondealstring is %@", jsonDealString);

   // NSLog(@"deal id is %@",dealId);

    // Download JSON
    NSString *jsonString = [NSString
                            stringWithContentsOfURL:[NSURL URLWithString:jsonDealString]
                            encoding:NSStringEncodingConversionAllowLossy
                            error:nil];

    // Create parser for the api
    SBJSON *parser = [[SBJSON alloc] init];
    NSDictionary *results = [parser objectWithString:jsonString error:nil];

    [self setDisplayItems:[results objectForKey:@"results"]];

    NSDictionary *item = (NSDictionary *)[displayItems objectAtIndex:0];

//    NSLog(@"item from details is %@", item);

    // set the labels and text views. 

    link = [item objectForKey:@"link"];
    catStr = [item objectForKey:@"cat"];
    vendorStr = [item objectForKey:@"vendor"];
    titleStr = [item objectForKey:@"main_tag"];

最佳答案

您需要设置UTF-8编码。

编辑以回应修改后的问题

只需添加 NSUTF8StringEncoding 如下

NSString *jsonString = [NSString
                        stringWithContentsOfURL:[NSURL URLWithString:jsonDealString]
                        encoding:NSStringEncodingConversionAllowLossy|NSUTF8StringEncoding
                        error:nil];

关于iphone - 如何让外语文本在 UITextView 中正确显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14072742/

相关文章:

ios - 为 PHP 发送参数,并等待响应

java - 如何在 iPhone 真实设备上检查和编写 xpath

iOS:从 CNContact 获取数字 key

ios - 使用 MKPinAnnotationView 和 MKAnnotationView 更新用户位置

ios - iPad 2 和 iPhone 4s 上不同的 UITableView 滚动性能

iphone - 如何在iPhone上构建WebKit?

iphone - 检查文档目录中是否存在文件

ios - 在单个 ViewController 上使用多个 Tableview

iphone - 将触摸传递给下一个响应者或其他 subview iOS

ios - tableView 重新加载时崩溃