From b8fc5536a50993c6cae973bc089d37892dc88597 Mon Sep 17 00:00:00 2001 From: Sunny <30768018+sunny52525@users.noreply.github.com> Date: Tue, 25 Aug 2020 00:50:17 +0530 Subject: [PATCH 1/2] added blockquote styling feature --- HtmlTextView/build.gradle | 2 +- .../htmltextview/DesignQuoteSpan.java | 79 +++++++++++++++++++ .../htmltextview/HtmlTextView.java | 38 ++++++++- HtmlTextView/src/main/res/values/colors.xml | 5 ++ example/build.gradle | 2 +- .../example/DataBindingExampleActivity.java | 5 +- .../htmltextview/example/MainActivity.java | 2 + example/src/main/res/raw/example.html | 6 +- example/src/main/res/values/colors.xml | 5 ++ 9 files changed, 136 insertions(+), 8 deletions(-) create mode 100644 HtmlTextView/src/main/java/org/sufficientlysecure/htmltextview/DesignQuoteSpan.java create mode 100644 HtmlTextView/src/main/res/values/colors.xml create mode 100644 example/src/main/res/values/colors.xml diff --git a/HtmlTextView/build.gradle b/HtmlTextView/build.gradle index 385e793..48d677d 100644 --- a/HtmlTextView/build.gradle +++ b/HtmlTextView/build.gradle @@ -22,4 +22,4 @@ 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..c43a354 --- /dev/null +++ b/HtmlTextView/src/main/java/org/sufficientlysecure/htmltextview/DesignQuoteSpan.java @@ -0,0 +1,79 @@ +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 android.util.Log; + +import androidx.annotation.NonNull; + +import static org.sufficientlysecure.htmltextview.HtmlTextView.TAG; + +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; + Log.d(TAG, "DesignQuoteSpanClass: Called"); + + } + + @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) { + Log.d(TAG, "drawLeadingMargin: Called"); + + 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) { + Log.d(TAG, "drawBackground: Called"); + 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..00be0c9 100644 --- a/HtmlTextView/src/main/java/org/sufficientlysecure/htmltextview/HtmlTextView.java +++ b/HtmlTextView/src/main/java/org/sufficientlysecure/htmltextview/HtmlTextView.java @@ -18,7 +18,11 @@ 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 android.util.Log; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -31,7 +35,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 +99,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 +165,28 @@ 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); + Log.d(TAG, "replaceQuoteSpans: " + spannable); + 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 From 222765fc953dbd233436d696ab1493fbba8bc7dc Mon Sep 17 00:00:00 2001 From: Sunny <30768018+sunny52525@users.noreply.github.com> Date: Tue, 25 Aug 2020 01:03:38 +0530 Subject: [PATCH 2/2] removed logs --- HtmlTextView/build.gradle | 1 + .../sufficientlysecure/htmltextview/DesignQuoteSpan.java | 6 ------ .../org/sufficientlysecure/htmltextview/HtmlTextView.java | 2 -- 3 files changed, 1 insertion(+), 8 deletions(-) diff --git a/HtmlTextView/build.gradle b/HtmlTextView/build.gradle index 48d677d..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 index c43a354..8b08784 100644 --- a/HtmlTextView/src/main/java/org/sufficientlysecure/htmltextview/DesignQuoteSpan.java +++ b/HtmlTextView/src/main/java/org/sufficientlysecure/htmltextview/DesignQuoteSpan.java @@ -5,12 +5,9 @@ import android.text.Layout; import android.text.style.LeadingMarginSpan; import android.text.style.LineBackgroundSpan; -import android.util.Log; import androidx.annotation.NonNull; -import static org.sufficientlysecure.htmltextview.HtmlTextView.TAG; - public class DesignQuoteSpan implements LeadingMarginSpan, LineBackgroundSpan { private int backgroundColor,stripColor; @@ -24,7 +21,6 @@ public class DesignQuoteSpan implements LeadingMarginSpan, LineBackgroundSpan { this.stripColor=stripColor; this.stripeWidth=stripWidth; this.gap=gap; - Log.d(TAG, "DesignQuoteSpanClass: Called"); } @@ -46,7 +42,6 @@ public void drawLeadingMargin(Canvas c, int end, boolean first, Layout layout) { - Log.d(TAG, "drawLeadingMargin: Called"); Paint.Style style=p.getStyle(); int paintColor=p.getColor(); @@ -70,7 +65,6 @@ public void drawBackground(@NonNull Canvas canvas, int start, int end, int lineNumber) { - Log.d(TAG, "drawBackground: Called"); int paintColor=paint.getColor(); paint.setColor(backgroundColor); canvas.drawRect((float)left,(float)top,(float)right,(float)bottom,paint); diff --git a/HtmlTextView/src/main/java/org/sufficientlysecure/htmltextview/HtmlTextView.java b/HtmlTextView/src/main/java/org/sufficientlysecure/htmltextview/HtmlTextView.java index 00be0c9..9f6cb26 100644 --- a/HtmlTextView/src/main/java/org/sufficientlysecure/htmltextview/HtmlTextView.java +++ b/HtmlTextView/src/main/java/org/sufficientlysecure/htmltextview/HtmlTextView.java @@ -22,7 +22,6 @@ import android.text.Spanned; import android.text.style.QuoteSpan; import android.util.AttributeSet; -import android.util.Log; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -171,7 +170,6 @@ private void replaceQuoteSpans(Spanned spanned) { Spannable spannable = (Spannable) spanned; QuoteSpan[] quoteSpans = spannable.getSpans(0, spannable.length() - 1, QuoteSpan.class); - Log.d(TAG, "replaceQuoteSpans: " + spannable); for (QuoteSpan quoteSpan : quoteSpans) { int start = spannable.getSpanStart(quoteSpan); int end = spannable.getSpanEnd(quoteSpan);