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

Commit bb5fc08

Browse files
authored
Merge pull request #189 from sunny52525/blockquote-style
added blockquote styling feature
2 parents a522a91 + 222765f commit bb5fc08

File tree

9 files changed

+129
-8
lines changed

9 files changed

+129
-8
lines changed

HtmlTextView/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ publish {
2222

2323
dependencies {
2424
implementation 'androidx.annotation:annotation:1.1.0'
25-
}
25+
26+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package org.sufficientlysecure.htmltextview;
2+
3+
import android.graphics.Canvas;
4+
import android.graphics.Paint;
5+
import android.text.Layout;
6+
import android.text.style.LeadingMarginSpan;
7+
import android.text.style.LineBackgroundSpan;
8+
9+
import androidx.annotation.NonNull;
10+
11+
public class DesignQuoteSpan implements LeadingMarginSpan, LineBackgroundSpan {
12+
13+
private int backgroundColor,stripColor;
14+
private float stripeWidth,gap;
15+
DesignQuoteSpan(int backgroundColor,
16+
int stripColor,
17+
float stripWidth,
18+
float gap){
19+
20+
this.backgroundColor=backgroundColor;
21+
this.stripColor=stripColor;
22+
this.stripeWidth=stripWidth;
23+
this.gap=gap;
24+
25+
}
26+
27+
@Override
28+
public int getLeadingMargin(boolean first) {
29+
return (int)(stripeWidth + gap);
30+
}
31+
32+
@Override
33+
public void drawLeadingMargin(Canvas c,
34+
Paint p,
35+
int x,
36+
int dir,
37+
int top,
38+
int baseline,
39+
int bottom,
40+
CharSequence text,
41+
int start,
42+
int end,
43+
boolean first,
44+
Layout layout) {
45+
46+
Paint.Style style=p.getStyle();
47+
int paintColor=p.getColor();
48+
p.setStyle(Paint.Style.FILL);
49+
p.setColor(stripColor);
50+
c.drawRect((float)x,(float)top,x+dir * stripeWidth,(float)bottom,p);
51+
p.setStyle(style);
52+
p.setColor(paintColor);
53+
54+
}
55+
56+
@Override
57+
public void drawBackground(@NonNull Canvas canvas,
58+
@NonNull Paint paint,
59+
int left,
60+
int right,
61+
int top,
62+
int baseline,
63+
int bottom,
64+
@NonNull CharSequence text,
65+
int start,
66+
int end,
67+
int lineNumber) {
68+
int paintColor=paint.getColor();
69+
paint.setColor(backgroundColor);
70+
canvas.drawRect((float)left,(float)top,(float)right,(float)bottom,paint);
71+
paint.setColor(paintColor);
72+
}
73+
}

HtmlTextView/src/main/java/org/sufficientlysecure/htmltextview/HtmlTextView.java

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818

1919
import android.content.Context;
2020
import android.text.Html;
21+
import android.text.Spannable;
22+
import android.text.Spanned;
23+
import android.text.style.QuoteSpan;
2124
import android.util.AttributeSet;
2225

2326
import androidx.annotation.NonNull;
@@ -31,7 +34,10 @@ public class HtmlTextView extends JellyBeanSpanFixTextView {
3134

3235
public static final String TAG = "HtmlTextView";
3336
public static final boolean DEBUG = false;
34-
37+
public int blockQuoteBackgroundColor= getResources().getColor(R.color.White);
38+
public int blockQuoteStripColor= getResources().getColor(R.color.black);
39+
public float blockQuoteStripWidth =10F;
40+
public float blockQuoteGap=20F;
3541
@Nullable
3642
private ClickableTableSpan clickableTableSpan;
3743
@Nullable
@@ -92,7 +98,10 @@ public void setHtml(@RawRes int resId, @Nullable Html.ImageGetter imageGetter) {
9298
* HtmlLocalImageGetter and HtmlRemoteImageGetter
9399
*/
94100
public void setHtml(@NonNull String html, @Nullable Html.ImageGetter imageGetter) {
95-
setText(HtmlFormatter.formatHtml(html, imageGetter, clickableTableSpan, drawTableLinkSpan, onClickATagListener,indent, removeTrailingWhiteSpace));
101+
102+
Spanned styledText = HtmlFormatter.formatHtml(html, imageGetter, clickableTableSpan, drawTableLinkSpan, onClickATagListener, indent, removeTrailingWhiteSpace);
103+
replaceQuoteSpans(styledText);
104+
setText(styledText);
96105

97106
// make links work
98107
setMovementMethod(LocalLinkMovementMethod.getInstance());
@@ -155,4 +164,27 @@ private static String convertStreamToString(@NonNull InputStream is) {
155164
Scanner s = new Scanner(is).useDelimiter("\\A");
156165
return s.hasNext() ? s.next() : "";
157166
}
167+
168+
169+
private void replaceQuoteSpans(Spanned spanned) {
170+
171+
Spannable spannable = (Spannable) spanned;
172+
QuoteSpan[] quoteSpans = spannable.getSpans(0, spannable.length() - 1, QuoteSpan.class);
173+
for (QuoteSpan quoteSpan : quoteSpans) {
174+
int start = spannable.getSpanStart(quoteSpan);
175+
int end = spannable.getSpanEnd(quoteSpan);
176+
int flags = spannable.getSpanFlags(quoteSpan);
177+
spannable.removeSpan(quoteSpan);
178+
spannable.setSpan(new DesignQuoteSpan(
179+
blockQuoteBackgroundColor,
180+
blockQuoteStripColor,
181+
blockQuoteStripWidth,
182+
blockQuoteGap),
183+
start,
184+
end,
185+
flags);
186+
}
187+
}
188+
189+
158190
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<color name="black">#000000</color>
4+
<color name="White">#ffff</color>
5+
</resources>

example/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ android {
1818

1919
dependencies {
2020
implementation project(':HtmlTextView')
21-
implementation 'androidx.appcompat:appcompat:1.1.0'
21+
implementation 'androidx.appcompat:appcompat:1.2.0'
2222
}

example/src/main/java/org/sufficientlysecure/htmltextview/example/DataBindingExampleActivity.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@
1616
package org.sufficientlysecure.htmltextview.example;
1717

1818
import android.app.Activity;
19-
import androidx.databinding.BindingAdapter;
20-
import androidx.databinding.DataBindingUtil;
2119
import android.os.Bundle;
20+
2221
import androidx.annotation.Nullable;
22+
import androidx.databinding.BindingAdapter;
23+
import androidx.databinding.DataBindingUtil;
2324

2425
import org.sufficientlysecure.htmltextview.DrawTableLinkSpan;
2526
import org.sufficientlysecure.htmltextview.HtmlResImageGetter;

example/src/main/java/org/sufficientlysecure/htmltextview/example/MainActivity.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ public void onClick(View widget, @Nullable String href) {
8181
toast.show();
8282
}
8383
});
84+
textView.blockQuoteBackgroundColor=getResources().getColor(R.color.whitish);
85+
textView.blockQuoteStripColor=getResources().getColor(R.color.blue);
8486

8587
textView.setHtml(R.raw.example, new HtmlResImageGetter(getBaseContext()));
8688
}

example/src/main/res/raw/example.html

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,9 @@ <h2>Hello world</h2>
116116
aliquam convallis dapibus. Aenean suscipit, orci id elementum vehicula, odio arcu fringilla massa,
117117
vel imperdiet augue est non mi.
118118
</p>
119-
<p>
119+
<blockquote>
120+
121+
<h4>This text is in blockquote TAG</h4>
120122
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam ut eros sed arcu auctor tincidunt
121123
id sit amet elit. Mauris in faucibus neque. Suspendisse facilisis urna nec nisi convallis tincidunt.
122124
Mauris at elit et arcu viverra auctor. Nullam et arcu ultricies, iaculis dolor efficitur, tristique eros.
@@ -127,7 +129,7 @@ <h2>Hello world</h2>
127129
Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Vestibulum
128130
aliquam convallis dapibus. Aenean suscipit, orci id elementum vehicula, odio arcu fringilla massa,
129131
vel imperdiet augue est non mi.
130-
</p>
132+
</blockquote>
131133
<p>
132134
Android will add extra space at the bottom of the textView by default fromHtml,
133135
use <code>setRemoveFromHtmlSpace(true)</code> on your <b>HtmlTextView</b>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<color name="blue">#03DAC5</color>
4+
<color name="whitish">#ccc</color>
5+
</resources>

0 commit comments

Comments
 (0)