Ashwini Chougale
Ashwini Chougale

Reputation: 1093

How to convert Chinese Html String into NSString

For converting html string into NSString i am using below function

Code Snippet :

+(NSString *)getStringFromHtmlString:(NSString *)str
{

    NSData *stringData = [str dataUsingEncoding:NSUTF8StringEncoding];
    //NSHTMLTextDocumentType
    NSDictionary *options = @{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType};

    NSAttributedString *decodedString;
    decodedString = [[NSAttributedString alloc] initWithData:stringData
                                                     options:options
                                          documentAttributes:NULL
                                                       error:NULL];


    return decodedString.string;
}

Above method encode English text in proper format

But I want to encode Chinese text.

When Chinese string encode using above method it returns

I want proper encoding of chinese text.
enter image description here

Upvotes: 3

Views: 433

Answers (2)

Jadian
Jadian

Reputation: 4181

Swift Version:

let options: [NSAttributedString.DocumentReadingOptionKey: Any] = [
    .documentType: NSAttributedString.DocumentType.html,
    .characterEncoding: String.Encoding.utf8.rawValue
]

Upvotes: 0

BSB
BSB

Reputation: 277

You can add NSCharacterEncodingDocumentAttribute:@(NSUTF8StringEncoding) options:

NSDictionary *options = @{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute:@(NSUTF8StringEncoding)};

Hope it will work.

Upvotes: 4

Related Questions