0

I want to get line count before drawing the text. Here in my code I'm getting count(current line number). But it's working not correct. In while loop I'm setting one word at one step then trying to know line count comparing canvas width and my temp text width.

    private void draw(Canvas canvas, String text, int size) {
    canvas.drawPaint(paint);

    tv = new TextView(mContext);

    tv.setTextColor(Color.BLACK);

    tv.setTextSize(size);

    int lineHeight = tv.getLineHeight();

    // max lines on the screen
    int lines = canvas.getHeight() / lineHeight;

    //max_height = lines * tv.getLineHeight();

    String screenText = "";

    Pattern stuff = Pattern.compile("[\\w']+|[\\s.,!?;:-]");
    Matcher matcher = stuff.matcher(text);
    List<String> matchList = new ArrayList<String>();

    while (matcher.find()) {
        matchList.add(matcher.group(0));
    }

    // String wholeText[] = text.split(" ");

    int i = 0;
    String word;
    paint.setTextSize(size*3);

    while (count <= lines-1&&i<=matchList.size()-1) {
        word = matchList.get(i++);
        //wholeText[i++];

        screenText += word;
        tempStr += word;

        float w = paint.measureText(tempStr, 0, tempStr.length());

        if(canvas.getWidth() <= w) {
            count++;
            tempStr=word;
        }

        if(count>lines){
            break;
        }

        tv.setText(screenText);

    }

    // you have to enable setDrawingCacheEnabled, or the getDrawingCache will return null
    tv.setDrawingCacheEnabled(true);

    tv.measure(MeasureSpec.makeMeasureSpec(canvas.getWidth(), MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(canvas.getHeight(), MeasureSpec.EXACTLY));
    tv.setPadding(10,0,10,0);
    //tv.layout(0, 0, tv.getMeasuredWidth(), lines * tv.getLineHeight());
    tv.layout(0, 0, tv.getMeasuredWidth(), tv.getMeasuredHeight());

    canvas.drawBitmap(tv.getDrawingCache(), 0, 0, paint);
    //canvas.drawText(tv.getText().toString(), 0, 0, paint);
    // disable drawing cache
    tv.setDrawingCacheEnabled(false);
}
sj_8
  • 173
  • 1
  • 16
  • possible duplicate of [How to get line count of textview before rendering?](http://stackoverflow.com/questions/15679147/how-to-get-line-count-of-textview-before-rendering) – samgak May 15 '15 at 03:28
  • @samgak I have used it. But I can't get real number on line. And how can I get textview width if its not rendered? – sj_8 May 15 '15 at 04:05
  • you are not adding `tv` to any parent view, what actually do you want to achieve? – pskink May 15 '15 at 05:20
  • @pskink I want to fill the screen with text as much as I can. And I want to know how much text(from whole text) I can set to the screen and need to know the position of last word setted to the screen – sj_8 May 15 '15 at 05:41
  • why don't you use `StaticLayout` class? – pskink May 15 '15 at 05:51

1 Answers1

1

Just write here:

fun getTextViewStaticLayout(tv: TextView, width: Int): StaticLayout {
    val textPaint = TextPaint()
    textPaint.isAntiAlias = true
    textPaint.textSize = tv.textSize
    return StaticLayout(tv.text.toString(), textPaint, width, Layout.Alignment.ALIGN_NORMAL,
        1F, 0F, true)
}

width - is with of textView (screenWidth or other)

Then we can use staticLayout to get the property of textView before draw (lineCount, getLineBottom ...)

Andrey Tuzov
  • 143
  • 1
  • 7