Hyphenation in Android

ぃ、小莉子 提交于 2019-11-27 11:39:57

Management of line breaks can be a hassle. The best option is to use UTF line-break modifier control characters since android supports full UTF

I know someone mentioned "soft-hyphen", but there are quite a few more.

You can also use the "Zero Width Space" between words on languages that lack spaces so you don't have to rely on dictionary interpretation. You can also use this as a soft-hyphen in languages that allow breaking of certain words over lines at certain points.

When using a compound word that you don't want broken, but you want the Text To Speech system to recognize it properly you should use "Word Separator" character. Don't use "Zero Width Non Breaking Space" as that has been deprecated due to it's use as BOM.

Finally, if you want a space but don't want a line break, use a simple non-breaking space.

The following library supports hyphenation. It does all types of text alignment (left/right/center/justified) and hyphenation for you. Not all languages have been added but can be added as necessary. This library uses NO WEBVIEWS and SUPPORTS SPANNABLES and allows for LONG TEXT.

LIBRARY: https://github.com/bluejamesbond/TextJustify-Android

ANDROID: 2.2 to 5.X

SETUP

DocumentView documentView = addDocumentView(new StringBuilder("Your long text content"), DocumentView.PLAIN_TEXT);
documentView.getDocumentLayoutParams().setTextAlignment(TextAlignment.JUSTIFIED);
documentView.getDocumentLayoutParams().setHyphenator(new Hyphenator(HyphenPattern.PT));
documentView.getDocumentLayoutParams().setHyphenated(true);

Its a new thing from Android 6 Marshmellow.

Try adding this to your TextView xml

android:hyphenationFrequency="none"

Soft hyphen worked on a Samsung Galaxy device starting with Android 4.3.

<!-- key combination to enter soft hyphen: [Alt Gr]+[-] or [Alt]+240 on German PC, see https://de.wikipedia.org/wiki/Weiches_Trennzeichen#Darstellung_auf_Computersystemen -->
<string name="no_connection">Nicht ver-bund-en</string>

Since my use case was pretty narrow, I just used one soft hyphen in the word "verbunden". The Unicode \u00ad had no effect.

The question is old but just found best solution for me:

I have to say i'm programming in Xamarin, so the code is in C# but should not be a problem to port into java.

I used the NHyphenator Logic (https://github.com/alkozko/NHyphenator) for Inserting SoftHyphens(UTF8 Symbol - 0x00AD) so the Textview do hyphenation at the right place.

To get Hyphenation for other languages i used the openoffice Dictionaries. e.g. i had to get Hyphenation for german-swiss language

http://extensions.openoffice.org/en/search?f[0]=field_project_tags%3A157 1. Download the extension 2. Unzip it with winrar or something else 3. Copy the hyph_xx_xx/hyph_xx_xx.dic file 4. Add new Language to the Hyphenator class

NHyphernator resource files are declared as: hyph-xx-xx.pat.txt --> content of the dic file without comments hyph-xx-xx.hyp.txt --> file which contains word-exceptions where the logic for hyphening does not give correct results

If anyone wants the portable Library for Xamarin just tell, i can upload it.

EDIT:

The breakstrategy should be set to Balanced in TExtview. API Level > 23, else don't use breakstrategy.

EDIT:

Here's the mono/xamarin code: https://github.com/sma84/NHyphenator-Mono

setEllipsize might be of help, if this error is yet fixed.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!