I want to apply spans on text of TextView
for two purpose.First for parsing smileys and second for highlighting the text .I have created this method for the same purpose but is not working .Please tell me where I am going wrong.
public CharSequence addSmileySpansTextFilter(final CharSequence text,String filter,int color) {
SpannableStringBuilder builder = new SpannableStringBuilder(text);
Matcher matcher = mPattern.matcher(text);
while (matcher.find()) {
int resId = mSmileyToRes.get(matcher.group());
builder.setSpan(new ImageSpan(mContext, resId), matcher.start(), matcher.end(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
if (filter!=null&&!filter.equals("")) {
int startPos = builder.toString().toLowerCase(Locale.US).indexOf(filter.toLowerCase(Locale.US));
int endPos = startPos + filter.length();
if (startPos != -1) // This should always be true, just a sanity check
{
BackgroundColorSpan highlightSpan = new BackgroundColorSpan(color);
builder.setSpan(highlightSpan, startPos, endPos,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
}
return builder;
}
Aucun commentaire:
Enregistrer un commentaire