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

Commit

Permalink
Merge pull request #189 from sunny52525/blockquote-style
Browse files Browse the repository at this point in the history
added blockquote styling feature
  • Loading branch information
dschuermann authored Sep 7, 2020
2 parents a522a91 + 222765f commit bb5fc08
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 8 deletions.
3 changes: 2 additions & 1 deletion HtmlTextView/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ publish {

dependencies {
implementation 'androidx.annotation:annotation:1.1.0'
}

}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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);
}
}


}
5 changes: 5 additions & 0 deletions HtmlTextView/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="black">#000000</color>
<color name="White">#ffff</color>
</resources>
2 changes: 1 addition & 1 deletion example/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ android {

dependencies {
implementation project(':HtmlTextView')
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.appcompat:appcompat:1.2.0'
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
Expand Down
6 changes: 4 additions & 2 deletions example/src/main/res/raw/example.html
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ <h2>Hello world</h2>
aliquam convallis dapibus. Aenean suscipit, orci id elementum vehicula, odio arcu fringilla massa,
vel imperdiet augue est non mi.
</p>
<p>
<blockquote>

<h4>This text is in blockquote TAG</h4>
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.
Expand All @@ -127,7 +129,7 @@ <h2>Hello world</h2>
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.
</p>
</blockquote>
<p>
Android will add extra space at the bottom of the textView by default fromHtml,
use <code>setRemoveFromHtmlSpace(true)</code> on your <b>HtmlTextView</b>
Expand Down
5 changes: 5 additions & 0 deletions example/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="blue">#03DAC5</color>
<color name="whitish">#ccc</color>
</resources>

0 comments on commit bb5fc08

Please sign in to comment.