多线的帆布drawtext

时间:2015-02-24 07:36:21

标签: android canvas drawtext

我正在开发一个图像评论应用程序。我用canvas.drawText(text, x, y, imgPaint);

在画布上绘制文字

这出现在一行中。当文本越过画布宽度

时,我需要将该行拆分为多行

提前致谢

3 个答案:

答案 0 :(得分:7)

您需要使用StaticLayout

TextPaint mTextPaint=new TextPaint();
StaticLayout mTextLayout = new StaticLayout("my text\nNext line is very long text that does not definitely fit in a single line on an android device. This will show you how!", mTextPaint, canvas.getWidth(), Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);

canvas.save();
// calculate x and y position where your text will be placed

textX = 100;
textY = 100;

canvas.translate(textX, textY);
mTextLayout.draw(canvas);
canvas.restore();

答案 1 :(得分:1)

您需要分割线条,并根据字体高度逐渐增加y来绘制每个片段。

例如:

var lines = text.split("\n"),
    x = 100, y = 100, fHeight = 16, // get x, y and proper font or line height here
    i = 0, line;

while(line = lines[i++]) {
    canvas.drawText(line, x, y, imgPaint);
    y += fHeight;
}

答案 2 :(得分:0)

现在添加另一个答案已经很晚了,但是如果有人不想使用StaticLayout,那么他们可以尝试我的多行文本逻辑

注意:此代码在View的onSizeChanged()方法中使用,并且textArray是存储每行的类变量

//This array will store all the words contained in input string
            val wordList = ArrayList<String>()

            //Temporary variable to store char or string
            var temp = ""

            it.trim().forEachIndexed { index, letter ->
                //Adding each letter to temp
                temp += letter

                //If letter is whiteSpace or last char then add it to wordList.
                //For example : Let input be "This is a Info text"
                //              since there is no whiteSpace after that last 't' then the last word
                //              will not be added to wordList there for checking for last letter is required
                //NOTE: the whiteSpace is also included in that word
                if (letter.isWhitespace() || index == it.length -1 ) {
                    wordList.add(temp)
                    //Resetting temp
                    temp = ""
                }
            }

            wordList.forEachIndexed { index, word ->
                //Measuring temp + word to check if their width in pixel is more than or equal to
                // the view's width + 60px(this is used so that word there is some space after each line. It can be changed)
                if (textPaint.measureText(temp + word) >= w - 60) {
                    textArray.add(temp)

                    //If adding last word to temp surpasses the required width then add the last word
                    // separately since the loop will be terminated after that
                    if (index == wordList.size - 1){
                        textArray.add(word)
                        return@forEachIndexed
                    }

                    //Resetting temp
                    temp = ""
                } else if (index == wordList.size - 1) {
                    //If adding last word to temp doesn't surpasses the required width the add that
                    // line to list
                    textArray.add(temp + word)
                    return@forEachIndexed
                }

                //Adding word to temp
                temp += word
            }

然后使用onDraw()方法

  textArray.forEachIndexed { index, singleLine ->
                    //x is set to 16f so that there is some space before first word
                    //y changes with each line i.e 1st line will be drawn at y = 60f, 2nd at 120f and so on
                    it.drawText(singleLine, 16f, (index + 1) * 60f, textPaint)
                }