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

时间:2012-12-28 16:41:37

标签: 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"];

1 个答案:

答案 0 :(得分:1)

您需要设置UTF-8编码。

编辑以回复修改后的问题

只需添加NSUTF8StringEncoding,如下所示

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