I have long text containing name that looks like "something-something". This long text is shown in TextView. The problem is "something-something" got line breaked.
I have found unicode character U+2011 NON-BREAKING HYPHEN. But it looks like this unicode character is supported in font since Android 3.0. However I'm supporting Android 2.1 where replacement character is shown instead.
I have looked at class Spannable, but I did not find how to define nonbreaking block of text. Maybe I overlook something.
To set the maximum length for text in TextView widget, set the maxLength attribute with required number of length.
SpannableString string = new SpannableString("Text with clickable text"); string. setSpan(new CustomClickableSpan(), 10, 19, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); Text with ClickableSpan .
I solved breaking of the text block by implementing ReplacementSpan
to render text in single block. Here is the code:
public class NonbreakingSpan extends ReplacementSpan {
@Override
public void draw(
Canvas canvas,
CharSequence text, int start, int end,
float x, int top, int y, int bottom,
Paint paint) {
canvas.drawText(text, start, end, x, y, paint);
}
@Override
public int getSize(
Paint paint,
CharSequence text, int start, int end,
FontMetricsInt fm) {
return Math.round(paint.measureText(text, start, end));
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With