简体   繁体   中英

Paint.breakText() counts glyphs, not characters

One of our developers ran into an issue where Paint.breakText() (which says it counts "chars") is actually counting glyphs.

Imagine that you are wrapping just the one word "fit" . It will fit on the line, so you expect breakText() to return 3. On some devices, it does; on others, the "fi" form a ligature, and breakText() returns 2 . This cause you to draw

fi
t

... which is not what you want!

Is there either

  1. A flag to make breakText() count Java chars, not glyphs? Or
  2. A way to detect that "fi" will be treated as a single glyph?

这似乎不是最佳选择,但是我们处理它的方法是使用paint.breakText("fit", true, 1000, null) == 2作为bugExists标志,然后使用measureText()进行检查结果。

I found how to do it well here: Android: is Paint.breakText(...) inaccurate?

my code is:

on onCreate:

paintOfText.setSubpixelText(true);
breakTextNumber = paintOfText.breakText(textToBreak, true,
    maximumSizeOfText, null);

on render class:

if (breakTextNumber < textToBreak.length()) {
    while (textToBreak.charAt(breakTextNumber) != ' ') {
        breakTextNumber--;
    }
    myCanvas.drawText(textToBreak.substring(0, breakTextNumber),
            placeToDrawX, placeToDrawY, paintOfText);
    myCanvas.drawText(
            textToBreak.substring(breakTextNumber+1),
            placeToDrawX, placeToDrawY2,, paintOfText);
    }
}

This is just to split text in 2 lines, cutting the string with spaces. Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM