如何填充,中风和半径添加到自定义文本与spannable [英] how to add padding,stroke and radius to custom text with spannable

查看:227
本文介绍了如何填充,中风和半径添加到自定义文本与spannable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何添加填充,角落和中风spannable文本。下面的code设置backgroundcolorspan和定制font.I真的AP preciate任何帮助。

 公共类CustomTextView扩展的TextView {
            公共CustomTextView(上下文的背景下){
                超级(上下文);
                并入setfont();
            }
            公共CustomTextView(上下文的背景下,ATTRS的AttributeSet){
                超(背景下,ATTRS);
                并入setfont();
            }
            公共CustomTextView(上下文的背景下,ATTRS的AttributeSet,诠释defStyle){
                超(背景下,ATTRS,defStyle);
                并入setfont();
            }            私人无效的并入setfont(){
                字体字型= Typeface.createFromAsset(的getContext()getAssets(),字体/ TEXT.ttf);
                setTypeface(字体,Typeface.NORMAL);
                的setText(gettext的(),BufferType.SPANNABLE);
            }           @覆盖
            公共无效的setText(CharSequence的文字,BufferType型){
                SpannableString跨度=新SpannableString(文本);
                  span.setSpan(新RoundedBackgroundSpan(),0,text.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        super.setText(跨度型);
    }
    公共类RoundedBackgroundSpan扩展ReplacementSpan {
        @覆盖
        公共无效画(油画画布,CharSequence的文字,诠释开始,诠释年底,浮法X,INT顶部,INT Y,INT底,涂料粉刷)
        {
            RectF RECT =新RectF(X,顶部,X + measureText(油漆,文本,开始,结束),底部);
            paint.setColor(Color.BLUE);
            canvas.drawRoundRect(矩形,1,1,油漆);
            paint.setColor(Color.MAGENTA);
            canvas.drawText(文字,开始,结束,X,Y,油漆);
        }
        @覆盖
        公众诠释的getSize(涂​​料粉刷,CharSequence的文字,诠释开始,诠释年底,Paint.FontMetricsInt FM)
        {
            返回Math.round(measureText(油漆,文本,开始,结束));
        }        私人浮动measureText(涂料粉刷,CharSequence的文字,诠释开始,诠释完)
        {
            返回paint.measureText(文字,起点,终点);
        }    }
}

XML:

 <?XML版本=1.0编码=UTF-8&GT?;
<形状的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>    <中风
        机器人:宽=1DP
        机器人:颜色=@色/红/>    [固体机器人:色=@色/红/>    <填充
        机器人:左=10dp
        机器人:右=10dp
        机器人:顶部=5DP
        机器人:底部=5DP/>    <角落的an​​droid:半径=2DP/>< /形状>

我的实现:

Mainactivity.class

  txtView =(的TextView)findViewById(R.id.textView_custom);
    txtView.setText(Lorem存有悲坐阿梅德,autem oporteat disputationi UT EST,在现状截止日aliquip delicatissimi);

在XML:

 < com.example.custom_font.CustomTextView
                    机器人:layout_width =WRAP_CONTENT
                    机器人:layout_height =WRAP_CONTENT
                    机器人:文字=你好
                   机器人:ID =@ + ID / textView_custom
                    />


解决方案

您正在绘制一个圆角与1px的为圆角半径,增加适量。还增加了填充,调整矩形的尺寸。 code以下修改

  @覆盖
公共无效画(油画画布,CharSequence的文字,诠释开始,诠释年底,浮法X,INT顶部,INT Y,INT底,涂料粉刷)
   {
        RectF RECT =新RectF(X - 20,顶部,X + measureText(油漆,文本,开始,结束)+ 20,底部);
        paint.setColor(Color.BLUE);
        canvas.drawRoundRect(矩形,20,20,油漆);
        paint.setColor(Color.MAGENTA);
        canvas.drawText(文字,开始,结束,X,Y,油漆);
    }

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"
                    />

解决方案

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

这篇关于如何填充,中风和半径添加到自定义文本与spannable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