简体   繁体   English

在TextView中缩放textSize

[英]Scaling textSize in a TextView

Say I have a TextView of a particular size (doesn't really matter what... fill_parent, 20dip, whatever). 假设我有一个特定大小的TextView (真正重要的是什么...... fill_parent,20dip,等等)。 Is it possible to tell the text to shrink/grow in size to fit the available space without doing a lot of math? 是否有可能告诉文本缩小/增大尺寸以适应可用空间而无需进行大量数学计算?

Well, it's a little simpler than "a lot of complex maths", but there's a hack solution I use for this that's workable if the width of your text isn't too far off from the width of your textview. 嗯,它比“很多复杂的数学”简单一点,但是如果文本的宽度与文本视图的宽度相差太远,那么我使用的黑客解决方案是可行的。

    // HACK to adjust text width
    final int INITIAL_TEXTSIZE = 15;
    final int TEXTVIEW_WIDTH = textviewBackground.getIntrinsicWidth(); // I use the background image to find the width, but you can use a fixed value or whatever other method you prefer.
    final String text = ...;
    textView.setText( text );

    int size = INITIAL_TEXTSIZE;
    while( textview.getPaint().measureText(text) > TEXTVIEW_WIDTH )
        textview.setTextSize( --size );

If the width of your text can be significantly different than the width of your TextView, it might be better to employ a binary search instead of a linear search to find the right font size. 如果文本的宽度可能与TextView的宽度明显不同,则最好采用二分搜索而不是线性搜索来查找正确的字体大小。

Not saying this is an ideal solution, but it's an expedient one if you don't often need to adjust your textsize. 不是说这是一个理想的解决方案,但如果你不经常需要调整你的文本大小,这是一个权宜之计。 If you need to frequently adjust your textsize, it might be better to do some complex maths. 如果你需要经常调整你的文本大小,那么做一些复杂的数学可能会更好。

I tried out almost all of the above and most failed including the top one. 我尝试了几乎所有上述内容,其中大多数都失败了,其中包括前一名。 The simplest and most effective I found was from emmby but found that you had to set the initial textsize. 我发现最简单和最有效的是来自emmby,但发现你必须设置初始的文本大小。 Here is an improvement on his where you don't need to specify any textsize at all: 以下是对他不需要指定任何文本大小的改进:

  final String text = "HELP";

  TextView tvFlash = (TextView) findViewById(R.id.tvFlash);

  DisplayMetrics metrics = getResources().getDisplayMetrics();
  int width = metrics.widthPixels;
  int height = metrics.heightPixels;

  tvFlash.setText(text);
  tvFlash.setTextSize(TypedValue.COMPLEX_UNIT_PX, 1);

  int size = 1; 

  do
  {
    float textWidth = tvFlash.getPaint().measureText(text);

    if (textWidth < width)
      tvFlash.setTextSize(++size);
    else
    {
      tvFlash.setTextSize(--size);
      break;
    }
  }while(true);

U can calculate the idea font size value for a TextView. 你可以计算TextView的想法字体大小值。 That is the font size that will fill it without being too big. 这是填充它而不是太大的字体大小。 The formula is: 公式是:

fontSize = (TextView Height) * (width DPI / height DPI) / (Screen Width * Screen Height);

The width and height DPI can be acquired by Overriding the onConfigurationChanged method in your activity, and using newConfig.screenWidthDp and newConfig.screenHeightDp. 可以通过覆盖活动中的onConfigurationChanged方法并使用newConfig.screenWidthDp和newConfig.screenHeightDp来获取宽度和高度DPI。

The screen size in pixels can be acquired using the following helper function 可以使用以下辅助函数获取像素的屏幕大小

// Get Screen Size
static public Point getScreenSize(Activity aActivity)
{
    Display display = aActivity.getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    return size;
}

I just calculate this value as needed (in onConfigurationChanged) and push it to some static helper class. 我只是根据需要计算这个值(在onConfigurationChanged中)并将其推送到一些静态助手类。 Then when a TextView needs to determine what font size to use, it pulls it out of this class. 然后,当TextView需要确定要使用的字体大小时,它会将其拉出此类。

It works, but might not be the simplest solution. 它有效,但可能不是最简单的解决方案。

不是我知道的,抱歉。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM