package epson.print.Util;

import android.content.Context;
import android.graphics.Rect;
import android.text.TextPaint;
import android.util.AttributeSet;

public class FitSizeTextView extends androidx.appcompat.widget.AppCompatTextView {
    public FitSizeTextView(Context context) {
        super(context);
    }

    public FitSizeTextView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
    }

    private void refitText(String str, int i) {
        if (i > 0) {
            float paddingLeft = (float) ((i - getPaddingLeft()) - getPaddingRight());
            TextPaint paint = getPaint();
            paint.getTextBounds(str, 0, str.length(), new Rect());
            float textSize = paint.getTextSize();
            float measureText = paint.measureText(str);
            while (measureText >= paddingLeft) {
                textSize -= 1.0f;
                paint.setTextSize(textSize);
                measureText = paint.measureText(str);
            }
            setSingleLine();
        }
    }

    protected void onMeasure(int i, int i2) {
        super.onMeasure(i, i2);
        int size = MeasureSpec.getSize(i);
        int size2 = MeasureSpec.getSize(i2);
        refitText(getText().toString(), size);
        setMeasuredDimension(size, size2);
    }

    protected void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
        refitText(getText().toString(), getWidth());
    }

    protected void onSizeChanged(int i, int i2, int i3, int i4) {
        if (i != i3) {
            refitText(getText().toString(), i);
        }
    }
}
