如何自动调整上多行文本大小的TextView根据视图的最大尺寸是多少? [英] How to auto-adjust text size on a multi-line TextView according to the view max dimensions?

查看:221
本文介绍了如何自动调整上多行文本大小的TextView根据视图的最大尺寸是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找汽车的一种方式适合一个TextView在文本。通过我的搜索,我发现了很多解决方案,如:

I've been searching for a way of auto fit a text inside a textview. Through my search I've found many solutions like:

  • <一个href="http://stackoverflow.com/questions/2617266/how-to-adjust-text-font-size-to-fit-textview">FontFitTextView
  • <一个href="http://stackoverflow.com/questions/5033012/auto-scale-textview-text-to-fit-within-bounds?lq=1">AutoResizeTextView
  • <一个href="http://stackoverflow.com/questions/2596452/how-to-scale-resize-text-to-fit-a-textview?lq=1">AutoScale/Resize
  • FontFitTextView
  • AutoResizeTextView
  • AutoScale/Resize

不过,和许多人一样,这些并没有解决我的问题。 当我们用一个TextView与多行如预期,他们不工作。

But like many others these doesn't solve my problem. They don't work as expected when we use a TextView with multiline.

基本上我的目标是这个:

Basically my goal is this one:

正如你所看到的,文本的大小调整的基础上的宽度,高度,也注重换行,创造了多行的TextView。也可以改变字体。

As you can see, the text resizes based on width, height and also pays attention to the line break, creating a multi-line textview. Also be able to change the typeface.

我的一个思路来解决,这是这样的:

One of my ideas to solve this was something like this:

int size = CONSTANT_MAX_SIZE;
TextView tv = (TextView) findViewById(R.id.textview1)
while(Math.abs(tv.getMeasuredHeight()) >= TEXTVIEW_MAX_HEIGHT) {
    size--;
    tv.setTextSize(size);
    tv.measure(MeasureSpec.UNSPECIFIED,MeasureSpec.UNSPECIFIED);
    i++;
}

CONSTANT_MAX_SIZE是一个常数,定义字体的最大大小(以一个TextView TEXTSIZE礼)

CONSTANT_MAX_SIZE is a constant that defines the max size of the font (textsize propriety in a TextView)

TEXTVIEW_MAX_HEIGHT是一个常数,它定义了最大规模的TextView的也多了。

TEXTVIEW_MAX_HEIGHT is a constant that defines the max size that the textview can have.

这被称为每一个在TextView的文本被改变的时间。

This was called every time the text in the textview was changed.

TextView的XML是这样的:

The textview xml was something like this:

<TextView
     android:id="@+id/textview1"
     android:layout_width="200dp"
     android:layout_height="wrap_content"
     android:singleLine="false"
     android:inputType="textMultiLine"
     android:text=""
     android:textSize="150sp" />

自的宽度将在XML是有限的,因为需要调节它在需要时将机器人自动创建多要考虑的视图的唯一的高度。

Since the width would be limited in the XML, only the height of the view needs to be considered since adjusting it android would automatically create a multiline when needed.

尽管这是一个潜在的解决方案是不正常使用(远非如此),它不支持调整下来(当你删除文本)。

Although this is a potential solution isn't working perfectly (far from it) and it doesn't support resize down (when you delete text).

任何sugestions和/或想法?

Any sugestions and/or ideas?

推荐答案

虽然我等了一个可能的解决这个问题,我一直在尝试,并试图找到答案。

While I waited for a possible solution to this question, I have been experimenting and trying to figure it out.

最接近的解决这个问题是根据在从涂料的方法。

The closest solution to this problem was based in a method from paint.

基本上画有一个名为breaktext方法,该方法:

Basically paint has a method called 'breaktext' which:

公众诠释breakText(CharSequence的文字,诠释开始,诠释年底,布尔   measureForwards,浮了maxWidth,浮法[]是measuredWidth)

public int breakText (CharSequence text, int start, int end, boolean measureForwards, float maxWidth, float[] measuredWidth)

加在API级别1

测量文本,停止早期如果测量宽度超过   了maxWidth。返回测量该字符的数目,并且如果   measuredWidth可以不为空,测得的实际宽度返回了。

Measure the text, stopping early if the measured width exceeds maxWidth. Return the number of chars that were measured, and if measuredWidth is not null, return in it the actual width measured.

我再加上油漆getTextBounds其中:

I combine that with paint 'getTextBounds' which:

公共无效getTextBounds(字符串文本,诠释开始,诠释年底,矩形边界)

public void getTextBounds (String text, int start, int end, Rect bounds)

加在API级别1

返回的边界(由调用者分配的),在封闭所有的>字符,有一个隐含的起源的最小矩形(0,0)。

Return in bounds (allocated by the caller) the smallest rectangle that encloses all of the >characters, with an implied origin at (0,0).

所以,现在我能获得适合在一个给定的宽字符的数量和这些字符的高度。

So now I can obtain the number of chars that fit in a given width and the height of those chars.

使用了一段时间,你可以保持从要测量并获得行数串动删除字符(用时(指数&LT; string.length减)),并乘上了getTextBounds获得的高度

Using a while you can keep moving the removing chars from the string you want to measure and obtain the number of lines (by using a while(index < string.length)) and multiply that by the height obtained in the getTextBounds.

此外,你将不得不增加一个变量高度每两行重新present线之间的空间(未在getTextBounds计)。

Additionally you will have to add a variable height for each two lines that represent the space between the lines (which is not counted in the getTextBounds).

作为一个例子code,功能认识了多行文本的高度是这样的:

As an example code, the function to know the height of a multiple line text is something like this:

public int getHeightOfMultiLineText(String text,int textSize, int maxWidth) {
    paint = new TextPaint();
    paint.setTextSize(textSize);
    int index = 0;
    int linecount = 0;
    while(index < text.length()) {
        index += paint.breakText(text,index,text.length,true,maxWidth,null);
        linecount++;
    }

    Rect bounds = new Rect();
    paint.getTextBounds("Yy", 0, 2, bounds);
    // obtain space between lines
    double lineSpacing = Math.max(0,((lineCount - 1) * bounds.height()*0.25)); 

    return (int)Math.floor(lineSpacing + lineCount * bounds.height());

注:变了maxWidth在像素

然后,你将不得不调用里面,而这个方法来确定什么是该高度最大的字体大小。一个例子code将是:

Then you will have to call this method inside a while to determine what is the max font size for that height. An example code would be:

textSize = 100;
int maxHeight = 50;
while(getHeightOfMultiLineText(text,textSize,maxWidth) > maxHeight)
  textSize--;

不幸的是,这是唯一的(据我所知)的方式,我能够实现从上面的图像方面。

Unfortunately this was the only (as far as I know) way I was able to achieve the aspect from the images above.

希望这可以帮助任何人试图克服这一障碍。

Hope this can be of help to anyone trying to overcome this obstacle.

这篇关于如何自动调整上多行文本大小的TextView根据视图的最大尺寸是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