我想将 SpannedString
绘制到 Canvas
。
SpannableString spannableString = new SpannableString("Hello World!");
ForegroundColorSpan foregroundSpan = new ForegroundColorSpan(Color.RED);
BackgroundColorSpan backgroundSpan = new BackgroundColorSpan(Color.YELLOW);
spannableString.setSpan(foregroundSpan, 1, 8, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableString.setSpan(backgroundSpan, 3, spannableString.length() - 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spannableString);
上面的示例是使用 TextView
绘制的,后者又使用 Layout
来绘制具有跨度的文本。我知道 using a Layout
is the recommended way将文本绘制到 Canvas 上。但是,我正在从头开始制作我自己的文本布局,所以我需要自己实现它。
这样做是行不通的
canvas.drawText(spannableString, 0, spannableString.length(), 0, 0, mTextPaint);
因为 drawText
只从 spannableString
中获取文本,而不是任何 span。绘图颜色由 TextPaint
单独处理。
如何使用canvas.drawText
(或drawTextRun
)绘制跨度信息(这里特别是前景色和背景色)?
相关
- How to loop through the spans in a SpannedString or SpannableString in Android
- Is it possible to display multi-color text with one call to Canvas.drawText()?
规划解决方案
我本来打算直接做一个 self 回答,但事实证明这比我想象的要难。所以我会先发帖,然后在我能弄明白的时候添加答案。 (我当然欢迎任何人先回答。)
以下是我到目前为止的作品:
- Draw each span range as a separate text run
- 使用
drawTextRun
绘制文本(examples)(更新:直到 API 23 才添加) - 使用
getRunAdvance
测量从哪里开始下一次文本运行(更新:直到 API 23 才添加,请改用measureText
) - 背景颜色可能需要单独绘制(使用
drawRect
或drawPath
?请参阅 here、here 和 here。) TextView
的源代码,StaticLayout
, 和TextLine
最佳答案
对于遇到这个问题的大多数人来说,您可能应该使用 StaticLayout
来绘制跨距文本。参见 this answer寻求帮助。
但是,如果您确实需要自己绘制跨区文本,那么您将需要 loop through all the spanned ranges并分别画出每一个。您还需要测量每个跨度中文本的长度,以便您知道从哪里开始绘制下一个跨度。
下面的代码处理 BackgroundColorSpan
和 ForegroundColorSpan
。
// set up the spanned string
SpannableString spannableString = new SpannableString("Hello World!");
ForegroundColorSpan foregroundSpan = new ForegroundColorSpan(Color.RED);
BackgroundColorSpan backgroundSpan = new BackgroundColorSpan(Color.YELLOW);
spannableString.setSpan(foregroundSpan, 1, 8, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableString.setSpan(backgroundSpan, 3, spannableString.length() - 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
// draw each span one at a time
int next;
float xStart = 0;
float xEnd;
for (int i = 0; i < spannableString.length(); i = next) {
// find the next span transition
next = spannableString.nextSpanTransition(i, spannableString.length(), CharacterStyle.class);
// measure the length of the span
xEnd = xStart + mTextPaint.measureText(spannableString, i, next);
// draw the highlight (background color) first
BackgroundColorSpan[] bgSpans = spannableString.getSpans(i, next, BackgroundColorSpan.class);
if (bgSpans.length > 0) {
mHighlightPaint.setColor(bgSpans[0].getBackgroundColor());
canvas.drawRect(xStart, mTextPaint.getFontMetrics().top, xEnd, mTextPaint.getFontMetrics().bottom, mHighlightPaint);
}
// draw the text with an optional foreground color
ForegroundColorSpan[] fgSpans = spannableString.getSpans(i, next, ForegroundColorSpan.class);
if (fgSpans.length > 0) {
int saveColor = mTextPaint.getColor();
mTextPaint.setColor(fgSpans[0].getForegroundColor());
canvas.drawText(spannableString, i, next, xStart, 0, mTextPaint);
mTextPaint.setColor(saveColor);
} else {
canvas.drawText(spannableString, i, next, xStart, 0, mTextPaint);
}
xStart = xEnd;
}
下图中最上面的字符串是用上面的代码绘制的。底部字符串是使用常规 TextView
(使用 StaticLayout
)绘制的。
关于android - 如何在 Android 中使用 Canvas.drawText 绘制 Spanned String,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43275159/