Intervals using SpannableStringBuilder in a TextView

As the question indicates, I am working on a TextView that will display formatted text using SpannableStringBuilder . It has several paragraphs, and I would like to know what would be the easiest (or at least least difficult) way to set the spacing between paragraphs using some built-in range. Is it possible? Or will I need to create a special span class for this?

+5
source share
1 answer

Implement the LineHeightSpan method and override chooseHeight as follows

 @Override public void chooseHeight(CharSequence text, int start, int end, int spanstartv, int v, FontMetricsInt fm) { Spanned spanned = (Spanned) text; int st = spanned.getSpanStart(this); int en = spanned.getSpanEnd(this); if (start == st) { fm.ascent -= TOP_SPACING; fm.top -= TOP_SPACING; } if (end == en) { fm.descent += BOTTOM_SPACING; fm.bottom += BOTTOM_SPACING; } } 

Remember to add \n to the end of your text for each paragraph.

+7
source

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


All Articles