简体   繁体   中英

Uniform text wrapping in TextView

I need nice text wrapping in TextView, especially for text in headers.

Text wrapping for TextView might look like this, where the last word is in new line:

| ========================= |
|          =====            |

That what I would like to have is wrapping where lines width is more equable:

|     ================      |
|      ==============       |

It's easy to add '\\n' for one language and test it on different screen sizes but not when there is more than 10 translations.

I have modified TextView and created UniformTextView. After investigating of TextView sources I have decided to minimize TextView's width to have preferred lines number.

    <pl.dziobas.uniformtextview.UniformTextView
        android:text="@string/sample_text"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        app:prefLineNumber="2"
        style="@style/sample_style" />

It works satisfactorily for me.
UniformTextView示例

Sources are available on github

You can add a '\\n' in your string resouce xml to add a newline so you can managethe wrapping yourself.

Another approach would be to dynamically add the '\\n' where you get the string length divided by 2 and search for the next space in either direction and on the first find you just add '\\n' there. A hack, but probably work.

Other than that there is not much in Android for Hyphenation or Typography. Propably this post will give you some tips: http://smarter-than-the-average-pierre.blogspot.co.at/2011/10/bad-android-typography-tale-of-text.html

There is an open source project in Github AutoFittextView and in BitBucket AutoScaletextView .

You can change according to your requirement.

I tried the AutoScaleTextview and reached to the below OutPut .

在此输入图像描述

Hope this will help you.

The algorithm would be roughly:

  1. calculate String width (with Paint.measureText or something)
  2. if it is less than container ( TextView ) width, just use it
  3. otherwise divide String width by container to know how many "\\n" to enter
  4. with some search algorithm look for a space character the closest to 1/nth of the width (again using measureText if the font is not monospace)
  5. substring at that point and repeat point 4 for n-1 (n > 1)

Probably will require some adjustments like using container width by some small percent smaller than real.

If you know whats the text in advance (I means if its a hard coded text), you can fix the width of TextView in dp, so that it wraps up properly, or else you may also use android:ems to fix the maximum number of characters in a line. So your TextView can be:

<TextView
   android:layout_height="wrap_content"
   android:layout_width="100dp"
   android:lines="2"/>

OR

  <TextView
     android:layout_height="wrap_content"
     android:layout_width="wrap_content"
     android:ems="15"
     android:lines="2" />

I hope this helps!

Check out Android's own autoTextSizing for TextView , added in add API 26 with Support Library that supports from API 14 onwards with app:autoSizeTextType="uniform" .

Have fun!

https://developer.android.com/guide/topics/ui/look-and-feel/autosizing-textview.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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