I have the following TextView within a ConstraintLayout:
<TextView
android:id="@+id/title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="3"
android:textAppearance="@style/TextAppearance.AppCompat.Title"
... />
When the text is too long to fit in one line, it will break into two lines. However, if the system language is set to Swedish it will break in the middle of words without adding a hyphen, like so:
Some text that is br oken into two lines
I'm not sure if this is an accurate example, but you get the idea.
What I want is this:
Some text that is br- oken into two lines
It does work when the system language is set to English. But it should also work for Swedish.
I have tried all possible combinations of android:breakStrategy="..." and android:hyphenationFrequency="..." on the TextView.
compileSdkVersion 28
targetSdkVersion 28
minSdkVersion 28
edit: I've implemented a workaround in the form of an extension function in Kotlin.
fun TextView.setAddHyphenAtWordBreakEnabled() {
viewTreeObserver.addOnGlobalLayoutListener {
for (i in 0 until layout.lineCount) {
val offset = layout.getLineVisibleEnd(i)
if (text.isNotEmpty()
&& offset != -1
&& offset != text.length
&& text[offset].toString() != " "
&& text[offset - 1].toString() != "-") {
text = text.let {
StringBuilder(it).insert(offset, "-")
}
}
}
}
}
from Hyphens are not added at line endings to join words in some languages
No comments:
Post a Comment