টেক্সটভিউয়ের সাথে মানানসই কিভাবে পাঠ্য ফন্টের আকার সমন্বয় করবেন


178

অ্যান্ড্রয়েডের যে জায়গাটি দখল করে আছে তার জায়গাটি ফিট করার জন্য পাঠ্যদর্শনটিতে পাঠ্যকে সামঞ্জস্য করার কোনও উপায় আছে কি?

যেমন আমি একটি ব্যবহার করছি TableLayoutএবং TextViewপ্রতিটি সারিতে বেশ কয়েকটি গুলি যুক্ত করছি । যেহেতু আমি চাই নাTextView যে পাঠ্যগুলি মোড়ানো হোক না কেন আমি বরং দেখতে পাচ্ছি এটি সামগ্রীটির ফন্টের আকার হ্রাস করে।

কোন ধারনা?

আমি চেষ্টা করেছি measureText, কিন্তু যেহেতু আমি কলামটির আকার জানি না এটি ব্যবহার করা অসুবিধাজনক বলে মনে হচ্ছে। এটি কোড যেখানে আমি ফন্টের আকারকে কিছু ফিট করতে চাই

TableRow row = new TableRow(this);   
for (int i=0; i < ColumnNames.length; i++) {    
    TextView textColumn = new TextView(this);      
    textColumn.setText(ColumnNames[i]);
    textColumn.setPadding(0, 0, 1, 0);
    textColumn.setTextColor(getResources().getColor(R.drawable.text_default));          
    row.addView(textColumn, new TableRow.LayoutParams()); 
} 
table.addView(row, new TableLayout.LayoutParams());  

এখানে ডুনির কোডের ভিত্তিতে আমার সমাধান পরীক্ষা করুন stackoverflow.com/questions/5033012/… দ্রষ্টব্য: আমি এটিকে একটি লুপ দিয়ে বাস্তবায়ন করি নি। পিএস: ধন্যবাদ ডুনি
বনাম

উত্তর:


129

নীচের সমাধানটি এখানে সমস্ত পরামর্শ অন্তর্ভুক্ত করে। এটি মূলত যা ডান্নি পোস্ট করেছিলেন তা দিয়ে শুরু হয়। এটি gjpc এর মতো বাইনারি অনুসন্ধান ব্যবহার করে তবে এটি কিছুটা বেশি পঠনযোগ্য। এটিতে গ্রেগমের বাগ ফিক্স এবং আমার নিজের একটি বাগ-ফিক্সও অন্তর্ভুক্ত রয়েছে।

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;
}

9
সমস্ত প্রতিক্রিয়া একত্রিত করার জন্য ধন্যবাদ। আমি দেখতে পাচ্ছি যে সমাধানটি কেবল বিবেচনায় প্রস্থ নেয় । আমার সমস্যা হ'ল শখ উচ্চতা অতিক্রম করে।
অ্যালিকেলজিন-কিলাকা

3
ওহ, এটি রানটাইমের সময় আংশিকভাবে কাজ করবে বলে মনে হচ্ছে। ডিভাইস ওটি এমুলেটরটিতে, পাঠ্যটি অর্ধেক করে কেটে দেওয়া হয়। ইক্লিপস লেআউট এডিটরটিতে এটি দুর্দান্ত দেখাচ্ছে। কোন ধারনা?
AlikElzin-kilaka

1
এই পোস্টে বলে মনে হচ্ছে (হাই জন্য / দেখ কাস্টম XML বৈশিষ্ট্যাবলী যোগ করার) কৌতুক করতে হবে: stackoverflow.com/a/8090772/156611
এড Sinek

7
এটি ছিল দুর্দান্ত সমাধান! যদি অন্য কেউ অ্যান্ড্রয়েড উন্নয়নে নতুন এবং বেশ কিভাবে XML- এর একটি বর্ধিত দৃশ্য বাস্তবায়ন জানে না, এটা দেখে মনে হচ্ছে এই: <com.example.zengame1.FontFitTextView android:paddingTop="5dip" android:id="@+id/childs_name" android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1" android:layout_gravity="center" android:textSize="@dimen/text_size"/>
ক্যাসি মারে

2
মহান কাজ! এটি বোতামগুলির সাথেও কাজ করছে। পাঠ্যদর্শন উচ্চতার যত্ন নিতে সবেমাত্র সেট করা হয়েছেfloat hi = this.getHeight() - this.getPaddingBottom() - this.getPaddingTop();
অ্যালেক্সগুটি

28

আমি একটি ক্লাস লিখেছি যা টেক্সটভিউ প্রসারিত এবং এটি করে। আপনার পরামর্শ অনুসারে এটি কেবল পরিমাপের পাঠ্য ব্যবহার করে। মূলত এটির সর্বাধিক পাঠ্য আকার এবং ন্যূনতম পাঠ্য আকার রয়েছে (যা পরিবর্তন করা যেতে পারে) এবং এটি কেবলমাত্র হ্রাসের মধ্যে তাদের মধ্যে আকারগুলির মধ্য দিয়ে চলতে থাকে যতক্ষণ না এটি সবচেয়ে বড় আকারের উপযুক্ত হবে। বিশেষভাবে মার্জিত নয়, তবে আমি অন্য কোনও উপায় জানি না।

কোডটি এখানে:

