How to find android TextView number of characters per line?

后端 未结 3 2162
北海茫月
北海茫月 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条回答
  •  Happy的楠姐
    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
    

提交回复
热议问题