Auto Scale TextView Text to Fit within Bounds

后端 未结 30 3446
囚心锁ツ
囚心锁ツ 2020-11-21 05:49

I\'m looking for an optimal way to resize wrapping text in a TextView so that it will fit within its getHeight and getWidth bounds. I\'m not simply looking for

30条回答
  •  滥情空心
    2020-11-21 05:51

    I hope this helps you

    import android.content.Context;
    import android.graphics.Rect;
    import android.text.TextPaint;
    import android.util.AttributeSet;
    import android.widget.TextView;
    
    /* Based on 
     * from http://stackoverflow.com/questions/2617266/how-to-adjust-text-font-size-to-fit-textview
     */
    public class FontFitTextView extends TextView {
    
    private static float MAX_TEXT_SIZE = 20;
    
    public FontFitTextView(Context context) {
        this(context, null);
    }
    
    public FontFitTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    
        float size = this.getTextSize();
        if (size > MAX_TEXT_SIZE)
            setTextSize(MAX_TEXT_SIZE);
    }
    
    private void refitText(String text, int textWidth) {
        if (textWidth > 0) {
            float availableWidth = textWidth - this.getPaddingLeft()
                    - this.getPaddingRight();
    
            TextPaint tp = getPaint();
            Rect rect = new Rect();
            tp.getTextBounds(text, 0, text.length(), rect);
            float size = rect.width();
    
            if (size > availableWidth)
                setTextScaleX(availableWidth / size);
        }
    }
    
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
        int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
        refitText(this.getText().toString(), parentWidth);
        this.setMeasuredDimension(parentWidth, parentHeight);
    }
    
    @Override
    protected void onTextChanged(final CharSequence text, final int start,
            final int before, final int after) {
        refitText(text.toString(), this.getWidth());
    }
    
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        if (w != oldw) {
            refitText(this.getText().toString(), w);
        }
    }
    }
    

    NOTE: I use MAX_TEXT_SIZE in case of text size is bigger than 20 because I don't want to allow big fonts applies to my View, if this is not your case, you can just simply remove it.

提交回复
热议问题