How to find android TextView number of characters per line?

后端 未结 3 2068
北海茫月
北海茫月 2020-12-01 05:26

So I have a TextView in android that has the width of the whole length of the screen and a padding of dip 5. How can I calculate the number of characters that will fit a si

相关标签:
3条回答
  • 2020-12-01 06:15

    Try this:

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

    Detecting how many characters fit will be impossible due to the variable width of the characters. The above function will test if a particular string will fit or not in the TextView. The content of newText should be all the characters in a particular line. If true, then start a new line (and using a new string to pass as parameter).


    Answer to the comment:

    1. because the app can be run in many systems is exactly why you need to measure it.
    2. This is a way to solve your "overall question". What is the difference between using str.size()>numCol vs is too large? You will need to implement your animation (hint #1: insert a newline character)
    3. as I said before when you start a new line, you start a new string (hint #2: if you extend TextView, you can implement all this in overriding setText). (hint #3: Keep track of the lines created with a static int lines; and use newString.split("\\r?\\n")[lines-1] to check for length).
    0 讨论(0)
  • 2020-12-01 06:17

    You can get total line of Textview and get string for each characters by below code.Then you can set style to each line whichever you want.

    I set first line bold.

    private void setLayoutListner( final TextView textView ) {
            textView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    textView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
    
                    final Layout layout = textView.getLayout();
    
                    // Loop over all the lines and do whatever you need with
                    // the width of the line
                    for (int i = 0; i < layout.getLineCount(); i++) {
                        int end = layout.getLineEnd(0);
                        SpannableString content = new SpannableString( textView.getText().toString() );
                        content.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, end, 0);
                        content.setSpan(new StyleSpan(android.graphics.Typeface.NORMAL), end, content.length(), 0);
                        textView.setText( content );
                    }
                }
            });
        }
    

    Try this way.You can apply multiple style this way.

    0 讨论(0)
  • 2020-12-01 06:20

    I had the same issue and I calculated the number characters per line by 2 steps: Step 1: Calculate the number of lines

     val widthOfTvComment = widthOfScreen - marginLeft - marginRight
     var bounds  = Rect()
     var paint = Paint()
     paint.textSize = textSize
     paint.getTextBounds(comment,0,comment.length,bounds)
    
    val lines = ( bounds.width()/widthOfTvComment)
    

    Step 2: Calculated the number characters per line

    val charactersPerLine = comment.length / lines
    
    0 讨论(0)
提交回复
热议问题