Skip to content

Commit aabeb7f

Browse files
author
Salvatore Ranieri
committed
added builder to manage table header centering
1 parent 90bbdbf commit aabeb7f

3 files changed

Lines changed: 164 additions & 20 deletions

File tree

HtmlSpanner/src/main/java/com/sysdata/htmlspanner/HtmlSpanner.java

Lines changed: 89 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,10 @@ public class HtmlSpanner {
9292

9393
private float textSize;
9494

95-
private boolean textAlignCenter;
95+
/**
96+
* This parameter is used to modify table header text centering, by default is true
97+
*/
98+
private Boolean tableHeaderCenter = null;
9699

97100
/**
98101
* Switch to determine if CSS is used
@@ -177,17 +180,35 @@ public HtmlSpanner(int textColor,float textSize) {
177180
* @param cleaner
178181
*/
179182
public HtmlSpanner(HtmlCleaner cleaner, FontResolver fontResolver,int textColor, float textSize) {
183+
initBaseComponents(cleaner, fontResolver);
184+
setTextColor(textColor);
185+
setTextSize(textSize);
186+
calculateBaseDimensions(textSize);
187+
registerBuiltInHandlers();
188+
}
189+
190+
private HtmlSpanner(Builder builder) {
191+
initBaseComponents(createHtmlCleaner(), new SystemFontResolver());
192+
setTextColor(builder.textColor);
193+
setTextSize(builder.textSize);
194+
setBackgroundColor(builder.backgroundColor);
195+
this.tableHeaderCenter = builder.tableHeaderCenter;
196+
calculateBaseDimensions(textSize);
197+
registerBuiltInHandlers();
198+
}
199+
200+
private void initBaseComponents(HtmlCleaner cleaner, FontResolver fontResolver) {
201+
this.handlers = new HashMap<>();
180202
this.htmlCleaner = cleaner;
181203
this.fontResolver = fontResolver;
182-
this.handlers = new HashMap<>();
183-
this.textColor=textColor;
184-
this.textSize=textSize;
204+
}
205+
206+
private void calculateBaseDimensions(float textSize) {
185207
Paint paint = new Paint();
186208
paint.setTextSize(textSize);
187209
NUMBER_WIDTH = Math.round(paint.measureText("4."));
188210
BULLET_WIDTH = Math.round(paint.measureText("\u2022"));
189211
BLANK_WIDTH = Math.round(paint.measureText(" "));
190-
registerBuiltInHandlers();
191212
}
192213

193214
public FontResolver getFontResolver() {
@@ -218,6 +239,10 @@ public float getTextSize() {
218239
return textSize;
219240
}
220241

242+
public void setTableHeaderCentered(boolean tableHeaderCenter) {
243+
this.tableHeaderCenter = tableHeaderCenter;
244+
}
245+
221246
/**
222247
* Switch to specify whether excess whitespace should be stripped from the
223248
* input.
@@ -542,6 +567,9 @@ private void registerBuiltInHandlers() {
542567
TableHandler tableHandler=new TableHandler();
543568
tableHandler.setTextSize(textSize * 0.83f);
544569
tableHandler.setTextColor(textColor);
570+
if(tableHeaderCenter != null){
571+
tableHandler.setHeaderCentered(tableHeaderCenter);
572+
}
545573
registerHandler("table",tableHandler);
546574

547575
registerHandler("h1", wrap(new HeaderHandler(2f, 0.5f)));
@@ -601,4 +629,60 @@ public static interface CancellationCallback {
601629
boolean isCancelled();
602630
}
603631

632+
/**
633+
* Builder class for {@link HtmlSpanner}
634+
*/
635+
public static final class Builder {
636+
private int backgroundColor;
637+
private int textColor;
638+
private float textSize;
639+
private Boolean tableHeaderCenter = null;
640+
641+
public Builder() {
642+
}
643+
644+
/**
645+
* Set the background color, used for paragraphs and div
646+
* @param value the backgournd color as int
647+
* @return
648+
*/
649+
public Builder backgroundColor(int value) {
650+
backgroundColor = value;
651+
return this;
652+
}
653+
654+
/**
655+
* Set the text color
656+
* @param value the text color as int
657+
* @return
658+
*/
659+
public Builder textColor(int value) {
660+
textColor = value;
661+
return this;
662+
}
663+
664+
/**
665+
* Set the text size
666+
* @param value the text size
667+
* @return
668+
*/
669+
public Builder textSize(float value) {
670+
textSize = value;
671+
return this;
672+
}
673+
674+
/**
675+
* use this method if you want to manage the table header text centering, by default is set to true
676+
* @param value
677+
* @return
678+
*/
679+
public Builder tableHeaderCenter(boolean value) {
680+
tableHeaderCenter = value;
681+
return this;
682+
}
683+
684+
public HtmlSpanner build() {
685+
return new HtmlSpanner(this);
686+
}
687+
}
604688
}

HtmlSpanner/src/main/java/com/sysdata/htmlspanner/handlers/TableHandler.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,16 @@ public class TableHandler extends TagNodeHandler {
5454

5555
private static final int PADDING = 5;
5656
private boolean hasHeader;
57+
private boolean isHeaderCentered = true;
58+
59+
/**
60+
* Set if the header text has to be centered, by default is true
61+
*
62+
* @param headerCentered
63+
*/
64+
public void setHeaderCentered(boolean headerCentered) {
65+
isHeaderCentered = headerCentered;
66+
}
5767

5868
/**
5969
* Sets how wide the table should be.
@@ -117,7 +127,7 @@ private void readNode(Object node, Table table) {
117127
}
118128

119129
if (tagNode.getName().equals("th")) {
120-
hasHeader = true;
130+
hasHeader = isHeaderCentered;
121131
Spanned result = this.getSpanner().fromTagNode(tagNode, null);
122132
table.addCell(result);
123133
return;

app/src/main/java/com/sysdata/kt/sdhtmltextview/MainActivity.kt

Lines changed: 64 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,69 @@ class MainActivity : AppCompatActivity() {
99
override fun onCreate(savedInstanceState: Bundle?) {
1010
super.onCreate(savedInstanceState)
1111
setContentView(R.layout.activity_main)
12-
textView.htmlText="<div style=\"background-color: #272822\"><span style=\"color: #272822\">_</span><span style=\"color: #f8f8f2\">SDRootValidatorConfig</span> <span style=\"color: #f8f8f2\">config</span> <span style=\"color: #f92672\">=</span> <span style=\"color: #f8f8f2\">SDRootValidatorConfig</span>\n<span style=\"color: #272822\">_</span><span style=\"color: #f92672\">.</span><span style=\"color: #a6e22e\">newBuilder</span><span style=\"color: #f92672\">(</span><span style=\"color: #f8f8f2\">getContext</span><span style=\"color: #f92672\">())</span>\n" +
13-
"<span style=\"color: #272822\">_</span><span style=\"color: #f92672\">.</span><span style=\"color: #a6e22e\">isRootCheckEnabled</span><span style=\"color: #f92672\">(</span><span style=\"color: #66d9ef\">true</span><span style=\"color: #f92672\">)</span>\n" +
14-
"<span style=\"color: #272822\">_</span><span style=\"color: #f92672\">.</span><span style=\"color: #a6e22e\">checkForBinaryBusybox</span><span style=\"color: #f92672\">(</span><span style=\"color: #66d9ef\">true</span><span style=\"color: #f92672\">)</span>\n" +
15-
"<span style=\"color: #272822\">_</span><span style=\"color: #f92672\">.</span><span style=\"color: #a6e22e\">checkForBinarySu</span><span style=\"color: #f92672\">(</span><span style=\"color: #66d9ef\">true</span><span style=\"color: #f92672\">)</span>\n" +
16-
"<span style=\"color: #272822\">_</span><span style=\"color: #f92672\">.</span><span style=\"color: #a6e22e\">checkForDangerousProps</span><span style=\"color: #f92672\">(</span><span style=\"color: #66d9ef\">true</span><span style=\"color: #f92672\">)</span>\n" +
17-
"<span style=\"color: #272822\">_</span><span style=\"color: #f92672\">.</span><span style=\"color: #a6e22e\">checkForMagiskBinary</span><span style=\"color: #f92672\">(</span><span style=\"color: #66d9ef\">true</span><span style=\"color: #f92672\">)</span>\n" +
18-
"<span style=\"color: #272822\">_</span><span style=\"color: #f92672\">.</span><span style=\"color: #a6e22e\">checkForRootNative</span><span style=\"color: #f92672\">(</span><span style=\"color: #66d9ef\">true</span><span style=\"color: #f92672\">)</span>\n" +
19-
"<span style=\"color: #272822\">_</span><span style=\"color: #f92672\">.</span><span style=\"color: #a6e22e\">checkForRWPaths</span><span style=\"color: #f92672\">(</span><span style=\"color: #66d9ef\">true</span><span style=\"color: #f92672\">)</span>\n" +
20-
"<span style=\"color: #272822\">_</span><span style=\"color: #f92672\">.</span><span style=\"color: #a6e22e\">checkSuExists</span><span style=\"color: #f92672\">(</span><span style=\"color: #66d9ef\">true</span><span style=\"color: #f92672\">)</span>\n" +
21-
"<span style=\"color: #272822\">_</span><span style=\"color: #f92672\">.</span><span style=\"color: #a6e22e\">detectPotentiallyDangerousApps</span><span style=\"color: #f92672\">(</span><span style=\"color: #66d9ef\">true</span><span style=\"color: #f92672\">)</span>\n" +
22-
"<span style=\"color: #272822\">_</span><span style=\"color: #f92672\">.</span><span style=\"color: #a6e22e\">detectRootManagementApps</span><span style=\"color: #f92672\">(</span><span style=\"color: #66d9ef\">true</span><span style=\"color: #f92672\">)</span>\n" +
23-
"<span style=\"color: #272822\">_</span><span style=\"color: #f92672\">.</span><span style=\"color: #a6e22e\">detectTestKeys</span><span style=\"color: #f92672\">(</span><span style=\"color: #66d9ef\">true</span><span style=\"color: #f92672\">)</span>\n" +
24-
"<span style=\"color: #272822\">_</span><span style=\"color: #f92672\">.</span><span style=\"color: #a6e22e\">build</span><span style=\"color: #f92672\">();</span>\n" +
25-
"</div>\n"
12+
textView.htmlText="<table border = \"1\">" +
13+
"<tr>" +
14+
"<th>Firstname</th>" +
15+
"<th>Lastname</th> " +
16+
"<th>Age</th>" +
17+
"</tr>" +
18+
"<tr>" +
19+
"<td>Jill</td>" +
20+
"<td>Smith</td>" +
21+
"<td>50</td>" +
22+
"</tr>" +
23+
"<tr>" +
24+
"<td>Eve</td>" +
25+
"<td>Jackson</td>" +
26+
"<td>94</td>" +
27+
"</tr>" +
28+
"<tr>" +
29+
"<td>John</td>" +
30+
"<td>Doe</td>" +
31+
"<td>80</td>" +
32+
"</tr>" +
33+
"</table>" +
34+
"<ul>" +
35+
" <li>Coffee</li>" +
36+
" <li>Tea</li>" +
37+
" <li>Milk</li>" +
38+
"</ul>" +
39+
"<table>" +
40+
"<tr>" +
41+
"<th>Firstname</th>" +
42+
"<th>Lastname</th> " +
43+
"<th>Age</th>" +
44+
"</tr>" +
45+
"<tr>" +
46+
"<td>Jill</td>" +
47+
"<td>Smith</td>" +
48+
"<td>50</td>" +
49+
"</tr>" +
50+
"<tr>" +
51+
"<td>Eve</td>" +
52+
"<td>Jackson</td>" +
53+
"<td>94</td>" +
54+
"</tr>" +
55+
"<tr>" +
56+
"<td>John</td>" +
57+
"<td>Doe</td>" +
58+
"<td>80</td>" +
59+
"</tr>" +
60+
"</table>" +
61+
"<h1>The line-height Property</h1>" +
62+
"<h2>line-height: normal (default):</h2>" +
63+
"<div>This is a paragraph with a standard line-height.<br>" +
64+
"The standard line height in most browsers is about 110% to 120%.</div>" +
65+
"<h2>line-height: 1.6 (recommended):</h2>" +
66+
"<div style=\"line-height: 1.6 em\">This is a paragraph with the recommended line-height.<br>" +
67+
"The line height is here set to 1.6. This is a unitless value;<br>" +
68+
"meaning that the line height will be relative to the font size.</div>" +
69+
"<h2>line-height: 80%:</h2>" +
70+
"<div style=\"line-height: 80 %;\">This is a paragraph with a smaller line-height.<br>" +
71+
"The line height is here set to 80%.</div>" +
72+
"<h2>line-height: 50 px:</h2>" +
73+
"<div style=\"line-height: 50 px;\">" +
74+
"This is a paragraph with a bigger line-height.<br>" +
75+
"The line height is here set to 200%.</div>"
2676
}
2777
}

0 commit comments

Comments
 (0)