Make the text fit as screen size

I have a long text, so I decided to divide it into pages, and I put it so that each scroll scrolls to the next page. So I did this:

    NumberOfPages=text.length()/CharPerPage;    //CharPerPage=500;  
    NumberOfPages++;        

    Chapters.add(text.indexOf(" ", CurrentPage*CharPerPage));
    CurrentPage++;
    int tmp=0;
    for(int i =NumberOfPages;i>0;i--)
    {           
         tmp =(CurrentPage*CharPerPage);
        if(tmp>text.length())
        {
            Chapters.add(text.length());    
            break;
        }
        else
        {
        Chapters.add(text.indexOf(" ", tmp));   
        CurrentPage++;
        }
    }

So, I divide the text into pages, and each page has 500 characters ... But this is not good, since Android has different sizes and shapes of the screen, and line breaks are not taken into account, so it can go beyond the screen ... So can it Does anyone suggest a way to find out how many characters it takes to fill the screen so that I can leave the rest of the text on another page? Thank.

+3
source share
1 answer

Ok, here's a shot - and rather awkward, but shorter:

* , * , *

-

  private boolean isTooLarge (TextView text, String newText) {    
     float textWidth = text.getPaint().measureText(newText);
     return (textWidth >= text.getMeasuredWidth ());
  } 

:

numLinesPerPage=mTextView.getHeight()/mTextView.getLineHeight();  //not this doesn't handle special cases where you've changed the font, bolded it etc

, , , ( , ).

note: getHeight , - onDraw , , onMeasure, .

+1

All Articles