简体   繁体   中英

How to set the font size depending on the screen size

I added in my values the font size for each screen size for normal screen: <dimen name="fontsize">22sp</dimen> for small screen: <dimen name="fontsize">19sp</dimen> ...

but when I have two normal screen sizes one xhdpi and the other hdpi I had to add another value folder: for normal-xhdpi screen: <dimen name="fontsize">22sp</dimen> for normal-hdpi screen: <dimen name="fontsize">15sp</dimen>

but I know that the sp unit differs depending on the screen dpi why I had this error and how can I have the needed fontsize on all the screens?

What I want to know is:

sp Scale-independent Pixels - this is like the dp unit, but it is also scaled by the user's font size preference. It is recommend you use this unit when specifying font sizes, so they will be adjusted for both the screen density and user's preference.

So why isn't that working? I placed a folder of valuse for each screen size small, normal, large and xlarge and I thought that the sp will be changed depending on the dpi so no need to add the dpi.. but that's not working wy?

use this utillity class:

com.cutompackage;
    import android.content.Context;
    import android.graphics.Paint;
    import android.util.AttributeSet;
    import android.util.TypedValue;
    import android.widget.TextView;
    /**
     * Utillity class which is changing font size of text view in app in order to fit to given dimensions
     * @author Darko
     *
     */
    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
            if (this.getTextSize() > lo) {
                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;
    }

and then just set in xml like:

 <com.cutompackage.FontFitTextView
                android:id="@+id/headerText"
                android:layout_width="0dip"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:textColor="@color/text_color"
                android:textSize="22sp" />

EDIT: it will automatically change font size to fit given dimension (height and width) of TextView now you just need to set dimensions properly.

hope it helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM