Auto Scale TextView Text to Fit within Bounds

后端 未结 30 2809
囚心锁ツ
囚心锁ツ 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: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);
    }
    

提交回复
热议问题