import android.content.Context;
import android.graphics.Paint;
import android.util.AttributeSet;
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() {
        testPaint = new Paint();
        testPaint.set(this.getPaint());
        //max size defaults to the intially specified text size unless it is too small
        maxTextSize = this.getTextSize();
        if (maxTextSize < 11) {
            maxTextSize = 20;
        }
        minTextSize = 10;
    }

    /* 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) {
            int availableWidth = textWidth - this.getPaddingLeft() - this.getPaddingRight();
            float trySize = maxTextSize;

            testPaint.setTextSize(trySize);
            while ((trySize > minTextSize) && (testPaint.measureText(text) > availableWidth)) {
                trySize -= 1;
                if (trySize <= minTextSize) {
                    trySize = minTextSize;
                    break;
                }
                testPaint.setTextSize(trySize);
            }

            this.setTextSize(trySize);
        }
    }

    @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);
        }
    }

    //Getters and Setters
    public float getMinTextSize() {
        return minTextSize;
    }

    public void setMinTextSize(int minTextSize) {
        this.minTextSize = minTextSize;
    }

    public float getMaxTextSize() {
        return maxTextSize;
    }

    public void setMaxTextSize(int minTextSize) {
        this.maxTextSize = minTextSize;
    }

    //Attributes
    private Paint testPaint;
    private float minTextSize;
    private float maxTextSize;

}

11
একটি বাইনারি অনুসন্ধান সাধারণত রৈখিক অনুসন্ধানের চেয়ে দ্রুততর হয়।
টেড হপ

আমি মনে করি "অনসাইজ চেঞ্জড" এর পরিবর্তে "অনলআউট" এ রিফিটটেক্সট করা আরও যুক্তিসঙ্গত হবে
জ্যাকি

আপনি কি দয়া করে আমার সমস্যার স্ট্যাকওভারফ্লো.com
মুহাম্মাদ

17

এটি স্পিড প্লেনেরFontFitTextView , তবে পাঠ্যটিকে ফিট করার প্রয়োজন হলে এটি কেবল হরফের আকার হ্রাস করে এবং এর ফন্টের আকারটি অন্যথায় রাখে। এটি উচ্চতার সাথে ফিট করার জন্য হরফের আকার বাড়ায় না।

public class FontFitTextView extends TextView {

    // Attributes
    private Paint mTestPaint;
    private float defaultTextSize;

    public FontFitTextView(Context context) {
        super(context);
        initialize();
    }

    public FontFitTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initialize();
    }

    private void initialize() {
        mTestPaint = new Paint();
        mTestPaint.set(this.getPaint());
        defaultTextSize = getTextSize();
    }

    /* 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 || text.isEmpty())
            return;

        int targetWidth = textWidth - this.getPaddingLeft() - this.getPaddingRight();

        // this is most likely a non-relevant call
        if( targetWidth<=2 )
            return;

        // text already fits with the xml-defined font size?
        mTestPaint.set(this.getPaint());
        mTestPaint.setTextSize(defaultTextSize);
        if(mTestPaint.measureText(text) <= targetWidth) {
            this.setTextSize(TypedValue.COMPLEX_UNIT_PX, defaultTextSize);
            return;
        }

        // adjust text size using binary search for efficiency
        float hi = defaultTextSize;
        float lo = 2;
        final float threshold = 0.5f; // How close we have to be
        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 || h != oldh) {
            refitText(this.getText().toString(), w);
        }
    }

}

এটি কীভাবে এক্সএমএলে ব্যবহৃত হতে পারে তার একটি উদাহরণ এখানে দেওয়া হয়েছে:

<com.your.package.activity.widget.FontFitTextView
    android:id="@+id/my_id"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="My Text"
    android:textSize="60sp" />

পাঠ্যটির প্রস্থে যতক্ষণ ফিট হয় ততক্ষণ এটি ফন্টের আকার 60sp রাখবে। যদি পাঠ্য দীর্ঘ হয় তবে এটি ফন্টের আকার হ্রাস পাবে। এই ক্ষেত্রে, TextViewএর উচ্চতা এছাড়াও কারণে পরিবর্তন হবেheight=wrap_content

যদি আপনি কোনও বাগ খুঁজে পান তবে সম্পাদনা করতে দ্বিধা বোধ করুন।


আপনি এটি কীভাবে ব্যবহার করেছেন? আমি আমার উত্তরে এটি কীভাবে ব্যবহার করি তার একটি উদাহরণ যুক্ত করেছি। আমি এটি বিভিন্ন অ্যান্ড্রয়েড সংস্করণ, অনুকরণকারী এবং এডিটি গ্রাফিকাল লেআউট সম্পাদক দিয়ে পরীক্ষা করেছি।
সুলাই

মূলত, আমি একটি নির্দিষ্ট উচ্চতা উল্লেখ করছিলাম না। আপনার onMeasure ভিউটি চাইলে এটি দখল করার অনুমতি দিচ্ছে। আমি একটি সমাধান নিয়ে আসতে this.setMeasuredDimension(parentWidth, height);
পেরেছি

1
গ্রেট ইনপুট, আমি কোডটি :) এখন এটা সঙ্গে কাজ করে সংশোধন match_parentএবং wrap_content
সোলাই

আপনি খালি স্ট্রিং লেখার জন্য কেন পরীক্ষা করছেন কেন "s.isEmpty () এর পরিবর্তে" "সমমান (গুলি) ?? অথবা s.leth () == 0? বুঝতে পারি না কেন আমি মাঝে মাঝে এই সমতা পরীক্ষা দেখতে পারি।
জোর্ডিড

1
@ প্রকটিক বুটানি এটি উল্লেখ করার জন্য ধন্যবাদ thanks সমতুল্য text.isEmpty()হবে "".equals(text)
সোলাই

14

এখানে আমার সমাধানটি যা এমুলেটর এবং ফোনগুলিতে কাজ করে তবে এক্লিপস লেআউট সম্পাদকটিতে খুব ভাল নয়। এটি কিলাকার কোড থেকে অনুপ্রাণিত হয়েছে তবে টেক্সটটির আকার পেইন্ট থেকে প্রাপ্ত হয় না তবে পাঠ্যদর্শনটি নিজেই কল করেmeasure(0, 0)

জাভা ক্লাস:

public class FontFitTextView extends TextView
{
    private static final float THRESHOLD = 0.5f;

    private enum Mode { Width, Height, Both, None }

    private int minTextSize = 1;
    private int maxTextSize = 1000;

    private Mode mode = Mode.None;
    private boolean inComputation;
    private int widthMeasureSpec;
    private int heightMeasureSpec;

    public FontFitTextView(Context context) {
            super(context);
    }

    public FontFitTextView(Context context, AttributeSet attrs) {
            this(context, attrs, 0);
    }

    public FontFitTextView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);

            TypedArray tAttrs = context.obtainStyledAttributes(attrs, R.styleable.FontFitTextView, defStyle, 0);
            maxTextSize = tAttrs.getDimensionPixelSize(R.styleable.FontFitTextView_maxTextSize, maxTextSize);
            minTextSize = tAttrs.getDimensionPixelSize(R.styleable.FontFitTextView_minTextSize, minTextSize);
            tAttrs.recycle();
    }

    private void resizeText() {
            if (getWidth() <= 0 || getHeight() <= 0)
                    return;
            if(mode == Mode.None)
                    return;

            final int targetWidth = getWidth();
            final int targetHeight = getHeight();

            inComputation = true;
            float higherSize = maxTextSize;
            float lowerSize = minTextSize;
            float textSize = getTextSize();
            while(higherSize - lowerSize > THRESHOLD) {
                    textSize = (higherSize + lowerSize) / 2;
                    if (isTooBig(textSize, targetWidth, targetHeight)) {
                            higherSize = textSize; 
                    } else {
                            lowerSize = textSize;
                    }
            }
            setTextSize(TypedValue.COMPLEX_UNIT_PX, lowerSize);
            measure(widthMeasureSpec, heightMeasureSpec);
            inComputation = false;
    }

    private boolean isTooBig(float textSize, int targetWidth, int targetHeight) {
            setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
            measure(0, 0);
            if(mode == Mode.Both)
                    return getMeasuredWidth() >= targetWidth || getMeasuredHeight() >= targetHeight;
            if(mode == Mode.Width)
                    return getMeasuredWidth() >= targetWidth;
            else
                    return getMeasuredHeight() >= targetHeight;
    }

    private Mode getMode(int widthMeasureSpec, int heightMeasureSpec) {
            int widthMode = MeasureSpec.getMode(widthMeasureSpec);
            int heightMode = MeasureSpec.getMode(heightMeasureSpec);
            if(widthMode == MeasureSpec.EXACTLY && heightMode == MeasureSpec.EXACTLY)
                    return Mode.Both;
            if(widthMode == MeasureSpec.EXACTLY)
                    return Mode.Width;
            if(heightMode == MeasureSpec.EXACTLY)
                    return Mode.Height;
            return Mode.None;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            if(!inComputation) {
                    this.widthMeasureSpec = widthMeasureSpec;
                    this.heightMeasureSpec = heightMeasureSpec;
                    mode = getMode(widthMeasureSpec, heightMeasureSpec);
                    resizeText();
            }
    }

    protected void onTextChanged(final CharSequence text, final int start, final int before, final int after) {
            resizeText();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
            if (w != oldw || h != oldh)
                    resizeText();
    }

    public int getMinTextSize() {
            return minTextSize;
    }

    public void setMinTextSize(int minTextSize) {
            this.minTextSize = minTextSize;
            resizeText();
    }

    public int getMaxTextSize() {
            return maxTextSize;
    }

    public void setMaxTextSize(int maxTextSize) {
            this.maxTextSize = maxTextSize;
            resizeText();
    }
}

এক্সএমএল বৈশিষ্ট্য ফাইল:

<resources>
    <declare-styleable name="FontFitTextView">
        <attr name="minTextSize" format="dimension" />
        <attr name="maxTextSize" format="dimension" />
    </declare-styleable>
</resources>

এই ক্লাসের সর্বশেষতম সংস্করণটির জন্য আমার গিথুবটি পরীক্ষা করুন । আমি আশা করি এটি কারও পক্ষে কার্যকর হতে পারে। যদি কোনও বাগ খুঁজে পাওয়া যায় বা কোডটির ব্যাখ্যা প্রয়োজন হয় তবে, গিথুব এ কোনও সমস্যা খোলার দ্বিধা বোধ করুন।


এটি আমার গ্যালাক্সি নেক্সাসে প্রিফেক্ট কাজ করে তবে ছোট স্ক্রিনযুক্ত ডিভাইসে আমার সমস্যা হয়।
টনি সেরালভা

ছোট স্ক্রিনযুক্ত ডিভাইসে আপনার সমস্যা কী?
yDelouis

দুঃখিত, সমস্যাটি ছোট পর্দার সাথে নয়, আপনার ভিউ অ্যান্ড্রয়েড 4.0.০, এমুলেটর অন্তর্ভুক্ত সমস্ত ডিভাইসে কাজ করছে না। আমি আপনার গিটহাবে নতুন সমস্যাটি খুলেছি
টনি সেরালভা

12

Https://stackoverflow.com/users/234270/speedplane আপনাকে অনেক ধন্যবাদ । দুর্দান্ত উত্তর!

এখানে তার প্রতিক্রিয়ার একটি উন্নত সংস্করণ যা উচ্চতার যত্নও নেয় এবং হরফের আকার সীমাবদ্ধ করতে একটি সর্বোচ্চফন্টসাইজ বৈশিষ্ট্য নিয়ে আসে (আমার ক্ষেত্রে এটি কার্যকর ছিল, তাই আমি এটি ভাগ করতে চেয়েছিলাম):

package com.<your_package>;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.TextView;


public class FontFitTextView extends TextView
{

    private Paint mTestPaint;
    private float maxFontSize;
    private static final float MAX_FONT_SIZE_DEFAULT_VALUE = 20f;

    public FontFitTextView(Context context)
    {
        super(context);
        initialise(context, null);
    }

    public FontFitTextView(Context context, AttributeSet attributeSet)
    {
        super(context, attributeSet);
        initialise(context, attributeSet);
    }

    public FontFitTextView(Context context, AttributeSet attributeSet, int defStyle)
    {
        super(context, attributeSet, defStyle);
        initialise(context, attributeSet);
    }

    private void initialise(Context context, AttributeSet attributeSet)
    {
        if(attributeSet!=null)
        {
            TypedArray styledAttributes = context.obtainStyledAttributes(attributeSet, R.styleable.FontFitTextView);
            maxFontSize = styledAttributes.getDimension(R.styleable.FontFitTextView_maxFontSize, MAX_FONT_SIZE_DEFAULT_VALUE);
            styledAttributes.recycle();
        }
        else
        {
            maxFontSize = MAX_FONT_SIZE_DEFAULT_VALUE;
        }

        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, int textHeight)
    {
        if (textWidth <= 0)
            return;
        int targetWidth = textWidth - this.getPaddingLeft() - this.getPaddingRight();
        int targetHeight = textHeight - this.getPaddingTop() - this.getPaddingBottom();
        float hi = maxFontSize;
        float lo = 2;
//      final float threshold = 0.5f; // How close we have to be
        final float threshold = 1f; // How close we have to be

        mTestPaint.set(this.getPaint());

        Rect bounds = new Rect();

        while ((hi - lo) > threshold)
        {
            float size = (hi + lo) / 2;
            mTestPaint.setTextSize(size);

            mTestPaint.getTextBounds(text, 0, text.length(), bounds);

            if (bounds.width() >= targetWidth || bounds.height() >= targetHeight)
                hi = size; // too big
            else
                lo = size; // too small

//          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, height);
        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(), this.getHeight());
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh)
    {
        if (w != oldw)
        {
            refitText(this.getText().toString(), w, h);
        }
    }
}

সংশ্লিষ্ট /res/values/attr.xML ফাইল:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="FontFitTextView">
        <attr name="maxFontSize" format="dimension" />
    </declare-styleable>

</resources>

উদাহরণ:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:res-auto="http://schemas.android.com/apk/res-auto"
    android:id="@+id/home_Layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    tools:ignore="ContentDescription" >
...

 <com.<your_package>.FontFitTextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:singleLine="true"
                    android:text="Sample Text"
                    android:textSize="28sp"
                    res-auto:maxFontSize="35sp"/>

...
</RelativeLayout>

নতুন maxFontSizeবৈশিষ্ট্যটি ব্যবহার করতে , xmlns:res-auto="http://schemas.android.com/apk/res-auto"উদাহরণ হিসাবে শো হিসাবে যুক্ত করতে ভুলবেন না ।


দুঃখিত, তবে এটি এখনও সব ক্ষেত্রে কার্যকর হয় না। আমি আরও মনে করি এটি মাল্টি লাইনগুলি সঠিকভাবে পরিচালনা করে না।
অ্যান্ড্রয়েড বিকাশকারী

ওহ .. আপনি আমাকে একটি মামলা দিতে পারেন যার জন্য এটি কাজ করে না? (একাধিক লাইন সমর্থিত নয়, আপনি ঠিক বলেছেন)
পাস্কাল

অনেক ক্ষেত্রে. আমি ঠিক এটি প্রমাণ করতে একটি এলোমেলো পরীক্ষক তৈরি করেছি। এখানে একটি নমুনা: দেখুন প্রস্থ: 317px, উচ্চতা: 137px, পাঠ্য: "Q7Lr"। আমি যা দেখছি তা হ'ল : tinypic.com/view.php?pic=2dv5yf9&s=6 । আমি যে নমুনা প্রকল্পটি করেছি তা এখানে: mega.co.nz/… । আমি মনে করি যে এই জাতীয় দৃষ্টিভঙ্গিতে বহু লাইন পরিচালনা করা উচিত, কোনও আকারের ফন্টকে সমর্থন করা উচিত, উচ্চতা হ্যান্ডেল করা উচিত এবং প্রস্থটি নয়, ... দুঃখের বিষয়, আমি যে নমুনাগুলি খুঁজে পেয়েছি তার কোনওটিই ভাল কাজ করতে পারেনি।
অ্যান্ড্রয়েড বিকাশকারী

ঠিক আছে, অবশ্যই একটি সম্পূর্ণ সমাধান নয়। দুঃখিত। আপাতত এটির উন্নতি করার কোনও সময় নেই .. আপনি যদি এটি উন্নতি করতে পারেন তবে আপনার সমাধান পোস্ট করতে দ্বিধা করবেন না।
পাস্কাল

1
আমি একটি নতুন থ্রেড তৈরি করেছি যা আমার পরীক্ষাগুলি দেখায়, এই আশায় যে কেউ একটি ভাল সমাধান করতে সক্ষম হবে: stackoverflow.com/questions/16017165/…
অ্যান্ড্রয়েড বিকাশকারী

7

আমি একই সমস্যা ছিল এবং আমার জন্য কাজ করে বলে মনে হচ্ছে এমন একটি ক্লাস লিখেছিলাম। মূলত, আমি একটি পৃথক ক্যানভাসে পাঠ্য আঁকার জন্য একটি স্থির বিন্যাস ব্যবহার করেছি এবং যতক্ষণ না আমি ফিট হবার কোনও ফন্টের আকার খুঁজে পাই। আপনি নীচের বিষয়ে পোস্ট করা ক্লাসটি দেখতে পারেন। আমি আসা করি এটা সাহায্য করবে.

বাউন্ডের মধ্যে ফিট করার জন্য অটো স্কেল টেক্সটভিউ পাঠ্য


6

আপনি এখন এটি তৃতীয় পক্ষের লাইব্রেরি বা উইজেট ছাড়াই করতে পারেন। এটি এপিআই লেভেলের 26 এ টেক্সটভিউতে অন্তর্নির্মিত । এটি android:autoSizeTextType="uniform"আপনার যুক্ত করুন TextViewএবং এর উচ্চতা নির্ধারণ করুন। এখানেই শেষ.

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

<?xml version="1.0" encoding="utf-8"?>
<TextView
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:autoSizeTextType="uniform" />

আপনি TextViewCompatসামঞ্জস্যের জন্যও ব্যবহার করতে পারেন ।


5

OnMeasure এ সামান্য পরিবর্তন:

@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);
}

এবং রিফিটটেক্সটে বাইনারি অনুসন্ধান:

private void refitText(String text, int textWidth) 
{ 
    if (textWidth > 0) 
    {
        int availableWidth = textWidth - this.getPaddingLeft() - this.getPaddingRight();         
        int trySize = (int)maxTextSize;
        int increment = ~( trySize - (int)minTextSize ) / 2;

        testPaint.setTextSize(trySize);
        while ((trySize > minTextSize) && (testPaint.measureText(text) > availableWidth)) 
        {
            trySize += increment;
            increment = ( increment == 0 ) ? -1 : ~increment / 2;
            if (trySize <= minTextSize) 
            {
                trySize = (int)minTextSize;
                break;
            }
            testPaint.setTextSize(trySize);
        }

        this.setTextSize( TypedValue.COMPLEX_UNIT_PX, trySize);
    }
}

3
বাইনারি অনুসন্ধানের পরিবর্তে একটি সাধারণ স্কেলিং বেশ ভালভাবে কাজ করে: trySize *= availableWidth / measured_width(তারপরে মিনিটেক্সট সাইজে ক্ল্যাম্পড)।
টেড হপ

5

আমার পক্ষে নিখুঁতভাবে কাজ করার জন্য আমি নিম্নলিখিতগুলি পেয়েছি। এটি লুপ করে না এবং উচ্চতা এবং প্রস্থ উভয়ের জন্য অ্যাকাউন্ট করে। মনে রাখবেন যে ভিউতে সেটটেক্সট সাইজ কল করার সময় পিএক্স ইউনিট নির্দিষ্ট করা গুরুত্বপূর্ণ। এর জন্য আগের পোস্টে টিপকে ধন্যবাদ!

Paint paint = adjustTextSize(getPaint(), numChars, maxWidth, maxHeight);
setTextSize(TypedValue.COMPLEX_UNIT_PX,paint.getTextSize());

আমি যে রুটিনটি ব্যবহার করি তা এখানে ভিউ পেন্ট () ভিউ থেকে পাস করে। একটি 'প্রশস্ত' অক্ষরযুক্ত একটি 10 ​​অক্ষরের স্ট্রিং প্রকৃত স্ট্রিং থেকে প্রস্থটি পৃথক করে অনুমান করতে ব্যবহৃত হয়।

private static final String text10="OOOOOOOOOO";
public static Paint adjustTextSize(Paint paint, int numCharacters, int widthPixels, int heightPixels) {
    float width = paint.measureText(text10)*numCharacters/text10.length();
    float newSize = (int)((widthPixels/width)*paint.getTextSize());
    paint.setTextSize(newSize);

    // remeasure with font size near our desired result
    width = paint.measureText(text10)*numCharacters/text10.length();
    newSize = (int)((widthPixels/width)*paint.getTextSize());
    paint.setTextSize(newSize);

    // Check height constraints
    FontMetricsInt metrics = paint.getFontMetricsInt();
    float textHeight = metrics.descent-metrics.ascent;
    if (textHeight > heightPixels) {
        newSize = (int)(newSize * (heightPixels/textHeight));
        paint.setTextSize(newSize);
    }

    return paint;
}

আপনি কীভাবে এটি একটি লেআউট এক্সএমএলে অন্তর্ভুক্ত করবেন?
অ্যালিকেলজিন-কিলাকা

এই কোডটি একটি সীমিত আকারের অঞ্চলে বিদ্যমান ভিউয়ের মধ্যে পাঠ্য স্থাপনের জন্য দরকারী বা আপনি টেক্সটভিউ থেকে আপনার নিজস্ব উত্পন্ন শ্রেণি তৈরি করতে পারেন এবং অন্যান্য পোস্টগুলিতে দেখানো অনুসারে ওভারমাইজারকে ওভাররাইড করতে পারেন। নিজেই এটি কোনও বিন্যাসে ব্যবহার করা যায় না।
গ্লেন

নিশ্চিত হয়ে নিন যে ভিউটি আঁকার পরে আপনি এর মাত্রা পরীক্ষা করেছেন check অনক্রিট () এর সময় এটি করা খুব তাড়াতাড়ি। সঠিক পরিমাপগুলি সঠিক সময়ে নেওয়া হয়েছে তা নিশ্চিত করার জন্য আমি ভিউ ট্রিউবার্সার ব্যবহার করেছি।
স্কট বিগস ২:01

4

পরিবর্তন সঙ্গে কাজ করে

আপনাকে পাঠ্য প্রদর্শনের আকারটি এ জাতীয়ভাবে সেট করতে হবে কারণ অন্যথায় সেটটেক্সট সাইজ ধরে নেয় মান এসপি ইউনিটে রয়েছে:

setTextSize(TypedValue.COMPLEX_UNIT_PX, trySize);

এবং আপনার স্পষ্টভাবে এই কোডটি যুক্ত করা দরকার।

@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);
}

4

app:autoSizeTextType="uniform"পশ্চাদপদ সামঞ্জস্যতার জন্য ব্যবহার করুন কারণ android:autoSizeTextType="uniform"কেবলমাত্র API স্তরের 26 এবং উচ্চতর ক্ষেত্রে কাজ করে।


2

আমি উপরের ডুনি সমাধানের একটি প্রকরণ ব্যবহার করেছি, তবে সেই নির্দিষ্ট কোডটি আমার পক্ষে কার্যকর হয়নি। বিশেষত, যখন পেইন্ট অবজেক্টটি ভিউর পেইন্ট অবজেক্টের বৈশিষ্ট্য রয়েছে এবং তারপরে পরিমাপ পাঠ্য () কল করার সময় এটি ব্যবহার করার চেষ্টা করার সময় এটি সরাসরি ভিউর পেইন্ট অবজেক্টকে কল করার মতো মান দেয় না। আমার মতামত সেট আপ করার পদ্ধতিতে সম্ভবত কিছু পার্থক্য রয়েছে যা আচরণকে আলাদা করে তোলে।

আমার সমাধানটি ছিল সরাসরি ভিউর পেইন্টটি ব্যবহার করা, যদিও একাধিকবার দেখার জন্য ফন্টের আকার পরিবর্তন করতে কিছু পারফরম্যান্স জরিমানা থাকতে পারে।


2

আমি স্পিড প্লেন থেকে দুর্দান্ত সমাধানটি উন্নত করার জন্য কাজ করছি এবং এটি নিয়ে এসেছি। এটি মার্জিনটি নির্ধারণ সহ উচ্চতা পরিচালনা করে যাতে পাঠ্যটি সঠিকভাবে উল্লম্বভাবে কেন্দ্রীভূত করা উচিত।

এটি প্রস্থটি পেতে একই ফাংশনটি ব্যবহার করে, যেমনটি এটি সবচেয়ে ভাল কাজ করে বলে মনে হয় তবে উচ্চতা কোথাও সরবরাহ করা হয় না বলে এটি উচ্চতা পেতে আলাদা ফাংশন ব্যবহার করে। কিছু সংশোধন করা দরকার যেগুলি করা দরকার, তবে আমি এটি করার জন্য একটি উপায় খুঁজে পেয়েছি, যখন চোখকে খুশী করে দেখছি।

import android.content.Context;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.TextView;

public class FontFitTextView extends TextView {

    public FontFitTextView(Context context) {
        super(context);
        initialize();
    }

    public FontFitTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initialize();
    }

    private void initialize() {
        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,int textHeight) 
    { 
        if (textWidth <= 0)
            return;
        int targetWidth = textWidth - this.getPaddingLeft() - this.getPaddingRight();
        int targetHeight = textHeight - this.getPaddingTop() - this.getPaddingBottom();
        float hi = Math.min(targetHeight,100);
        float lo = 2;
        final float threshold = 0.5f; // How close we have to be

        Rect bounds = new Rect();

        mTestPaint.set(this.getPaint());

        while((hi - lo) > threshold) {
            float size = (hi+lo)/2;
            mTestPaint.setTextSize(size);
            mTestPaint.getTextBounds(text, 0, text.length(), bounds);
            if((mTestPaint.measureText(text)) >= targetWidth || (1+(2*(size+(float)bounds.top)-bounds.bottom)) >=targetHeight) 
                hi = size; // too big
            else
                lo = size; // too small
        }
        // Use lo so that we undershoot rather than overshoot
        this.setTextSize(TypedValue.COMPLEX_UNIT_PX,(float) lo);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
        int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
        int height = getMeasuredHeight();
        refitText(this.getText().toString(), parentWidth,height);
        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(),this.getHeight());
    }

    @Override
    protected void onSizeChanged (int w, int h, int oldw, int oldh) {

        if (w != oldw) {
            refitText(this.getText().toString(), w,h);
        }
    }

    //Attributes
    private Paint mTestPaint;
}


1

আমি এই লাইব্রেরিটি না পাওয়া পর্যন্ত আমার প্রকল্পগুলিতে দীর্ঘ সময় ধরে আমার এই ব্যথা ছিল:

compile 'me.grantland:autofittextview:0.2.+'

আপনার প্রয়োজন অনুসারে আপনাকে কেবল এক্সএমএল যুক্ত করতে হবে এবং এটি হয়ে গেছে। উদাহরণ স্বরূপ:

<me.grantland.widget.AutofitTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:maxLines="2"
android:textSize="40sp"
autofit:minTextSize="16sp"
/>

0

পূর্ববর্তী পোস্টারগুলি দ্বারা অনুপ্রাণিত হয়ে আমি আমার সমাধানটি ভাগ করতে চেয়েছিলাম। এটি স্কেল ফ্যাক্টরের সাথে কাজ করে যা উপলভ্য জায়গার ফিট করার জন্য পূর্ববর্তী ফন্টের আকারে প্রয়োগ করা হয়। টেক্সটভিউ অনড্র পদ্ধতিতে অপ্রত্যাশিত আচরণ রোধ ছাড়াও, এটি কেবল নিজেরাই টেক্সট আঁকবে।

public class FontFitTextView extends TextView {

    // How much of the available space should be used in percent.
    private static final float MARGINHEIGHT = 0.8f;
    private static final float MARGINWIDTH = 0.8f;

    private Paint paint;
    private int viewWidth;
    private int viewHeight;
    private float textHeight;
    private float textWidth;

    public FontFitTextView(Context c) {
        this(c, null);
    }

    public FontFitTextView(Context c, AttributeSet attrs) {
        super(c, attrs);
        initComponent();
    }

    // Default constructor override
    public FontFitTextView(Context c, AttributeSet attrs, int defStyle) {
        super(c, attrs, defStyle);
        initComponent();
    }

    private void initComponent() {
        paint = new Paint();
        paint.setTextSize(30);
        paint.setTextAlign(Align.CENTER);
        paint.setAntiAlias(true);
    }

    public void setFontColor(int c) {
        paint.setColor(c);
    }

    private void calcTextSize(String s, Canvas c) {

        float availableHeight = viewHeight;
        float availableWidth = viewWidth;

        // This value scales the old font up or down to match the available
        // space.
        float scale = 1.0f;

        // Rectangle for measuring the text dimensions
        Rect rect = new Rect();
        float oldFontSize = paint.getTextSize();

        // Calculate the space used with old font size
        paint.getTextBounds(s, 0, s.length(), rect);
        textWidth = rect.width();
        textHeight = rect.height();

        // find scale-value to fit the text horizontally
        float scaleWidth = 1f;
        if (textWidth > 0.0f) {
            scaleWidth = (availableWidth) / textWidth * MARGINWIDTH;
        }

        // find scale-value to fit the text vertically
        float scaleHeight = 1f;
        if (textHeight > 0.0f) {
            scaleHeight = (availableHeight) / textHeight * MARGINHEIGHT;
        }

        // We are always limited by the smaller one
        if (scaleWidth < scaleHeight) {
            scale = scaleWidth;
        } else {
            scale = scaleHeight;
        }

        // We apply the scale to the old font size to make it bigger or smaller
        float newFontSize = (oldFontSize * scale);
        paint.setTextSize(newFontSize);
    }

    /**
     * Calculates the origin on the Y-Axis (width) for the text in this view.
     * 
     * @return
     */
    private float calcStartDrawingPosX() {
        float left = getMeasuredWidth();
        float centerY = left - (viewWidth / 2);
        return centerY;
    }

    /**
     * Calculates the origin on the Y-Axis (height) for the text in this view.
     * 
     * @return
     */
    private float calcStartDrawingPosY() {
        float bottom = getMeasuredHeight();
        // The paint only centers horizontally, origin on the Y-Axis stays at
        // the bottom, thus we have to lift the origin additionally by the
        // height of the font.
        float centerX = bottom - (viewHeight / 2) + (textHeight / 2);
        return centerX;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        String text = getText().toString();
        if (text.length() > 0) {
            calcTextSize(text, canvas);
            canvas.drawText(text, calcStartDrawingPosX(),
                    calcStartDrawingPosY(), paint);
        }
    };

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        viewWidth = w;
        viewHeight = h;
        super.onSizeChanged(w, h, oldw, oldh);
    }
}

