下面列出了android.text.Editable#clearSpans ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void reset(EditText editText) {
final Editable editable = editText.getEditableText();
if (TextUtils.isEmpty(editable)) {
return;
}
editable.clearSpans();
ChipDrawableSpan.apply(editText.getContext(), editable, editText.hasFocus());
}
public void setColoredAdContent(TextView contentText, final ForumAdJson forumAd) {
String subject = StringUtils.get(forumAd.getContent());
Editable editable = contentText.getEditableText();
if (editable != null) {
editable.clear();
editable.clearSpans();
}
final String recomName = forumAd.getRecomName();
SpannableStringBuilder ssb = new SpannableStringBuilder(subject + recomName);
Drawable bg = context.getResources().getDrawable(R.drawable.bg_recom);
ImageSpan imageSpan = new ImageSpan(bg) {
@Override
public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y,
int bottom, Paint paint) {
paint.setTypeface(Typeface.DEFAULT);
int textSize = DensityUtils.dip2px(context, 12);
paint.setTextSize(textSize);
Rect bounds = new Rect();
paint.getTextBounds(text.toString(), start, end, bounds);
getDrawable().setBounds(0, 0, bounds.width() + 10, bottom - top);
super.draw(canvas, text, start, end, x, top, y, bottom, paint);
paint.setColor(Color.TRANSPARENT);
canvas.drawText(text.subSequence(start, end).toString(), x + 5, y, paint);
}
};
ssb.setSpan(imageSpan, subject.length(), subject.length() + recomName.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
contentText.setText(ssb);
}
public static void reset(EditText editText) {
Editable editable = editText.getText();
if (editable != null) {
editable.clearSpans();
}
editText.setText(editable);
}
private Editable highlight( Editable e )
{
try
{
e.clearSpans();
if( e.length() == 0 )
return e;
for( Matcher m = numbers.matcher( e );
m.find(); )
e.setSpan(
new ForegroundColorSpan(Color.parseColor(COLOR_NUMBER)),
m.start(),
m.end(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE );
for( Matcher m = keywords.matcher( e );
m.find(); )
e.setSpan(
new ForegroundColorSpan(Color.parseColor(COLOR_KEYWORD)),
m.start(),
m.end(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE );
for( Matcher m = variables.matcher( e );
m.find(); )
e.setSpan(
new ForegroundColorSpan(Color.parseColor(COLOR_VARIABLE)),
m.start(),
m.end(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE );
for( Matcher m = builtins.matcher( e );
m.find(); )
e.setSpan(
new ForegroundColorSpan( COLOR_BUILTIN ),
m.start(),
m.end(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE );
for( Matcher m = strings.matcher( e );
m.find(); )
e.setSpan(
new ForegroundColorSpan(Color.parseColor(COLOR_STRING)),
m.start(),
m.end(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE );
for( Matcher m = comments.matcher( e );
m.find(); )
e.setSpan(
new ForegroundColorSpan( COLOR_COMMENT ),
m.start(),
m.end(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE );
}
catch( Exception ex )
{
}
//
// e.setSpan(
// new BackgroundColorSpan( COLOR_COMMENT ),
// 1, e.length() -1,
// Spannable.SPAN_EXCLUSIVE_EXCLUSIVE );
// e.setSpan(new TypefaceSpan("monospace"), 0, e.length() -1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE );
return e;
}
public Editable highlight(Editable editable, boolean newText) {
editable.clearSpans();
if (editable.length() == 0) {
return editable;
}
editorHeight = getHeight();
if (!newText && editorHeight > 0) {
firstVisibleIndex = getLayout().getLineStart(LineUtils.Companion.getFirstVisibleLine(MainActivity.verticalScroll, editorHeight, lineCount));
lastVisibleIndex = getLayout().getLineEnd(LineUtils.Companion.getLastVisibleLine(MainActivity.verticalScroll, editorHeight, lineCount, deviceHeight) - 1);
} else {
firstVisibleIndex = 0;
lastVisibleIndex = CHARS_TO_COLOR;
}
firstColoredIndex = firstVisibleIndex - (CHARS_TO_COLOR / 5);
// normalize
if (firstColoredIndex < 0)
firstColoredIndex = 0;
if (lastVisibleIndex > editable.length())
lastVisibleIndex = editable.length();
if (firstColoredIndex > lastVisibleIndex)
firstColoredIndex = lastVisibleIndex;
textToHighlight = editable.subSequence(firstColoredIndex, lastVisibleIndex);
if (TextUtils.isEmpty(MainActivity.Companion.getFileExtension()))
MainActivity.Companion.setFileExtension("");
HighlightDriver highlightDriver = new HighlightDriver(new AndroidHighlightColorProvider(),
MainActivity.Companion.getFileExtension());
List<HighlightInfo> highlights = highlightDriver.highlightText(textToHighlight, firstColoredIndex);
for (HighlightInfo info : highlights) {
editable.setSpan(
new ForegroundColorSpan(info.getColor()),
info.getStart(),
info.getEnd(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
return editable;
}