The way to show uncommon characters and emoji on Android

Source: Internet
Author: User

In theory, Android can display all Unicode characters, display all Chinese characters (currently there are more than 80,000 East Asian characters in Unicode), emoji of course, no problem.
Chinese characters with Unicode values below 0xFFFF have long been widely supported, so this article does not treat them as uncommon characters. There are more than 20,000 simplified and traditional Chinese characters .
The uncommon word in this paper refers to Chinese characters with Unicode values above 0xFFFF, and emoji is in this range. This range is also called non-bmp.
You can read this article to understand the approximate distribution of Chinese characters.

There are mainly three key points:

1, make sure that the character set can overwrite uncommon words. that is, the character set used to save the string in a file or database should be: GB18030, UTF-16, UTF-32, and so on, which can support all Unicdoe characters .
Myth A: If you use the GB2312 or GBK character set, they are limited in scope and uncommon characters are not included.
Myth B:UTF-8 encoding also supports all Unicode characters in theory, but Android's UTF-8 decoder seems to have problems, only a subset of the range of characters commonly used characters can be supported, see this post. (This article has not been verified, for reference only)

2, string in the processing of uncommon words will use surrogate pair.
The Java program string uses the UTF-16 encoding. A char of string is two bytes and can represent a range of values between 0~0xffff.
So in string, the characters commonly used (Unicode value is less than or equal to 0xFFFF) is represented by a char, and the remaining characters (Unicode values greater than 0xFFFF) are represented by two char combinations, and these two char are called surrogate pairs . So our uncommon characters need to be represented in two char combinations .

The principle of surrogate is: Two char each has a reserved area, the reserved area has 1024 positions, so 1024x1024 is approximately equal to 1.04 million, plus the characters contained in a single char, It is possible to overwrite all Unicode 110多万个 location points.

We don't need to worry about this, just remember that the return value of the length () function of the string may be greater than the actual number of characters , and you want to know the actual number of characters in the string can be used with the Codepointcount () function. Similarly, char-based functions such as charAt () may no longer apply.

3. Use fonts that support uncommon characters.
Even if the program supports uncommon characters, fonts are not supported or displayed. Many fonts, including the default font, do not support uncommon characters.
We can find a font that supports uncommon characters and then put it in the program's resource directory, specifying that it is used instead of the system's default font. Packaging the font of the program will be much larger, this is no way to do.
Link A: A font that supports all Chinese characters, posts, web drives.
Link B: The way to use the specified font in the program see method two of this article.

The way to show uncommon characters and emoji on Android

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.