0
/* get your context */
Context c = getActivity().getApplicationContext();

LinearLayout l = new LinearLayout(c);
l.setOrientation(LinearLayout.VERTICAL);
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 0);

l.setLayoutParams(params);
l.setBackgroundResource(R.drawable.border);

TextView tv=new TextView(c);
tv.setText(" your text here");

/* set typeface if needed */
Typeface tf = Typeface.createFromAsset(c.getAssets(),"fonts/VERDANA.TTF");  
tv.setTypeface(tf);

// LayoutParams lp = new LayoutParams();

tv.setTextColor(Color.parseColor("#282828"));

tv.setGravity(Gravity.CENTER | Gravity.BOTTOM);
//  tv.setLayoutParams(lp);

tv.setTextSize(20);
l.addView(tv);

return l;

1
আপনার কোডটি কেবল পড়ে, আমি মনে করি না এটি কার্যকর হবে। মনে রাখবেন, ভিউটিতে ফিট করার জন্য প্রশ্নটি স্বয়ংক্রিয়ভাবে পাঠ্য দর্শনের ফন্টের আকারটি সামঞ্জস্য করার ছিল। আপনি একটি স্থির ফন্টের আকার সেট করেছেন।
সোলাই

0

এটি একটি সহজ সমাধান হওয়া উচিত:

