美文网首页
(转)Android自定义TextView自适应字体大小

(转)Android自定义TextView自适应字体大小

作者: 曾经的追风少年 | 来源:发表于2017-12-01 13:59 被阅读0次

原文地址:http://m.blog.csdn.net/qq_27489007/article/details/78063346
刚好有这个需求--根据textview的高度自适应字体大小,找到网友的这个帖子,解决问题。做一个记录,谢谢大神的分享!!!

1、根据高度自适应
import android.content.Context;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.TextView;


/**
 * Created by akitaka on 2017/9/20.
 *
 * @filename FitHeightTextView
 * @describe 根据高度自适应字体文字大小
 * @email 960576866@qq.com
 */

public class FitHeightTextView extends TextView {
    private Paint mTextPaint;
    private float mMaxTextSize; // 获取当前所设置文字大小作为最大文字大小
    private float mMinTextSize = 8;    //最小的字体大小

    public FitHeightTextView(Context context) {
        super(context);
    }

    public FitHeightTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        setGravity(getGravity() | Gravity.CENTER_VERTICAL); // 默认水平居中
        setLines(1);
        initialise();
    }

    private void initialise() {
        mTextPaint = new TextPaint();
        mTextPaint.set(this.getPaint());
        //默认的大小是设置的大小,如果撑不下了 就改变
        mMaxTextSize = this.getTextSize();
    }

    //文字改变的时候
    @Override
    protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
        refitText(text.toString(), this.getHeight());   //textview视图的高度
        super.onTextChanged(text, start, lengthBefore, lengthAfter);
    }

    private void refitText(String textString, int height) {
        if (height > 0) {
            int availableHeight = height - this.getPaddingTop() - this.getPaddingBottom();   //减去边距为字体的实际高度
            float trySize = mMaxTextSize;
            mTextPaint.setTextSize(trySize);
            while (mTextPaint.descent()-mTextPaint.ascent() > availableHeight) {   //测量的字体高度过大,不断地缩放
                trySize -= 1;  //字体不断地减小来适应
                if (trySize <= mMinTextSize) {
                    trySize = mMinTextSize;  //最小为这个
                    break;
                }
                mTextPaint.setTextSize(trySize);
            }
            setTextSize(px2sp(getContext(), trySize));
        }
    }

    //大小改变的时候
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        if (h != oldh) {
            refitText(this.getText().toString(), h);
        }
    }

    /**
     * 将px值转换为sp值,保证文字大小不变
     */
    public static float px2sp(Context context, float pxValue) {
        float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (pxValue / fontScale);
    }
}
2、根据宽度自适应
public class FitTextView extends TextView{
     private Paint mTextPaint;
        private float mMaxTextSize; // 获取当前所设置文字大小作为最大文字大小
        private float mMinTextSize = 3;

        public FitTextView(Context context) {
            this(context, null);
        }

        public FitTextView(Context context, AttributeSet attrs) {
            super(context, attrs);
            setGravity(getGravity() | Gravity.CENTER_VERTICAL); // 默认水平居中
            setLines(1);
            initialise();
        }

        @Override
        protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
            refitText(text.toString(), this.getWidth());
            super.onTextChanged(text, start, lengthBefore, lengthAfter);
        }

        private void initialise() {
            mTextPaint = new TextPaint();
            mTextPaint.set(this.getPaint());
            // 最大的大小默认为特定的文本大小,除非它太小了
            mMaxTextSize = this.getTextSize();
//          mMinTextSize = 8;
        }

        @Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh) {
            if (w != oldw) {
                refitText(this.getText().toString(), w);
            }
        }

        /**
         * Resize the font so the specified text fits in the text box
         * assuming the text box is the specified width.
         *    
         */
        private void refitText(String text, int textWidth) {
            if (textWidth > 0) {
                int availableWidth = textWidth - this.getPaddingLeft() - this.getPaddingRight();
                float trySize = mMaxTextSize;

                mTextPaint.setTextSize(trySize);
                while (mTextPaint.measureText(text) > availableWidth) {

                    trySize -= 1;
                    if (trySize <= mMinTextSize) {
                        trySize = mMinTextSize;
                        break;
                    }
                    mTextPaint.setTextSize(trySize);
                }

                // setTextSize参数值为sp值
                setTextSize(px2sp(getContext(), trySize));
            }
        }

        /**
         * 将px值转换为sp值,保证文字大小不变
         */
        public static float px2sp(Context context, float pxValue) {
            float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
            return (pxValue / fontScale);
        }

}
3、使用时的注意事项

在布局中使用的话,注意按照你最大的设备来设置字体大小,这样在小设备上回自动缩放

<自定义控件位置.FitHeightTextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="设置"
                            android:textSize="最大设备的字体大小"/>

备注:因为EditText继承TextView,以上方法,同样适用。

原文地址:http://m.blog.csdn.net/qq_27489007/article/details/78063346
再次感谢!

相关文章

网友评论

      本文标题:(转)Android自定义TextView自适应字体大小

      本文链接:https://www.haomeiwen.com/subject/elxwbxtx.html