Skip to content

Commit 44db9fb

Browse files
authored
Merge pull request #9 from SysdataSpA/feature/list_fix
updated list item span with variable width of numbers and bullets
2 parents 743d805 + 04d03e3 commit 44db9fb

4 files changed

Lines changed: 33 additions & 8 deletions

File tree

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.sysdata.htmlspanner;
1818

19+
import android.graphics.Paint;
1920
import android.text.Spannable;
2021
import android.text.SpannableStringBuilder;
2122
import android.text.TextUtils;
@@ -72,6 +73,9 @@ public class HtmlSpanner {
7273
* Used for calculating margins.
7374
*/
7475
public static final int HORIZONTAL_EM_WIDTH = 10;
76+
public static int NUMBER_WIDTH = 5;
77+
public static int BULLET_WIDTH = 3;
78+
public static int BLANK_WIDTH = 10;
7579

7680

7781
private Map<String, TagNodeHandler> handlers;
@@ -175,9 +179,15 @@ public HtmlSpanner(int textColor,float textSize) {
175179
public HtmlSpanner(HtmlCleaner cleaner, FontResolver fontResolver,int textColor, float textSize) {
176180
this.htmlCleaner = cleaner;
177181
this.fontResolver = fontResolver;
178-
this.handlers = new HashMap<String, TagNodeHandler>();
182+
this.handlers = new HashMap<>();
179183
this.textColor=textColor;
180184
this.textSize=textSize;
185+
Paint paint = new Paint();
186+
paint.setTextSize(textSize);
187+
NUMBER_WIDTH = Math.round(paint.measureText("4."));
188+
if(NUMBER_WIDTH <= 0)
189+
BULLET_WIDTH = Math.round(paint.measureText("\u2022"));
190+
BLANK_WIDTH = Math.round(paint.measureText(" "));
181191
registerBuiltInHandlers();
182192
}
183193

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
1616

1717
package com.sysdata.htmlspanner.spans;
1818

19+
import android.app.Notification;
1920
import android.graphics.Canvas;
2021
import android.graphics.Paint;
2122
import android.text.Layout;
2223
import android.text.Spanned;
24+
import android.text.style.BulletSpan;
2325
import android.text.style.LeadingMarginSpan;
2426
import com.sysdata.htmlspanner.HtmlSpanner;
2527

@@ -36,11 +38,11 @@
3638
public class ListItemSpan implements LeadingMarginSpan {
3739
private final int mNumber;
3840

39-
private static final int BULLET_RADIUS = 3;
40-
private static final int NUMBER_RADIUS = 5;
41+
private static final int BULLET_RADIUS = HtmlSpanner.BULLET_WIDTH;
42+
private static final int NUMBER_RADIUS = HtmlSpanner.NUMBER_WIDTH;
4143

4244
//Gap should be about 1em
43-
public static final int STANDARD_GAP_WIDTH = HtmlSpanner.HORIZONTAL_EM_WIDTH;
45+
public static final int STANDARD_GAP_WIDTH = HtmlSpanner.BLANK_WIDTH;
4446

4547
public ListItemSpan() {
4648
mNumber = -1;
@@ -52,9 +54,9 @@ public ListItemSpan(int number) {
5254

5355
public int getLeadingMargin(boolean first) {
5456
if (mNumber != -1) {
55-
return 2 * (NUMBER_RADIUS + STANDARD_GAP_WIDTH);
57+
return (NUMBER_RADIUS + STANDARD_GAP_WIDTH);
5658
} else {
57-
return 2 * (BULLET_RADIUS + STANDARD_GAP_WIDTH);
59+
return (BULLET_RADIUS + STANDARD_GAP_WIDTH);
5860
}
5961
}
6062

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,19 @@ class MainActivity : AppCompatActivity() {
99
override fun onCreate(savedInstanceState: Bundle?) {
1010
super.onCreate(savedInstanceState)
1111
setContentView(R.layout.activity_main)
12-
textView.htmlText="<h1>That's a header 1</h1><h2>That's a header 2</h2><h3>That's a header 3</h3><h4>That's a header 4</h4><h5>That's a header 5</h5><h6>That's a header 6</h6>NomeCognome<br />MarioRossi<div style=\"line-height:30px\">Some text displayed in center position into a paragraph <span style=\"background-color:blue; color:white\">other text</span> (<code>span</code> markup used);<br /><big>big thing</big> (<code>big</code> markup used)<br /><small>this is not that important</small> (<code>small</code> markup used)<br />this is <em>very</em> simple (<code>em</code> markup used for emphasizing a word)<br /><cite>Origin of Species</cite> (a book title;<code>cite</code> markup used)<br /><em>Homo sapiens</em> (should appear in italics; <code>i</code> markup used)<br /><br /><strong>bolded</strong> (<code>b</code> markup used - just bolding with unspecified semantics)<div style=\"line-height:24px\"><br />&nbsp;</div><tt>text in monospace font</tt> (<code>tt</code> markup used)<br /><u>underlined</u> text (<code>u</code> markup used)<br /><strong>bolded</strong> (<code>strong</code> markup used)<ul><li>Test: <span>large size</span> (<code>font size=30px</code> markup used)</li><li>Test: <span style=\"color:red\">red text</span> (<code>font color=red</code> markup used)</li></ul>a<sub>B</sub> (<code>sub</code> markup used)<br />a<sup>C</sup> (<code>sup</code> markup used)</div><ol style=\"list-style-position:inside;\"><li>list element 1</li><li>list element 2</li></ol><div style=\"line-height:50px\">that's a div body and a line-height<br />with a <code>br</code> inside and a font styling<br /><span style=\"font-size:30px\">large size</span> (<code>style=&quot;font-size:30px&quot;</code> markup used)<br /><span style=\"font-family:Courier\">Courier font</span> (<code>style=&quot;font-family:Courier&quot;</code> markup used)<br /><span style=\"color:red\">red text</span> (<code>style=&quot;color:red&quot;</code> markup used)<br /><em>italic text</em> (<code>style=&quot;font-style:italic&quot;</code> markup used)<p style=\"text-align:left\">left text</p><p style=\"text-align:center\">centered text</p><p style=\"text-align:right\">right text</p>deleted text</div>"
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"
1326
}
1427
}

app/src/main/res/layout/activity_main.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@
1313
app:layout_constraintBottom_toBottomOf="parent"
1414
app:layout_constraintLeft_toLeftOf="parent"
1515
app:layout_constraintRight_toRightOf="parent"
16-
app:layout_constraintTop_toTopOf="parent" />
16+
app:layout_constraintTop_toTopOf="parent"/>
1717

1818
</android.support.constraint.ConstraintLayout>

0 commit comments

Comments
 (0)