Q
計算文本大小
22
A
回答
29
如果是空的,然後後面的文本需要你的大小以下可能幫助:
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();
編輯(後評論): 使用上述反向:
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
//這個方法可能會稍微調整一下,但是給你一個你可以做什麼的想法
26
這應該是一個簡單的解決方案:
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
這應該是解決方案。 – Claud 2014-06-04 03:07:35
2
我也有,以確保文本適合特定的盒子時,面臨同樣的問題。以下是表現最好的,最準確的解決方案,我目前有這方面:
/**
* 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));
}
}
這只是計算正確的大小,而不是運行的試驗/錯誤的測試。
您可以使用它像這樣:
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);
或其他方式,如果你想實現垃圾:
/**
* 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));
}
使用像這樣:
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;
}
相關問題
- 1. 計算文本大小與NSLayoutManager
- 2. 計算用GDI呈現的文本(文本矩形)的大小
- 3. Appwidget大小計算
- 4. C++計算大小
- 5. 浮點大小的計算成本
- 6. 計算文本字體大小來填充容器文字
- 7. 計算理論文本文件的大小
- 8. 根據文本大小計算文本框大小,但不包含實際的文本框
- 9. 計算擬合的文字大小
- 10. `find'如何計算文件的大小?
- 11. 下載前計算文件大小
- 12. 計算大小文件夾python
- 13. 計算響應的文件大小?
- 14. 如何計算音頻文件大小?
- 15. 計算文件大小(ASCII值)
- 16. 在PostgreSQL中計算文件大小
- 17. 計算和緩存文件夾大小
- 18. MongoDB如何計算文檔的大小?
- 19. 如何計算protobuf文件大小?
- 20. 計算shell中文件的大小
- 21. 計算硬盤大小
- 22. 計算數組的大小
- 23. 視頻大小計算
- 24. 計算數組大小
- 25. 計算元組的大小
- 26. AMR幀大小計算
- 27. 計算數組的大小
- 28. 位圖行大小計算
- 29. 計算字體大小
- 30. 計算目錄大小
這裏檢查這個答案http://stackoverflow.com/questions/7259016/scale-text-in-a-view-to-fit/7259136#7259136 – Ronnie 2012-05-06 15:02:08