Este código debe establecer en negrita todo lo que entre en la etiqueta html bold. Y también elimina la etiqueta para que solo se muestre el contenido en el interior.
SpannableStringBuilder sb = new SpannableStringBuilder("this is <b>bold</b> and this is <b>bold too</b> and this is <b>bold too, again</b>.");
Pattern p = Pattern.compile("<b>.*?</b>", Pattern.CASE_INSENSITIVE);
boolean stop = false;
while (!stop)
{
Matcher m = p.matcher(sb.toString());
if (m.find()) {
sb.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), m.start(), m.end(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
sb.delete(m.end()-4, m.end());
sb.delete(m.start(), m.start() + 3);
}
else
stop = true;
}
Este código también se puede adaptar para otros etiquetas de estilo HTML, como superíndice (tag sup), etc.
SpannableStringBuilder sb = new SpannableStringBuilder("text has <sup>superscript</sup> tag");
Pattern p = Pattern.compile("<sup>.*?</sup>", Pattern.CASE_INSENSITIVE);
boolean stop = false;
while (!stop)
{
Matcher m = p.matcher(sb.toString());
if (m.find()) {
sb.setSpan(new SuperscriptSpan(), m.start(), m.end(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
sb.delete(m.end()-6, m.end());
sb.delete(m.start(), m.start() + 5);
}
else
stop = true;
}
para establecer el color, sólo tiene que utilizar el ForegroundColorSpan con setSpan.
sb.setSpan(new ForegroundColorSpan(Color.rgb(255, 0, 0)), m.start(), m.end(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
Espero que ayude.
¿en qué está configurado el TextEdit? –
También, ¿cómo puedo establecer el tamaño de fuente también para decir el negrita? entonces estoy especificando ambos que es negrita y digo font 20? También –
favor de actualización del índice ser (finalString.indexOf (normalBOLD), finalString.indexOf (normalBOLD) + normalBOLD.lenth()) –