public void correctWidth(TextView textView, int desiredWidth)
{
    Paint paint = new Paint();
    Rect bounds = new Rect();

    paint.setTypeface(textView.getTypeface());
    float textSize = textView.getTextSize();
    paint.setTextSize(textSize);
    String text = textView.getText().toString();
    paint.getTextBounds(text, 0, text.length(), bounds);

    while (bounds.width() > desiredWidth)
    {
        textSize--;
        paint.setTextSize(textSize);
        paint.getTextBounds(text, 0, text.length(), bounds);
    }

    textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
}

0

টেক্সটভিউ প্রসারিত করুন এবং নীচের কোডটি দিয়ে ওনরাইডকে ওভাররাইড করুন। এটি পাঠ্যের দিক অনুপাত রাখবে তবে স্থান পূরণের জন্য এটি আকার করবে। প্রয়োজনে আপনি প্রসারিত করার জন্য কোডটি সহজেই সংশোধন করতে পারেন।

  @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

আমি একটি সংক্ষিপ্ত সহায়ক শ্রেণি লিখেছি যা একটি পাঠ্যদর্শনকে একটি নির্দিষ্ট প্রস্থের মধ্যে ফিট করে এবং সর্বনিম্ন পাঠ্যদর্শনটি "..." যুক্ত করে যদি ন্যূনতম পাঠ্যক্রমটি অর্জন করা যায় না।

