如何画一条线以完美对齐文本的顶部?
How to draw a line to perfectly align top of a text?
我能够完美地对齐基线。 getLineBounds
将提供与文本完美对齐的基线(lineBottom - descent)。
我使用 rect.top
作为顶线,这将在顶部给出一行填充,请参见屏幕截图。
主要问题是我有不同 fonts.And 此代码在某些字体上完美运行。
这是代码
int baseline = getLineBounds(i, rect);
int topLine = rect.top;
canvas.drawLine(rect.left - padding, baseline, rect.right + padding,
baseline, paint);
canvas.drawLine(rect.left - padding, topLine, rect.right + padding, topLine,
paint);
canvas.drawLine(rect.left - padding, (baseline + topLine) / 2, rect.right
+ padding, (baseline + topLine) / 2, paint1);
这是我试过的。
1) 使用 "StaticLayout" 得到顶行但没有任何区别。
2) 使用paint获取字体高度并加上baseline
paint.setTextSize(getTextSize());
paint.setTypeface(getCurrentTypFace());
paint.getTextBounds(getText().toString(), 0, getText().length(), r);
int height = r.height();
int topLine = baseline + height;
3) 尝试使用 FontPadding =false android:includeFontPadding="false"
所以我的问题是如何获得像基线一样的顶线。
非常感谢任何帮助。
再次尝试 paint.getTextBounds(...)
,但使用 r.top
(或实际上 -r.top
)而不是 r.height
:
paint.setTextSize(getTextSize());
paint.setTypeface(getCurrentTypFace());
paint.getTextBounds(getText().toString(), 0, getText().length(), r);
int ascent = r.top;
int topLine = baseline + ascent;
r.height
给出了从字符最底部到最顶部的距离,而 r.top
应该给出从基线到最顶部的距离的负值。
如果这也不起作用,您可能只需要将文本绘制到内存中的临时 canvas 并 运行 逐行绘制像素,直到找到黑色的自己测量上升。
无论哪种情况,请确保您使用的文本中包含大写字母。如果您使用仅包含小写字母的文本,它可能会为您提供橙色虚线的坐标。
请注意,这两种方法都会为您提供字体最上边缘的像素坐标,而不是笔画的中心。此外,某些字体中的某些字母可能会超出您真正想要绘制的坐标。你最安全的选择可能是选择一个标准字符串来测量,比如蓝线的大写 O
和橙色线的小写 o
(如果它不是具有O
右上角的一个小波浪形,超出圆圈的顶部......)然后为每种字体存储一些小的偏移量,告诉你笔划的线宽有多粗,所以你的线条穿过字母的顶部而不是上面。
我能够完美地对齐基线。 getLineBounds
将提供与文本完美对齐的基线(lineBottom - descent)。
我使用 rect.top
作为顶线,这将在顶部给出一行填充,请参见屏幕截图。
主要问题是我有不同 fonts.And 此代码在某些字体上完美运行。
这是代码
int baseline = getLineBounds(i, rect);
int topLine = rect.top;
canvas.drawLine(rect.left - padding, baseline, rect.right + padding,
baseline, paint);
canvas.drawLine(rect.left - padding, topLine, rect.right + padding, topLine,
paint);
canvas.drawLine(rect.left - padding, (baseline + topLine) / 2, rect.right
+ padding, (baseline + topLine) / 2, paint1);
这是我试过的。
1) 使用 "StaticLayout" 得到顶行但没有任何区别。
2) 使用paint获取字体高度并加上baseline
paint.setTextSize(getTextSize());
paint.setTypeface(getCurrentTypFace());
paint.getTextBounds(getText().toString(), 0, getText().length(), r);
int height = r.height();
int topLine = baseline + height;
3) 尝试使用 FontPadding =false android:includeFontPadding="false"
所以我的问题是如何获得像基线一样的顶线。 非常感谢任何帮助。
再次尝试 paint.getTextBounds(...)
,但使用 r.top
(或实际上 -r.top
)而不是 r.height
:
paint.setTextSize(getTextSize());
paint.setTypeface(getCurrentTypFace());
paint.getTextBounds(getText().toString(), 0, getText().length(), r);
int ascent = r.top;
int topLine = baseline + ascent;
r.height
给出了从字符最底部到最顶部的距离,而 r.top
应该给出从基线到最顶部的距离的负值。
如果这也不起作用,您可能只需要将文本绘制到内存中的临时 canvas 并 运行 逐行绘制像素,直到找到黑色的自己测量上升。
无论哪种情况,请确保您使用的文本中包含大写字母。如果您使用仅包含小写字母的文本,它可能会为您提供橙色虚线的坐标。
请注意,这两种方法都会为您提供字体最上边缘的像素坐标,而不是笔画的中心。此外,某些字体中的某些字母可能会超出您真正想要绘制的坐标。你最安全的选择可能是选择一个标准字符串来测量,比如蓝线的大写 O
和橙色线的小写 o
(如果它不是具有O
右上角的一个小波浪形,超出圆圈的顶部......)然后为每种字体存储一些小的偏移量,告诉你笔划的线宽有多粗,所以你的线条穿过字母的顶部而不是上面。