Auto Scale TextView Text to Fit within Bounds

后端 未结 30 2865
囚心锁ツ
囚心锁ツ 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:50

    UPDATE: Following code also fulfills the requirement of an ideal AutoScaleTextView as described here : Auto-fit TextView for Android and is marked as winner.

    UPDATE 2: Support of maxlines added, now works fine before API level 16.

    Update 3: Support for android:drawableLeft, android:drawableRight, android:drawableTop and android:drawableBottom tags added, thanks to MartinH's simple fix here.


    My requirements were little bit different. I needed an efficient way to adjust size because I was animating an integer from, may be 0 to ~4000 in TextView in 2 seconds and I wanted to adjust the size accordingly. My solution works bit differently. Here is what final result looks like:

    enter image description here

    and the code that produced it:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="16dp" >
    
        <com.vj.widgets.AutoResizeTextView
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:ellipsize="none"
            android:maxLines="2"
            android:text="Auto Resized Text, max 2 lines"
            android:textSize="100sp" /> <!-- maximum size -->
    
        <com.vj.widgets.AutoResizeTextView
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:ellipsize="none"
            android:gravity="center"
            android:maxLines="1"
            android:text="Auto Resized Text, max 1 line"
            android:textSize="100sp" /> <!-- maximum size -->
    
        <com.vj.widgets.AutoResizeTextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Auto Resized Text"
            android:textSize="500sp" /> <!-- maximum size -->
    
    </LinearLayout>
    

    And finally the java code:

    import android.annotation.TargetApi;
    import android.content.Context;
    import android.content.res.Resources;
    import android.graphics.RectF;
    import android.os.Build;
    import android.text.Layout.Alignment;
    import android.text.StaticLayout;
    import android.text.TextPaint;
    import android.util.AttributeSet;
    import android.util.SparseIntArray;
    import android.util.TypedValue;
    import android.widget.TextView;
    
    public class AutoResizeTextView extends TextView {
    private interface SizeTester {
        /**
         * 
         * @param suggestedSize
         *            Size of text to be tested
         * @param availableSpace
         *            available space in which text must fit
         * @return an integer < 0 if after applying {@code suggestedSize} to
         *         text, it takes less space than {@code availableSpace}, > 0
         *         otherwise
         */
        public int onTestSize(int suggestedSize, RectF availableSpace);
    }
    
    private RectF mTextRect = new RectF();
    
    private RectF mAvailableSpaceRect;
    
    private SparseIntArray mTextCachedSizes;
    
    private TextPaint mPaint;
    
    private float mMaxTextSize;
    
    private float mSpacingMult = 1.0f;
    
    private float mSpacingAdd = 0.0f;
    
    private float mMinTextSize = 20;
    
    private int mWidthLimit;
    
    private static final int NO_LINE_LIMIT = -1;
    private int mMaxLines;
    
    private boolean mEnableSizeCache = true;
    private boolean mInitiallized;
    
    public AutoResizeTextView(Context context) {
        super(context);
        initialize();
    }
    
    public AutoResizeTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initialize();
    }
    
    public AutoResizeTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initialize();
    }
    
    private void initialize() {
        mPaint = new TextPaint(getPaint());
        mMaxTextSize = getTextSize();
        mAvailableSpaceRect = new RectF();
        mTextCachedSizes = new SparseIntArray();
        if (mMaxLines == 0) {
            // no value was assigned during construction
            mMaxLines = NO_LINE_LIMIT;
        }
        mInitiallized = true;
    }
    
    @Override
    public void setText(final CharSequence text, BufferType type) {
        super.setText(text, type);
        adjustTextSize(text.toString());
    }
    
    @Override
    public void setTextSize(float size) {
        mMaxTextSize = size;
        mTextCachedSizes.clear();
        adjustTextSize(getText().toString());
    }
    
    @Override
    public void setMaxLines(int maxlines) {
        super.setMaxLines(maxlines);
        mMaxLines = maxlines;
        reAdjust();
    }
    
    public int getMaxLines() {
        return mMaxLines;
    }
    
    @Override
    public void setSingleLine() {
        super.setSingleLine();
        mMaxLines = 1;
        reAdjust();
    }
    
    @Override
    public void setSingleLine(boolean singleLine) {
        super.setSingleLine(singleLine);
        if (singleLine) {
            mMaxLines = 1;
        } else {
            mMaxLines = NO_LINE_LIMIT;
        }
        reAdjust();
    }
    
    @Override
    public void setLines(int lines) {
        super.setLines(lines);
        mMaxLines = lines;
        reAdjust();
    }
    
    @Override
    public void setTextSize(int unit, float size) {
        Context c = getContext();
        Resources r;
    
        if (c == null)
            r = Resources.getSystem();
        else
            r = c.getResources();
        mMaxTextSize = TypedValue.applyDimension(unit, size,
                r.getDisplayMetrics());
        mTextCachedSizes.clear();
        adjustTextSize(getText().toString());
    }
    
    @Override
    public void setLineSpacing(float add, float mult) {
        super.setLineSpacing(add, mult);
        mSpacingMult = mult;
        mSpacingAdd = add;
    }
    
    /**
     * Set the lower text size limit and invalidate the view
     * 
     * @param minTextSize
     */
    public void setMinTextSize(float minTextSize) {
        mMinTextSize = minTextSize;
        reAdjust();
    }
    
    private void reAdjust() {
        adjustTextSize(getText().toString());
    }
    
    private void adjustTextSize(String string) {
        if (!mInitiallized) {
            return;
        }
        int startSize = (int) mMinTextSize;
        int heightLimit = getMeasuredHeight() - getCompoundPaddingBottom()
            - getCompoundPaddingTop();
        mWidthLimit = getMeasuredWidth() - getCompoundPaddingLeft()
            - getCompoundPaddingRight();
        mAvailableSpaceRect.right = mWidthLimit;
        mAvailableSpaceRect.bottom = heightLimit;
        super.setTextSize(
                TypedValue.COMPLEX_UNIT_PX,
                efficientTextSizeSearch(startSize, (int) mMaxTextSize,
                        mSizeTester, mAvailableSpaceRect));
    }
    
    private final SizeTester mSizeTester = new SizeTester() {
        @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
        @Override
        public int onTestSize(int suggestedSize, RectF availableSPace) {
            mPaint.setTextSize(suggestedSize);
            String text = getText().toString();
            boolean singleline = getMaxLines() == 1;
            if (singleline) {
                mTextRect.bottom = mPaint.getFontSpacing();
                mTextRect.right = mPaint.measureText(text);
            } else {
                StaticLayout layout = new StaticLayout(text, mPaint,
                        mWidthLimit, Alignment.ALIGN_NORMAL, mSpacingMult,
                        mSpacingAdd, true);
                // return early if we have more lines
                if (getMaxLines() != NO_LINE_LIMIT
                        && layout.getLineCount() > getMaxLines()) {
                    return 1;
                }
                mTextRect.bottom = layout.getHeight();
                int maxWidth = -1;
                for (int i = 0; i < layout.getLineCount(); i++) {
                    if (maxWidth < layout.getLineWidth(i)) {
                        maxWidth = (int) layout.getLineWidth(i);
                    }
                }
                mTextRect.right = maxWidth;
            }
    
            mTextRect.offsetTo(0, 0);
            if (availableSPace.contains(mTextRect)) {
                // may be too small, don't worry we will find the best match
                return -1;
            } else {
                // too big
                return 1;
            }
        }
    };
    
    /**
     * Enables or disables size caching, enabling it will improve performance
     * where you are animating a value inside TextView. This stores the font
     * size against getText().length() Be careful though while enabling it as 0
     * takes more space than 1 on some fonts and so on.
     * 
     * @param enable
     *            enable font size caching
     */
    public void enableSizeCache(boolean enable) {
        mEnableSizeCache = enable;
        mTextCachedSizes.clear();
        adjustTextSize(getText().toString());
    }
    
    private int efficientTextSizeSearch(int start, int end,
            SizeTester sizeTester, RectF availableSpace) {
        if (!mEnableSizeCache) {
            return binarySearch(start, end, sizeTester, availableSpace);
        }
        String text = getText().toString();
        int key = text == null ? 0 : text.length();
        int size = mTextCachedSizes.get(key);
        if (size != 0) {
            return size;
        }
        size = binarySearch(start, end, sizeTester, availableSpace);
        mTextCachedSizes.put(key, size);
        return size;
    }
    
    private static int binarySearch(int start, int end, SizeTester sizeTester,
            RectF availableSpace) {
        int lastBest = start;
        int lo = start;
        int hi = end - 1;
        int mid = 0;
        while (lo <= hi) {
            mid = (lo + hi) >>> 1;
            int midValCmp = sizeTester.onTestSize(mid, availableSpace);
            if (midValCmp < 0) {
                lastBest = lo;
                lo = mid + 1;
            } else if (midValCmp > 0) {
                hi = mid - 1;
                lastBest = hi;
            } else {
                return mid;
            }
        }
        // make sure to return last best
        // this is what should always be returned
        return lastBest;
    
    }
    
    @Override
    protected void onTextChanged(final CharSequence text, final int start,
            final int before, final int after) {
        super.onTextChanged(text, start, before, after);
        reAdjust();
    }
    
    @Override
    protected void onSizeChanged(int width, int height, int oldwidth,
            int oldheight) {
        mTextCachedSizes.clear();
        super.onSizeChanged(width, height, oldwidth, oldheight);
        if (width != oldwidth || height != oldheight) {
            reAdjust();
        }
    }
    }
    
    0 讨论(0)
  • 2020-11-21 05:50

    This solutions works for us:

    public class CustomFontButtonTextFit extends CustomFontButton
    {
        private final float DECREMENT_FACTOR = .1f;
    
        public CustomFontButtonTextFit(Context context) {
            super(context);
        }
    
        public CustomFontButtonTextFit(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public CustomFontButtonTextFit(Context context, AttributeSet attrs,
                int defStyle) {
            super(context, attrs, defStyle);
        }
    
        private synchronized 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();
    
                while(size > availableWidth)
                {
                    setTextSize( getTextSize() - DECREMENT_FACTOR );
                    tp = getPaint();
    
                    tp.getTextBounds(text, 0, text.length(), rect);
                    size = rect.width();
                }
            }
        }
    
        @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);
    
            if(parentWidth < getSuggestedMinimumWidth())
                parentWidth = getSuggestedMinimumWidth();
    
            if(parentHeight < getSuggestedMinimumHeight())
                parentHeight = getSuggestedMinimumHeight();
    
            this.setMeasuredDimension(parentWidth, parentHeight);
        }
    
        @Override
        protected void onTextChanged(final CharSequence text, final int start,
                final int before, final int after) 
        {
            super.onTextChanged(text, start, before, after);
    
            refitText(text.toString(), this.getWidth());
        }
    
        @Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh)
        {
            super.onSizeChanged(w, h, oldw, oldh);
    
            if (w != oldw) 
                refitText(this.getText().toString(), w);
        }
    }
    
    0 讨论(0)
  • 2020-11-21 05:50

    Extend TextView and override onDraw with the code below. It will keep text aspect ratio but size it to fill the space. You could easily modify code to stretch if necessary.

      @Override
      protected void onDraw(@NonNull Canvas canvas) {
        TextPaint textPaint = getPaint();
        textPaint.setColor(getCurrentTextColor());
        textPaint.setTextAlign(Paint.Align.CENTER);
        textPaint.drawableState = getDrawableState();
    
        String text = getText().toString();
        float desiredWidth = getMeasuredWidth() - getPaddingLeft() - getPaddingRight() - 2;
        float desiredHeight = getMeasuredHeight() - getPaddingTop() - getPaddingBottom() - 2;
        float textSize = textPaint.getTextSize();
    
        for (int i = 0; i < 10; i++) {
          textPaint.getTextBounds(text, 0, text.length(), rect);
          float width = rect.width();
          float height = rect.height();
    
          float deltaWidth = width - desiredWidth;
          float deltaHeight = height - desiredHeight;
    
          boolean fitsWidth = deltaWidth <= 0;
          boolean fitsHeight = deltaHeight <= 0;
    
          if ((fitsWidth && Math.abs(deltaHeight) < 1.0)
              || (fitsHeight && Math.abs(deltaWidth) < 1.0)) {
            // close enough
            break;
          }
    
          float adjustX = desiredWidth / width;
          float adjustY = desiredHeight / height;
    
          textSize = textSize * (adjustY < adjustX ? adjustY : adjustX);
    
          // adjust text size
          textPaint.setTextSize(textSize);
        }
        float x = desiredWidth / 2f;
        float y = desiredHeight / 2f - rect.top - rect.height() / 2f;
        canvas.drawText(text, x, y, textPaint);
      }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-21 05:52

    Here's yet another solution, just for kicks. It's probably not very efficient, but it does cope with both height and width of the text, and with marked-up text.

    @Override
    protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec)
    {
        if ((MeasureSpec.getMode(widthMeasureSpec) != MeasureSpec.UNSPECIFIED)
                && (MeasureSpec.getMode(heightMeasureSpec) != MeasureSpec.UNSPECIFIED)) {
    
            final float desiredWidth = MeasureSpec.getSize(widthMeasureSpec);
            final float desiredHeight = MeasureSpec.getSize(heightMeasureSpec);
    
            float textSize = getTextSize();
            float lastScale = Float.NEGATIVE_INFINITY;
            while (textSize > MINIMUM_AUTO_TEXT_SIZE_PX) {
                // Measure how big the textview would like to be with the current text size.
                super.onMeasure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
    
                // Calculate how much we'd need to scale it to fit the desired size, and
                // apply that scaling to the text size as an estimate of what we need.
                final float widthScale = desiredWidth / getMeasuredWidth();
                final float heightScale = desiredHeight / getMeasuredHeight();
                final float scale = Math.min(widthScale, heightScale);
    
                // If we don't need to shrink the text, or we don't seem to be converging, we're done.
                if ((scale >= 1f) || (scale <= lastScale)) {
                    break;
                }
    
                // Shrink the text size and keep trying.
                textSize = Math.max((float) Math.floor(scale * textSize), MINIMUM_AUTO_TEXT_SIZE_PX);
                setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
                lastScale = scale;
            }
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
    
    0 讨论(0)
  • 2020-11-21 05:53

    You can use the android.text.StaticLayout class for this. That's what TextView uses internally.

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