Android: How to make an indestructible block in a TextView?

I have a long text containing a name that looks like "something." This long text is shown in a TextView. The problem is that β€œsomething” got a line break.

I found the Unicode character U + 2011 NON-DISTRIBUTING GIFT. But it looks like this Unicode character is supported by a font with Android 3.0. However, I support Android 2.1, where a replacement character is displayed instead.

I looked at the Spannable class, but I did not find how to define a non-intercepting block of text. Maybe I'm missing something.

+6
source share
1 answer

I decided to split the text block by implementing ReplacementSpan to render the text in one 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)); } } 
+2
source

Source: https://habr.com/ru/post/895315/


All Articles