2
2
3
3
import android .content .Context ;
4
4
import android .content .res .AssetManager ;
5
+ import android .graphics .Color ;
5
6
import android .graphics .Typeface ;
6
7
import android .os .Build ;
7
8
import android .text .SpannableStringBuilder ;
12
13
import android .text .style .TypefaceSpan ;
13
14
import android .util .Log ;
14
15
16
+ import org .xml .sax .InputSource ;
17
+ import org .xml .sax .SAXException ;
18
+ import org .xml .sax .XMLReader ;
19
+ import org .xml .sax .helpers .XMLReaderFactory ;
20
+
15
21
import java .io .File ;
22
+ import java .io .IOException ;
23
+ import java .io .StringReader ;
16
24
import java .util .ArrayList ;
17
25
import java .util .HashMap ;
18
26
import java .util .StringTokenizer ;
@@ -201,7 +209,8 @@ public static SpannableStringBuilder stringBuilderFromHtmlString(Context context
201
209
if (htmlString == null ) {
202
210
return null ;
203
211
}
204
- Spanned spannedString = HtmlCompat .fromHtml (htmlString , HtmlCompat .FROM_HTML_MODE_COMPACT );
212
+ // Spanned spannedString = HtmlCompat.fromHtml(htmlString, HtmlCompat.FROM_HTML_MODE_COMPACT);
213
+ CharSequence spannedString = fromHtml (htmlString , context , false );
205
214
SpannableStringBuilder builder = new SpannableStringBuilder (spannedString );
206
215
207
216
TypefaceSpan [] spans = builder .getSpans (0 , builder .length (), android .text .style .TypefaceSpan .class );
@@ -215,8 +224,8 @@ public static SpannableStringBuilder stringBuilderFromHtmlString(Context context
215
224
if (split .length > 1 ) {
216
225
style = split [1 ];
217
226
}
218
- Typeface typeface = createTypeface (context , fontFolder , fontFamily , style == "bold" ? "bold" : "normal" ,
219
- style == "bold" , style == "italic" );
227
+ Typeface typeface = createTypeface (context , fontFolder , fontFamily , style . equals ( "bold" ) ? "bold" : "normal" ,
228
+ style . equals ( "bold" ) , style . equals ( "italic" ) );
220
229
221
230
if (typeface == null ) {
222
231
typeface = Typeface .create (fontFamily , Typeface .NORMAL );
@@ -338,4 +347,24 @@ public static SpannableStringBuilder stringBuilderFromFormattedString(Context co
338
347
339
348
return ssb ;
340
349
}
350
+
351
+ public static CharSequence fromHtml (CharSequence html , Context context , final boolean disableLinkStyle ) {
352
+ if (html instanceof String ) {
353
+ XMLReader xmlReader ;
354
+ try {
355
+ xmlReader = XMLReaderFactory .createXMLReader ("org.ccil.cowan.tagsoup.Parser" );
356
+ HtmlToSpannedConverter converter =
357
+ new HtmlToSpannedConverter (context , null , null , disableLinkStyle );
358
+ xmlReader .setContentHandler (converter );
359
+ xmlReader .parse (new InputSource (new StringReader ((String )html )));
360
+ return converter .spannable ();
361
+ } catch (SAXException | IOException e ) {
362
+ e .printStackTrace ();
363
+ }
364
+ }
365
+ return html ;
366
+ }
367
+ public static CharSequence fromHtml (Context context , CharSequence html ) {
368
+ return fromHtml (html , context ,false );
369
+ }
341
370
}
0 commit comments