Android 自动适应字体大小的EditText

Android 自动适应字体大小的EditText

Android 中 EditText 单行显示时,如果字体过多,自动缩小字体,在字号小于指定字号时,显示"..." ,具体请参考如下代码:


package com.zepp.newsports.ui.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.text.TextPaint;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.widget.TextView;
import com.zepp.newsports.R;
import com.zepp.z3a.common.util.FontUtil;

/**
 * Created by QY on 15/7/20.
 */
public class AutoStretchTextView extends TextView {
    private float DEFAULT_MIN_TEXTSIZE = 28;
    private String TAG = AutoStretchTextView.class.getSimpleName();
    private Rect viewRect;
    private int textColor = Color.LTGRAY;
    private boolean centerHorization;
    private boolean alignLeft;

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

    public AutoStretchTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }

    public AutoStretchTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs);
    }

    @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.getSize(heightMeasureSpec));
    }

    @Override protected void onDraw(Canvas canvas) {
        int layoutWidth = getWidth();
        double charWidth = 0;
        int height = getHeight();
        TextPaint paint = getPaint();
        String text = getText().toString();
        viewRect = new Rect();

        float drawedWidth = paint.measureText(text);
        char[] textCharArray = text.toCharArray();
        if (textCharArray.length > 0) {
            paint.getTextBounds(textCharArray, 0, textCharArray.length, viewRect);
            charWidth = paint.measureText(textCharArray, textCharArray.length - 1, 1);
        }

        while (layoutWidth - drawedWidth < charWidth) {
            if (getTextSize()  <= DEFAULT_MIN_TEXTSIZE) {
                StringBuilder builder = new StringBuilder();
                for (char chars : textCharArray) {
                    builder.append(chars);
                    float visibleTextWidth = paint.measureText(builder.toString() + "...");
                    if (layoutWidth - visibleTextWidth < 1.5 * charWidth) {
                        text = builder.append("...").toString();
                        char[] chars1 = text.toCharArray();
                        paint.getTextBounds(chars1, 0, chars1.length, viewRect);
                        break;
                    }
                } break;
            }
            paint.setTextSize(getTextSize() - 1);
            drawedWidth = paint.measureText(text);
            if(textCharArray.length > 0)
                charWidth = paint.measureText(textCharArray, textCharArray.length - 1, 1);
        }


        float startY = height -(height- viewRect.height()) * 0.5f;
        paint.setColor(textColor);
        if (centerHorization) {
            canvas.drawText(text, Math.abs(layoutWidth - viewRect.width()) / 2 , startY, paint);
        } else if (alignLeft) {
            canvas.drawText(text, 5, startY, paint);
        } else {
            int startX = layoutWidth - viewRect.width();
            if (startX < 0)
                startX = 0;
            canvas.drawText(text, startX, startY, paint);
        }
    }

    private void init(AttributeSet attrs) {
        if (!isInEditMode()) {

            String resource = attrs.getAttributeValue("http://schemas.android.com/apk/res/android", "textColor");
            if (!TextUtils.isEmpty(resource)) {
                if (resource.contains("@")) {
                    textColor = getContext().getResources().getColor(Integer.valueOf(resource.substring(1, resource.length())));
                } else {
                    textColor = Color.parseColor(resource);
                }
            }

            TypedArray typedArray =
                    getContext().obtainStyledAttributes(attrs, R.styleable.AutoStretchTextView);
            DEFAULT_MIN_TEXTSIZE =
                    typedArray.getDimension(R.styleable.AutoStretchTextView_default_min_textsize,
                            DEFAULT_MIN_TEXTSIZE);
            typedArray.recycle();
            int font = attrs.getAttributeIntValue("http://schemas.android.com/apk/res-auto", "font",
                    0);
            getPaint().setTypeface(FontUtil.getInstance().getFontTypeface(getContext(), font));

            centerHorization =
                    attrs.getAttributeBooleanValue("http://schemas.android.com/apk/res-auto",
                            "centerHorization", false);
            alignLeft =
                    attrs.getAttributeBooleanValue("http://schemas.android.com/apk/res-auto",
                            "alignLeft", false);
        }
    }


}

在 attr.xml中设置自定义的属性,用于设置最小字体,绘制字体是居中或巨左,默认是居右绘制的


<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="AutoStretchTextView">
        <attr name="default_min_textsize" format="dimension"/>
        <attr name="centerHorization" format="boolean"/>
        <attr name="alignLeft" format="boolean"/>
    </declare-styleable>

</resources>


  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值