如何使用spannable向自定义文本添加填充,笔触和半径

时间:2015-08-30 04:42:21

标签: android spannable

如何为可跨越文本添加填充,边角和笔触。下面的代码设置backgroundcolorspan和自定义字体。我非常感谢任何帮助。

     public class CustomTextView extends TextView {
            public CustomTextView(Context context) {
                super(context);
                setFont();
            }
            public CustomTextView(Context context, AttributeSet attrs) {
                super(context, attrs);
                setFont();
            }
            public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
                super(context, attrs, defStyle);
                setFont();
            }

            private void setFont() {
                Typeface font = Typeface.createFromAsset(getContext().getAssets(), "fonts/TEXT.ttf");
                setTypeface(font, Typeface.NORMAL);


                setText(getText(), BufferType.SPANNABLE);




            }

           @Override
            public void setText(CharSequence text, BufferType type) {
                SpannableString span = new SpannableString(text);
                  span.setSpan(new RoundedBackgroundSpan(), 0, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        super.setText(span, type);
    }


    public class RoundedBackgroundSpan extends ReplacementSpan {


        @Override
        public  void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint)
        {
            RectF rect = new RectF(x, top, x + measureText(paint, text, start, end), bottom);
            paint.setColor(Color.BLUE);
            canvas.drawRoundRect(rect, 1, 1, paint);
            paint.setColor(Color.MAGENTA);
            canvas.drawText(text, start, end, x, y, paint);
        }
        @Override
        public  int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm)
        {
            return Math.round(measureText(paint, text, start, end));
        }

        private float measureText(Paint paint, CharSequence text, int start, int end)
        {
            return paint.measureText(text, start, end);
        }

    }
}

Xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <stroke
        android:width="1dp"
        android:color="@color/red" />

    <solid android:color="@color/red" />

    <padding
        android:left="10dp"
        android:right="10dp"
        android:top="5dp"
        android:bottom="5dp"/>

    <corners android:radius="2dp" />

</shape>

我的实施:

Mainactivity.class

     txtView = (TextView) findViewById(R.id.textView_custom);


    txtView.setText("Lorem ipsum dolor sit amet, autem oporteat disputationi ut est, quo in quem aliquip delicatissimi ");

在XML中:

  <com.example.custom_font.CustomTextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="hello"
                   android:id="@+id/textView_custom"
                    />

1 个答案:

答案 0 :(得分:0)

您正在绘制圆角,1px作为圆角半径,增加到适当的数量。还要增加填充,调整矩形的尺寸。代码修改如下

@Override
public  void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint)
   {
        RectF rect = new RectF(x - 20, top, x + measureText(paint, text, start, end) + 20, bottom);
        paint.setColor(Color.BLUE);
        canvas.drawRoundRect(rect, 20, 20, paint);
        paint.setColor(Color.MAGENTA);
        canvas.drawText(text, start, end, x, y, paint);
    }