How to make space between spannable string in Android?

Code:

private void setSpans(Editable s, @ColorInt int backgroundColor) { BackgroundColorSpan[] spans = s.getSpans(0, s.length(), BackgroundColorSpan.class); String[] words; if (s.toString().endsWith(" ")) { words = (s.toString() + "X").split("\\s"); } else { words = s.toString().split("\\s"); } int completedWordsCount = words.length - 1; if (spans.length != completedWordsCount) { for (BackgroundColorSpan span : spans) { s.removeSpan(span); } int currentIndex = 0; for (int i = 0; i < words.length-1; i++) { s.setSpan(new CustomDrawble(Color.GRAY, Color.WHITE), currentIndex, currentIndex + words[i].length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); currentIndex += words[i].length() + 1; } } 

The above function is useful for creating a spannable String and adding a Border to it. I call the following class:

 public class CustomDrawble extends ReplacementSpan { private int mBackgroundColor; private int mForegroundColor; public CustomDrawble(int backgroundColor, int foregroundColor) { this.mBackgroundColor = backgroundColor; this.mForegroundColor = foregroundColor; } @Override public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) { return Math.round(measureText(paint, text, start, end)); } @Override public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) { float padding = 4f; RectF rect = new RectF(x, top + 3, x + measureText(paint, text, start, end) + 10, bottom + 10); paint.setColor(mBackgroundColor); canvas.drawRoundRect(rect, 10,10,paint); paint.setColor(mForegroundColor); canvas.drawText(text, start, end, x, y, paint); } private float measureText(Paint paint, CharSequence text, int start, int end) { return paint.measureText(text, start, end); } } 

The result that I get on top:

enter image description here

The problem with the above code:

  • How to add space between two spannable lines?
  • How to add space when starting a rectangle and the first character of a line, similar to the end of a rectangle. I tried to add a space for this code:

     RectF rect = new RectF(x+10, top + 3, x + measureText(paint, text, start, end) + 10, bottom + 10); 

but he gave me this distorted result:

enter image description here

If I'm not mistaken, the problem with the above code is that there spannable not enough spaces between the two spannable lines.

How can I do it right?

Edit-1

Used RectF rect = new RectF(x - 5, top + 3, x + measureText(paint, text, start, end)+5, bottom + 10);

enter image description here

Look at the first one, which he cuts slightly from the starting point. But if I used the above configuration, I would run out of space on two lines. This is the main question.

+5
source share
1 answer

Removing extra pixels from the end of your rect will solve the problem of no space between spans

so remove + 10 from the parameter for right

like this:

 RectF rect = new RectF(x, top + 3, x + measureText(paint, text, start, end), bottom + 10); 

also add space at startup by putting some negative margin at startup for example:

  RectF rect = new RectF(x - 5, top + 3, x + measureText(paint, text, start, end), bottom + 10); 

The following image will explain this visually:

dig

+1
source

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


All Articles