I have a text that should be set to TextView with specified width. It needs to calculate the text size to fit it into TextView.
我有一个文本应该设置为具有指定宽度的TextView。它需要计算文本大小以使其适合TextView。
In other words: Is there a way to fit text into TextView area, like the ImageView scale type feature?
换句话说:有没有办法将文本放入TextView区域,比如ImageView缩放类型功能?
4 个解决方案
#1
29
If it is the Size of the space the Text takes your after then the following might help:
如果它是文本占用你的空间的大小,那么以下可能会有所帮助:
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();
Edit (after comment): Use the above in reverse:
编辑(评论后):反过来使用上面的内容:
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
//this method may be tweaked a bit, but gives you an idea on what you can do
//这个方法可能会稍微调整一下,但会让你知道你能做些什么
#2
28
This should be a simple solution:
这应该是一个简单的解决方案:
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);
}
#3
8
public static float getFitTextSize(TextPaint paint, float width, String text) {
float nowWidth = paint.measureText(text);
float newSize = (float) width / nowWidth * paint.getTextSize();
return newSize;
}
#4
2
I also had to face the same problem when having to ensure that text fits into a specific box. The following is the best performing and most accurate solution that I have for it at the moment:
当必须确保文本适合特定的框时,我还必须面对同样的问题。以下是我目前最好的,最准确的解决方案:
/**
* 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));
}
}
This simply calculates the correct size rather than running a trial/error test.
这只是计算正确的大小而不是运行试验/错误测试。
You can use it like this:
你可以像这样使用它:
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);
Or otherwise, if you like to deliver garbage:
或者,如果你想提供垃圾:
/**
* 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));
}
Use like so:
使用如下:
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);
#1
29
If it is the Size of the space the Text takes your after then the following might help:
如果它是文本占用你的空间的大小,那么以下可能会有所帮助:
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();
Edit (after comment): Use the above in reverse:
编辑(评论后):反过来使用上面的内容:
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
//this method may be tweaked a bit, but gives you an idea on what you can do
//这个方法可能会稍微调整一下,但会让你知道你能做些什么
#2
28
This should be a simple solution:
这应该是一个简单的解决方案:
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);
}
#3
8
public static float getFitTextSize(TextPaint paint, float width, String text) {
float nowWidth = paint.measureText(text);
float newSize = (float) width / nowWidth * paint.getTextSize();
return newSize;
}
#4
2
I also had to face the same problem when having to ensure that text fits into a specific box. The following is the best performing and most accurate solution that I have for it at the moment:
当必须确保文本适合特定的框时,我还必须面对同样的问题。以下是我目前最好的,最准确的解决方案:
/**
* 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));
}
}
This simply calculates the correct size rather than running a trial/error test.
这只是计算正确的大小而不是运行试验/错误测试。
You can use it like this:
你可以像这样使用它:
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);
Or otherwise, if you like to deliver garbage:
或者,如果你想提供垃圾:
/**
* 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));
}
Use like so:
使用如下:
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);