how to add padding,stroke and radius to custom text with spannable

▼魔方 西西 提交于 2020-01-03 06:06:09

问题


How can I add padding,corner and stroke to spannable text. The code below sets the backgroundcolorspan and custom font.I really appreciate any help.

     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>

My implementation:

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 ");

In 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:


You are drawing a rounded with 1px as the rounded radius, increase to appropriate amount. also to increase the padding, adjust the dimensions of the rectangle. code modified below

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


来源:https://stackoverflow.com/questions/32293229/how-to-add-padding-stroke-and-radius-to-custom-text-with-spannable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!