如何缩放/调整文本以适合 TextView? [英] How to scale/resize text to fit a TextView?
问题描述
我正在尝试创建一种方法来调整 TextView
中多行文本的大小,使其适合 TextView的边界(X 和 Y 尺寸)代码>.
I'm trying to create a method for resizing multi-line text in a TextView
such that it fits within the bounds (both the X and Y dimensions) of the TextView
.
目前,我有一些东西,但它所做的只是调整文本大小,使文本的第一个字母/字符填充 TextView
的尺寸(即只有第一个字母可见,而且很大).我需要它来适应 TextView 范围内的所有文本行.
At present, I have something, but all it does is resize the text such that just the first letter/character of the text fills the dimensions of the TextView
(i.e. only the first letter is viewable, and it's huge). I need it to fit all the lines of the text within the bounds of the TextView.
这是我目前所拥有的:
public static void autoScaleTextViewTextToHeight(TextView tv)
{
final float initSize = tv.getTextSize();
//get the width of the view's back image (unscaled)....
float minViewHeight;
if(tv.getBackground()!=null)
{
minViewHeight = tv.getBackground().getIntrinsicHeight();
}
else
{
minViewHeight = 10f;//some min.
}
final float maxViewHeight = tv.getHeight() - (tv.getPaddingBottom()+tv.getPaddingTop())-12;// -12 just to be sure
final String s = tv.getText().toString();
//System.out.println(""+tv.getPaddingTop()+"/"+tv.getPaddingBottom());
if(minViewHeight >0 && maxViewHeight >2)
{
Rect currentBounds = new Rect();
tv.getPaint().getTextBounds(s, 0, s.length(), currentBounds);
//System.out.println(""+initSize);
//System.out.println(""+maxViewHeight);
//System.out.println(""+(currentBounds.height()));
float resultingSize = 1;
while(currentBounds.height() < maxViewHeight)
{
resultingSize ++;
tv.setTextSize(resultingSize);
tv.getPaint().getTextBounds(s, 0, s.length(), currentBounds);
//System.out.println(""+(currentBounds.height()+tv.getPaddingBottom()+tv.getPaddingTop()));
//System.out.println("Resulting: "+resultingSize);
}
if(currentBounds.height()>=maxViewHeight)
{
//just to be sure, reduce the value
tv.setTextSize(resultingSize-1);
}
}
}
我认为问题在于 tv.getPaint().getTextBounds(...)
的使用.它总是返回文本边界的小数字...相对于 tv.getWidth()
和 tv.getHeight()
值较小...即使文本大小远大于TextView
的宽度或高度.
I think the problem is in the use of tv.getPaint().getTextBounds(...)
. It always returns small numbers for the text bounds... small relative to the tv.getWidth()
and tv.getHeight()
values... even if the text size is far larger than the width or height of the TextView
.
推荐答案
MavenCentral 的 AutofitTextView 库很好地处理了这个问题.源代码托管在 Github(1k+ 星)上,位于 https://github.com/grantland/android-autofittextview
The AutofitTextView library from MavenCentral handles this nicely. The source hosted on Github(1k+ stars) at https://github.com/grantland/android-autofittextview
将以下内容添加到您的 app/build.gradle
Add the following to your app/build.gradle
repositories {
mavenCentral()
}
dependencies {
implementation 'me.grantland:autofittextview:0.2.+'
}
在代码中启用任何扩展 TextView 的视图:
Enable any View extending TextView in code:
AutofitHelper.create(textView);
启用任何在 XML 中扩展 TextView 的视图:
Enable any View extending TextView in XML:
<me.grantland.widget.AutofitLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
/>
</me.grantland.widget.AutofitLayout>
在代码或 XML 中使用内置的 Widget:
Use the built in Widget in code or XML:
<me.grantland.widget.AutofitTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
/>
这篇关于如何缩放/调整文本以适合 TextView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!