Skip to content
This repository was archived by the owner on Sep 7, 2020. It is now read-only.

Extracted the HTML format logic to the HtmlFormatter #172

Conversation

ivannarino
Copy link
Contributor

This allows to format an HTML string without having to use an HtmlTextView in the layout.

Also this allows to customize the html before passing it to the TextView, for example one could change all BulletSpans with its own implementation.

Spanned formattedHtml = HtmlFormatter.formatHtml(new HtmlFormatterBuilder().setHtml("<h2>Hello wold</h2><ul><li>cats</li><li>dogs</li></ul><img src=\"cat_pic\"/>").setImageGetter(new HtmlResImageGetter(htmlTextView.getContext())));
SpannableStringBuilder textSpanBuilder = new SpannableStringBuilder(formattedHtml);
final BulletSpan[] bulletSpans = textSpanBuilder.getSpans(0, text.length(), BulletSpan.class);
for (final BulletSpan bulletSpan : bulletSpans) {
  int start = text.getSpanStart(bulletSpan);
  int end = text.getSpanEnd(bulletSpan);
  textSpanBuilder.removeSpan(bulletSpan);
  textSpanBuilder.setSpan(
    new FixedBulletSpan(bulletSpan.getGapWidth()),
    start,
    end,
    Spanned.SPAN_INCLUSIVE_EXCLUSIVE
  );
}

HtmlFormatterBuilder parameter uses the builder pattern.

Removed unused HtmlTagHandler#mTextPaint
Replace HtmlResImageGetter#container TextView with Context

This allows to format an HTML string without having to use an HtmlTextView in the layout.
Also this allows to customize the html before passing it to the TextView, for example one could change all BulletSpans with its own implementation.
HtmlFormatterBuilder parameter uses the builder pattern.

Removed unused HtmlTagHandler#mTextPaint
Replace HtmlResImageGetter#container TextView with Context
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants