grantland / android-autofittextview

A TextView that automatically resizes text to fit perfectly within its bounds.
Apache License 2.0
4.27k stars 689 forks source link

Text in one line only #31

Open pawelantczak opened 9 years ago

pawelantczak commented 9 years ago

Hello. What I want to achieve is exactly what is shown on animation. But, if I set android:singleLine="true" text never expands to second line. If single line is not set, resizing don't work.

Even if I run your demo, text is always in one line. How to make it behave as on animated gif?

grantland commented 9 years ago

Use android:maxLines="2" instead of android:singleLine="true".

herrbert74 commented 9 years ago

Using maxLines="2" doesn't work because in AutofitHelper.autofit() you call getAutofitTextSize only if maxLines==1. I deleted this requirement in my subclass and it works.

grantland commented 9 years ago

It should work though, because I check that OR lineCount > maxLines here: https://github.com/grantland/android-autofittextview/blob/master/library/src/main/java/me/grantland/widget/AutofitHelper.java#L125

Is that returning something incorrect?

herrbert74 commented 9 years ago

I see. For me the text was two words on a button, and the first word was too long, so it was chopped and the second part was added to the second line. The second line wasn't too long even so, so getAutofitTextSize was never triggered. So basically it doesn't work if a single word is longer than a line. We need a check for that.

sehoi commented 8 years ago
  1. use the type class TextView instead of class AutofitTextView.
  2. add textView.setMaxLines(2) programmatically and AutofitHelper.create(textView)
niosus commented 8 years ago

@sehoi this doesn't seem to work on my side. The behavior is the same as if I set it in xml

@herrbert74 is right - it seems to happen when a single word is longer than a line.