, , , , StaticLayout
. . .
, , . , , .
BackgroundColorSpan
ForegroundColorSpan
.
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);
int next;
float xStart = 0;
float xEnd;
for (int i = 0; i < spannableString.length(); i = next) {
next = spannableString.nextSpanTransition(i, spannableString.length(), CharacterStyle.class);
xEnd = xStart + mTextPaint.measureText(spannableString, i, next);
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);
}
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
).