2011-01-25 40 views
22

Tôi có văn bản sẽ được đặt thành TextView có chiều rộng được chỉ định. Nó cần phải tính toán kích thước văn bản để phù hợp với nó vào TextView.Tính kích thước văn bản theo chiều rộng của vùng văn bản

Nói cách khác: Có cách nào để vừa với văn bản vào vùng TextView, chẳng hạn như tính năng loại tỷ lệ của ImageView không?

+1

Kiểm tra câu trả lời ở đây http://stackoverflow.com/questions/7259016/scale-text-in-a-view-to-fit/7259136#7259136 – Ronnie

Trả lời

29

Nếu đó là kích thước của không gian văn bản mất sau thì sau đây có thể giúp:

Paint paint = new Paint(); 
Rect bounds = new Rect(); 

int text_height = 0; 
int text_width = 0; 

paint.setTypeface(Typeface.DEFAULT);// your preference here 
paint.setTextSize(25);// have this the same as your text size 

String text = "Some random text"; 

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

text_height = bounds.height(); 
text_width = bounds.width(); 

Chỉnh sửa (sau khi bình luận): Sử dụng trên theo hướng ngược lại:

int text_height = 50; 
int text_width = 200; 

int text_check_w = 0; 
int text_check_h = 0; 

int incr_text_size = 1; 
boolean found_desired_size = true; 

while (found_desired_size){ 
    paint.setTextSize(incr_text_size);// have this the same as your text size 

    String text = "Some random text"; 

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

    text_check_h = bounds.height(); 
    text_check_w = bounds.width(); 
    incr_text_size++; 

if (text_height == text_check_h && text_width == text_check_w){ 
found_desired_size = false; 
} 
} 
return incr_text_size; // this will be desired text size from bounds you already have 

// phương pháp này có thể được điều chỉnh một chút, nhưng cung cấp cho bạn ý tưởng về những gì bạn có thể làm

+2

Nó không phải là chính xác những gì tôi muốn. Tôi không cần tính chiều cao/chiều rộng của văn bản theo kích thước và phông chữ. Tôi cần phải tính toán kích thước văn bản theo chiều cao/chiều rộng và phông chữ. – kankan

+0

@kankan: didi bạn nhận được câu trả lời? – DKV

26

Đây phải là một giải pháp đơn giản:

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

Đây phải là giải pháp. – Claud

+0

Giải pháp tốt nhất và dễ dàng. Cảm ơn! –

2

Tôi cũng phải đối mặt với cùng một vấn đề khi phải đảm bảo văn bản phù hợp với một hộp cụ thể. Sau đây là giải pháp hiệu quả nhất và chính xác nhất mà tôi có cho nó tại thời điểm này:

/** 
* A paint that has utilities dealing with painting text. 
* @author <a href="maillto:nospam">Ben Barkay</a> 
* @version 10, Aug 2014 
*/ 
public class TextPaint extends android.text.TextPaint { 
    /** 
    * Constructs a new {@code TextPaint}. 
    */ 
    public TextPaint() { 
     super(); 
    } 

    /** 
    * Constructs a new {@code TextPaint} using the specified flags 
    * @param flags 
    */ 
    public TextPaint(int flags) { 
     super(flags); 
    } 

    /** 
    * Creates a new {@code TextPaint} copying the specified {@code source} state. 
    * @param source The source paint to copy state from. 
    */ 
    public TextPaint(Paint source) { 
     super(source); 
    } 

    // Some more utility methods... 

    /** 
    * Calibrates this paint's text-size to fit the specified text within the specified width. 
    * @param text  The text to calibrate for. 
    * @param boxWidth The width of the space in which the text has to fit. 
    */ 
    public void calibrateTextSize(String text, float boxWidth) { 
     calibrateTextSize(text, 0, Float.MAX_VALUE, boxWidth); 
    } 

    /** 
    * Calibrates this paint's text-size to fit the specified text within the specified width. 
    * @param text  The text to calibrate for. 
    * @param min  The minimum text size to use. 
    * @param max  The maximum text size to use. 
    * @param boxWidth The width of the space in which the text has to fit. 
    */ 
    public void calibrateTextSize(String text, float min, float max, float boxWidth) { 
     setTextSize(10); 
     setTextSize(Math.max(Math.min((boxWidth/measureText(text))*10, max), min)); 
    } 
} 

Điều này chỉ cần tính toán kích thước chính xác hơn là chạy thử nghiệm/kiểm tra lỗi.

Bạn có thể sử dụng nó như thế này:

float availableWidth = ...; // use your text view's width, or any other width. 
String text = "Hi there"; 
TextPaint paint = new TextPaint(Paint.ANTI_ALIAS_FLAG); 
paint.setTypeface(...); 
paint.calibrateTextSize(text, availableWidth); 

Hoặc cách khác, nếu bạn muốn cung cấp thùng rác:

/** 
* Calibrates this paint's text-size to fit the specified text within the specified width. 
* @param paint  The paint to calibrate. 
* @param text  The text to calibrate for. 
* @param min  The minimum text size to use. 
* @param max  The maximum text size to use. 
* @param boxWidth The width of the space in which the text has to fit. 
*/ 
public static void calibrateTextSize(Paint paint, String text, float min, float max, float boxWidth) { 
    paint.setTextSize(10); 
    paint.setTextSize(Math.max(Math.min((boxWidth/paint.measureText(text))*10, max), min)); 
} 

Sử dụng như sau:

float availableWidth = ...; // use your text view's width, or any other width. 
String text = "Hi there"; 
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); 
paint.setTypeface(...); 
calibrateTextSize(paint, text, 0, Float.MAX_VALUE, availableWidth); 
3
public static float getFitTextSize(TextPaint paint, float width, String text) { 
    float nowWidth = paint.measureText(text); 
    float newSize = (float) width/nowWidth * paint.getTextSize(); 
    return newSize; 
} 
+7

Vui lòng giải thích mã của bạn để người hỏi hiểu giải pháp của bạn. – SuperBiasedMan

+0

Đây phải là giải pháp được chấp nhận. Nhanh nhất và đơn giản nhất. – AJoe16

+0

Đơn giản và nhanh chóng – ucMedia

Các vấn đề liên quan