I\'m making my own text processor in Android (a custom vertical script TextView for Mongolian). I thought I would have to find all the line breaking locations myself so that
BreakIterator
can be used to find the possible breaks between characters, words, lines, and sentences. This is useful for things like moving the cursor through visible characters, double clicking to select words, triple clicking to select sentences, and line wrapping.
The following code is used in the examples below. Just adjust the first part to change the text and type of BreakIterator
.
// change these two lines for the following examples
String text = "This is some text.";
BreakIterator boundary = BreakIterator.getCharacterInstance();
// boiler plate code
boundary.setText(text);
int start = boundary.first();
for (int end = boundary.next(); end != BreakIterator.DONE; end = boundary.next()) {
System.out.println(start + " " + text.substring(start, end));
start = end;
}
If you just want to test this out, you can paste it directly into an Activity's onCreate
in Android. I'm using System.out.println
rather than Log
so that it is also testable in a Java only environment.
I'm using the java.text.BreakIterator
rather than the ICU one, which is only available from API 24. See the links at the bottom for more information.
Change the boilerplate code to include the following
String text = "Hi 中文éé\uD83D\uDE00\uD83C\uDDEE\uD83C\uDDF3.";
BreakIterator breakIterator = BreakIterator.getCharacterInstance();
Output
0 H
1 i
2
3 中
4 文
5 é
6 é
8