মনে রাখবেন যে এটি ফিট না হওয়া পর্যন্ত বা ন্যূনতম পাঠ্যের আকার না আসা পর্যন্ত এটি কেবল পাঠ্যকে ছোট করে তোলে। বড় আকারের সাথে পরীক্ষা করতে, সহায়তা পদ্ধতিটি কল করার আগে পাঠ্যকে বড় আকারে সেট করুন।

এটি পিক্সেল লাগে, সুতরাং আপনি যদি মাত্রা থেকে মান ব্যবহার করে থাকেন তবে আপনি এটিকে এটি কল করতে পারেন:


float minTextSizePx = getResources().getDimensionPixelSize(R.dimen.min_text_size);
float maxTextWidthPx = getResources().getDimensionPixelSize(R.dimen.max_text_width);
WidgetUtils.fitText(textView, text, minTextSizePx, maxTextWidthPx);

এটি আমি ব্যবহৃত ক্লাস:


public class WidgetUtils {

    public static void fitText(TextView textView, String text, float minTextSizePx, float maxWidthPx) {
        textView.setEllipsize(null);
        int size = (int)textView.getTextSize();
        while (true) {
            Rect bounds = new Rect();
            Paint textPaint = textView.getPaint();
            textPaint.getTextBounds(text, 0, text.length(), bounds);
            if(bounds.width() < maxWidthPx){
                break;
            }
            if (size <= minTextSizePx) {
                textView.setEllipsize(TextUtils.TruncateAt.END);
                break;
            }
            size -= 1;
            textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
        }
    }
}

এটি পাঠ্যের একাধিক লাইনকে সমর্থন করে?
রায়মসনসন

0

যদি সমস্ত ক্যাপগুলির মতো ট্রান্সফর্মেশন সেট করা থাকে, স্পিডপ্লেনের পদ্ধতির বগি। আমি এটি সংশোধন করেছি, নিম্নলিখিত কোডের ফলে (দুঃখিত, আমার খ্যাতি আমাকে এটিকে স্পিডপ্লেনের সমাধানে মন্তব্য হিসাবে যুক্ত করতে দেয় না):

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 (getTransformationMethod() != null) {
            text = getTransformationMethod().getTransformation(text, this).toString();
        }

        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;
            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

আমি জানি না এটি সঠিক উপায় বা এটির কাজটি বিটি নয় ... আপনার দৃষ্টিভঙ্গিটি দেখুন এবং অন্লো্লোবাল্যালোআউটলিস্টনার () দেখুন এবং টেক্সটভিউ লাইনক্যান্ট পেয়ে টেক্সট সাইজ সেট করুন।

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

এটি খুব সাধারণ কয়েকটি লাইন কোড ..

আমাদের সাইট ব্যবহার করে, আপনি স্বীকার করেছেন যে আপনি আমাদের কুকি নীতি এবং গোপনীয়তা নীতিটি পড়েছেন এবং বুঝতে পেরেছেন ।
Licensed under cc by-sa 3.0 with attribution required.