How to make space between spannable string in Andr

2020-07-13 09:49发布

问题:

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;
        }
    }

Above function is useful for creating spannable String and to add Border to it. I am calling 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 I am getting from above is:

The problem with the above code is:

  • How to add space between two spannable strings?

  • How can I add space at the starting of rectangle and first character of a string similar to end of rectangle. I tried to add the space by this code :

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

but it gave me this distorted result:

If I am not wrong then the problem with the above code is there are not enough spaces between two spannable strings.

How can I make it correct?

Edit-1

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

Look at the first one it's slightly cutting from starting point. But If I used the above configuration I don't have enough space in two strings. This is the main Issue.

回答1:

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

so remove + 10 from param for right

like this:

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

also to add space at starting put some negative margin at starting like this:

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

following image will explain this visually: