diff --git a/HtmlTextView/build.gradle b/HtmlTextView/build.gradle index 385e793..0501b05 100644 --- a/HtmlTextView/build.gradle +++ b/HtmlTextView/build.gradle @@ -22,4 +22,5 @@ publish { dependencies { implementation 'androidx.annotation:annotation:1.1.0' -} + + } diff --git a/HtmlTextView/src/main/java/org/sufficientlysecure/htmltextview/DesignQuoteSpan.java b/HtmlTextView/src/main/java/org/sufficientlysecure/htmltextview/DesignQuoteSpan.java new file mode 100644 index 0000000..8b08784 --- /dev/null +++ b/HtmlTextView/src/main/java/org/sufficientlysecure/htmltextview/DesignQuoteSpan.java @@ -0,0 +1,73 @@ +package org.sufficientlysecure.htmltextview; + +import android.graphics.Canvas; +import android.graphics.Paint; +import android.text.Layout; +import android.text.style.LeadingMarginSpan; +import android.text.style.LineBackgroundSpan; + +import androidx.annotation.NonNull; + +public class DesignQuoteSpan implements LeadingMarginSpan, LineBackgroundSpan { + + private int backgroundColor,stripColor; + private float stripeWidth,gap; + DesignQuoteSpan(int backgroundColor, + int stripColor, + float stripWidth, + float gap){ + + this.backgroundColor=backgroundColor; + this.stripColor=stripColor; + this.stripeWidth=stripWidth; + this.gap=gap; + + } + + @Override + public int getLeadingMargin(boolean first) { + return (int)(stripeWidth + gap); + } + + @Override + public void drawLeadingMargin(Canvas c, + Paint p, + int x, + int dir, + int top, + int baseline, + int bottom, + CharSequence text, + int start, + int end, + boolean first, + Layout layout) { + + Paint.Style style=p.getStyle(); + int paintColor=p.getColor(); + p.setStyle(Paint.Style.FILL); + p.setColor(stripColor); + c.drawRect((float)x,(float)top,x+dir * stripeWidth,(float)bottom,p); + p.setStyle(style); + p.setColor(paintColor); + + } + + @Override + public void drawBackground(@NonNull Canvas canvas, + @NonNull Paint paint, + int left, + int right, + int top, + int baseline, + int bottom, + @NonNull CharSequence text, + int start, + int end, + int lineNumber) { + int paintColor=paint.getColor(); + paint.setColor(backgroundColor); + canvas.drawRect((float)left,(float)top,(float)right,(float)bottom,paint); + paint.setColor(paintColor); + } +} diff --git a/HtmlTextView/src/main/java/org/sufficientlysecure/htmltextview/HtmlTextView.java b/HtmlTextView/src/main/java/org/sufficientlysecure/htmltextview/HtmlTextView.java index b3541d0..9f6cb26 100644 --- a/HtmlTextView/src/main/java/org/sufficientlysecure/htmltextview/HtmlTextView.java +++ b/HtmlTextView/src/main/java/org/sufficientlysecure/htmltextview/HtmlTextView.java @@ -18,6 +18,9 @@ import android.content.Context; import android.text.Html; +import android.text.Spannable; +import android.text.Spanned; +import android.text.style.QuoteSpan; import android.util.AttributeSet; import androidx.annotation.NonNull; @@ -31,7 +34,10 @@ public class HtmlTextView extends JellyBeanSpanFixTextView { public static final String TAG = "HtmlTextView"; public static final boolean DEBUG = false; - + public int blockQuoteBackgroundColor= getResources().getColor(R.color.White); + public int blockQuoteStripColor= getResources().getColor(R.color.black); + public float blockQuoteStripWidth =10F; + public float blockQuoteGap=20F; @Nullable private ClickableTableSpan clickableTableSpan; @Nullable @@ -92,7 +98,10 @@ public void setHtml(@RawRes int resId, @Nullable Html.ImageGetter imageGetter) { * HtmlLocalImageGetter and HtmlRemoteImageGetter */ public void setHtml(@NonNull String html, @Nullable Html.ImageGetter imageGetter) { - setText(HtmlFormatter.formatHtml(html, imageGetter, clickableTableSpan, drawTableLinkSpan, onClickATagListener,indent, removeTrailingWhiteSpace)); + + Spanned styledText = HtmlFormatter.formatHtml(html, imageGetter, clickableTableSpan, drawTableLinkSpan, onClickATagListener, indent, removeTrailingWhiteSpace); + replaceQuoteSpans(styledText); + setText(styledText); // make links work setMovementMethod(LocalLinkMovementMethod.getInstance()); @@ -155,4 +164,27 @@ private static String convertStreamToString(@NonNull InputStream is) { Scanner s = new Scanner(is).useDelimiter("\\A"); return s.hasNext() ? s.next() : ""; } + + + private void replaceQuoteSpans(Spanned spanned) { + + Spannable spannable = (Spannable) spanned; + QuoteSpan[] quoteSpans = spannable.getSpans(0, spannable.length() - 1, QuoteSpan.class); + for (QuoteSpan quoteSpan : quoteSpans) { + int start = spannable.getSpanStart(quoteSpan); + int end = spannable.getSpanEnd(quoteSpan); + int flags = spannable.getSpanFlags(quoteSpan); + spannable.removeSpan(quoteSpan); + spannable.setSpan(new DesignQuoteSpan( + blockQuoteBackgroundColor, + blockQuoteStripColor, + blockQuoteStripWidth, + blockQuoteGap), + start, + end, + flags); + } + } + + } \ No newline at end of file diff --git a/HtmlTextView/src/main/res/values/colors.xml b/HtmlTextView/src/main/res/values/colors.xml new file mode 100644 index 0000000..813f0c6 --- /dev/null +++ b/HtmlTextView/src/main/res/values/colors.xml @@ -0,0 +1,5 @@ + + + #000000 + #ffff + \ No newline at end of file diff --git a/example/build.gradle b/example/build.gradle index 0bfb6fd..553b6f4 100644 --- a/example/build.gradle +++ b/example/build.gradle @@ -18,5 +18,5 @@ android { dependencies { implementation project(':HtmlTextView') - implementation 'androidx.appcompat:appcompat:1.1.0' + implementation 'androidx.appcompat:appcompat:1.2.0' } diff --git a/example/src/main/java/org/sufficientlysecure/htmltextview/example/DataBindingExampleActivity.java b/example/src/main/java/org/sufficientlysecure/htmltextview/example/DataBindingExampleActivity.java index 3579e61..e1c7eb5 100644 --- a/example/src/main/java/org/sufficientlysecure/htmltextview/example/DataBindingExampleActivity.java +++ b/example/src/main/java/org/sufficientlysecure/htmltextview/example/DataBindingExampleActivity.java @@ -16,10 +16,11 @@ package org.sufficientlysecure.htmltextview.example; import android.app.Activity; -import androidx.databinding.BindingAdapter; -import androidx.databinding.DataBindingUtil; import android.os.Bundle; + import androidx.annotation.Nullable; +import androidx.databinding.BindingAdapter; +import androidx.databinding.DataBindingUtil; import org.sufficientlysecure.htmltextview.DrawTableLinkSpan; import org.sufficientlysecure.htmltextview.HtmlResImageGetter; diff --git a/example/src/main/java/org/sufficientlysecure/htmltextview/example/MainActivity.java b/example/src/main/java/org/sufficientlysecure/htmltextview/example/MainActivity.java index 819313e..8018694 100644 --- a/example/src/main/java/org/sufficientlysecure/htmltextview/example/MainActivity.java +++ b/example/src/main/java/org/sufficientlysecure/htmltextview/example/MainActivity.java @@ -81,6 +81,8 @@ public void onClick(View widget, @Nullable String href) { toast.show(); } }); + textView.blockQuoteBackgroundColor=getResources().getColor(R.color.whitish); + textView.blockQuoteStripColor=getResources().getColor(R.color.blue); textView.setHtml(R.raw.example, new HtmlResImageGetter(getBaseContext())); } diff --git a/example/src/main/res/raw/example.html b/example/src/main/res/raw/example.html index 0019bfd..4eaee21 100644 --- a/example/src/main/res/raw/example.html +++ b/example/src/main/res/raw/example.html @@ -116,7 +116,9 @@

Hello world

aliquam convallis dapibus. Aenean suscipit, orci id elementum vehicula, odio arcu fringilla massa, vel imperdiet augue est non mi.

-

+

+ +

This text is in blockquote TAG

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam ut eros sed arcu auctor tincidunt id sit amet elit. Mauris in faucibus neque. Suspendisse facilisis urna nec nisi convallis tincidunt. Mauris at elit et arcu viverra auctor. Nullam et arcu ultricies, iaculis dolor efficitur, tristique eros. @@ -127,7 +129,7 @@

Hello world

Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Vestibulum aliquam convallis dapibus. Aenean suscipit, orci id elementum vehicula, odio arcu fringilla massa, vel imperdiet augue est non mi. -

+

Android will add extra space at the bottom of the textView by default fromHtml, use setRemoveFromHtmlSpace(true) on your HtmlTextView diff --git a/example/src/main/res/values/colors.xml b/example/src/main/res/values/colors.xml new file mode 100644 index 0000000..07df4af --- /dev/null +++ b/example/src/main/res/values/colors.xml @@ -0,0 +1,5 @@ + + + #03DAC5 + #ccc + \ No newline at end of file