Uniform text wrapping in TextView

前端 未结 6 1395
隐瞒了意图╮
隐瞒了意图╮ 2021-01-19 06:34

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:



        
相关标签:
6条回答
  • 2021-01-19 07:11

    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

    0 讨论(0)
  • 2021-01-19 07:26

    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 example

    Sources are available on github

    0 讨论(0)
  • 2021-01-19 07:28

    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!

    0 讨论(0)
  • 2021-01-19 07:29

    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.

    enter image description here

    Hope this will help you.

    0 讨论(0)
  • 2021-01-19 07:29

    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

    0 讨论(0)
  • 2021-01-19 07:31

    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.

    0 讨论(0)
提交回复
热议问题