【发布时间】:2014-06-28 20:52:39
【问题描述】:
在我正在开发的应用程序中,我正在尝试使用 Paint 在 Canvas 上绘制一些文本。为此,我使用:
String string = " a ";
Rect bounds = new Rect();
mPaint.getTextBounds(string, 0, string.length(), bounds);
问题是,这给了我“a”的界限,而不是“a”的界限。它会删除 a 之后的空格,但我不希望它这样做。
有谁知道我如何获得包括字母后空格在内的文本边界?
**编辑:**
我找到了解决方法:
String string = " a ";
Rect bounds = new Rect();
mPaint.getTextBounds(string, 0, string.length(), bounds);
float width = mPaint.measureText(string);
float height = bounds.height();
RectF boundsF = new RectF(x, y, x + width, y + height);
问题在于 getTextBounds() 是一种删除所有后沿空格的本机方法。 Paint.measureText(String string) 不这样做,所以我们可以使用这个宽度。
【问题讨论】:
-
您是否尝试过将“ ”字符临时替换为其他字符,例如“_”(下划线)
-
是的,如果我这样做,它会显示下划线:)