How to adjust text font size to fit textview

后端 未结 22 1676
無奈伤痛
無奈伤痛 2020-11-22 05:15

Is there any way in android to adjust the textsize in a textview to fit the space it occupies?

E.g. I\'m using a TableLayout and adding several Te

相关标签:
22条回答
  • 2020-11-22 06:05

    I used a variation of Dunni solution above, but that particular code didn't work for me. In particular, when trying to use the Paint object set to have the traits of the view's Paint object, and then calling measureText(), it doesn't return the same value as directly calling the view's Paint object. Perhaps there are some differences in the way my views are set up that make the behavior different.

    My solution was to directly use the view's Paint, even though there might be some performance penalties in changing the font size for the view multiple times.

    0 讨论(0)
  • 2020-11-22 06:05

    Google already made this feature.

    <TextView
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:autoSizeTextType="uniform" />
    

    https://developer.android.com/guide/topics/ui/look-and-feel/autosizing-textview.html

    0 讨论(0)
  • 2020-11-22 06:08

    The solution below incorporates all of the suggestions here. It starts with what was originally posted by Dunni. It uses a binary search like gjpc's, but it is a bit more readable. It also include's gregm's bug fixes and a bug-fix of my own.

    import android.content.Context;
    import android.graphics.Paint;
    import android.util.AttributeSet;
    import android.util.TypedValue;
    import android.widget.TextView;
    
    public class FontFitTextView extends TextView {
    
        public FontFitTextView(Context context) {
            super(context);
            initialise();
        }
    
        public FontFitTextView(Context context, AttributeSet attrs) {
            super(context, attrs);
            initialise();
        }
    
        private void initialise() {
            mTestPaint = new Paint();
            mTestPaint.set(this.getPaint());
            //max size defaults to the initially specified text size unless it is too small
        }
    
        /* Re size 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)
                return;
            int targetWidth = textWidth - this.getPaddingLeft() - this.getPaddingRight();
            float hi = 100;
            float lo = 2;
            final float threshold = 0.5f; // How close we have to be
    
            mTestPaint.set(this.getPaint());
    
            while((hi - lo) > threshold) {
                float size = (hi+lo)/2;
                mTestPaint.setTextSize(size);
                if(mTestPaint.measureText(text) >= targetWidth) 
                    hi = size; // too big
                else
                    lo = size; // too small
            }
            // Use lo so that we undershoot rather than overshoot
            this.setTextSize(TypedValue.COMPLEX_UNIT_PX, lo);
        }
    
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
        {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
            int height = getMeasuredHeight();
            refitText(this.getText().toString(), parentWidth);
            this.setMeasuredDimension(parentWidth, height);
        }
    
        @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);
            }
        }
    
        //Attributes
        private Paint mTestPaint;
    }
    
    0 讨论(0)
  • 2020-11-22 06:08

    I don't known this is correct way or not bt its working ...take your view and check OnGlobalLayoutListener() and get textview linecount then set textSize.

     yourView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if (textView.getLineCount()>=3) {
                    textView.setTextSize(20);
                }else{
                    //add somthing
                  }
            }
        });
    

    Its very simple few line code..

    0 讨论(0)
提交回复
热议问题