Auto Scale TextView Text to Fit within Bounds

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

    Since I've been looking for this forever, and I found a solution a while ago which is missing here, I'm gonna write it here, for future reference also.

    Note: this code was taken directly from Google Android Lollipop dialer a while back, I don't remember If changes were made at the time. Also, I don't know which license is this under, but I have reason to think it is Apache 2.0.

    Class ResizeTextView, the actual View

    public class ResizeTextView extends TextView {
    
    private final int mOriginalTextSize;
    private final int mMinTextSize;
    private final static int sMinSize = 20;
    public ResizeTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mOriginalTextSize = (int) getTextSize();
        mMinTextSize = (int) sMinSize;
    }
    @Override
    protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
        super.onTextChanged(text, start, lengthBefore, lengthAfter);
        ViewUtil.resizeText(this, mOriginalTextSize, mMinTextSize);
    }
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        ViewUtil.resizeText(this, mOriginalTextSize, mMinTextSize);
    }
    

    This ResizeTextView class could extend TextView and all its children as I undestand, so EditText as well.

    Class ViewUtil with method resizeText(...)

    /*
    * Copyright (C) 2012 The Android Open Source Project
    *
    * Licensed under the Apache License, Version 2.0 (the "License");
    * you may not use this file except in compliance with the License.
    * You may obtain a copy of the License at
    *
    *      http://www.apache.org/licenses/LICENSE-2.0
    *
    * Unless required by applicable law or agreed to in writing, software
    * distributed under the License is distributed on an "AS IS" BASIS,
    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    * See the License for the specific language governing permissions and
    * limitations under the License.
    */
    
    import android.graphics.Paint;
    import android.util.TypedValue;
    import android.widget.TextView;
    
    public class ViewUtil {
    
        private ViewUtil() {}
    
        public static void resizeText(TextView textView, int originalTextSize, int minTextSize) {
            final Paint paint = textView.getPaint();
            final int width = textView.getWidth();
            if (width == 0) return;
            textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, originalTextSize);
            float ratio = width / paint.measureText(textView.getText().toString());
            if (ratio <= 1.0f) {
                textView.setTextSize(TypedValue.COMPLEX_UNIT_PX,
                        Math.max(minTextSize, originalTextSize * ratio));
            }
        }
    }
    

    You should set your view as

    
    

    Hope it helps!

提交回复
热议问题