Several Problems about displaying Chinese text files with UIWebView

Source: Internet
Author: User

UIWebView supports text file display. You can use the following code to load text files:

[

Unfortunately, for Chinese, UIWebView does not support very well:

The 48_1.txt file contains Chinese characters. We do not know what encoding it is. It may be UTF8, GBK, or GB18030. We can infer its encoding through the stringWithContentsOfFile method:

{

Encoding = 0x80000632;

Body = [

}

Encoding = 0x80000631;

Body = [

}

Replace them with UTF16 encoding and save them to a new file:

[InData

}

}

Note that the pathForTemporaryFile function is customized. It actually points the file to the temp folder of the sandbox.

In this way, UIWebView can correctly display Chinese characters:

But I don't know why, when UIWebView displays a text file, several bytes at the end of the file are displayed as squares. These squares are actually some "\ 0" characters. When I open the 48_1.txt file using a text editor or TextMate program, these "\ 0" characters are not displayed on the screen. However, in UIWebView, they are displayed as squares. Therefore, you can add the following code before saving the file:

Body = [body

In this way, you can remove those blocks.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.