Skip to content

Commit a6f699a

Browse files
author
Salvatore Ranieri
committed
updated html text view
updated builder
1 parent c3d4747 commit a6f699a

File tree

3 files changed

+71
-11
lines changed

3 files changed

+71
-11
lines changed

README.md

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,23 @@ If you want to handle href you need to add
9898
tv.setMovementMethod(LinkMovementMethod.getInstance());
9999
```
100100

101+
You can alternatively use a builder to initialize the HtmlSpanner
102+
103+
```java
104+
HtmlSpanner htmlSpanner = new HtmlSpanner.Builder()
105+
.textColor(tv.getCurrentTextColor())
106+
.textSize(tv.getTextSize())
107+
.backgroundColor(tv.getSolidColor())
108+
.tableHeaderCenter(isTableHeaderCentered)
109+
.build();
110+
```
111+
112+
the builder possible parameters are:
113+
- textColor, this attribute is required since is used to initialize the htmlspanner with a custom text color;
114+
- textSize, this attribute is required since is used to initialize the htmlspanner with a custom text size;
115+
- backgroundColor, this attribute is required since is used to initialize the htmlspanner with a custom background color to handle html tags like "div" correctly;
116+
- tableHeaderCenter, if this attribute is set to false or true it will define the centering of the table header fields in tables, by default table header is centered.
117+
101118
2. Alternatively you can use an **SDHtmlTextView** which is a custom TextView that integrates HtmlSpanner to handle html texts :
102119

103120
2.1 **Add the library as a dependency**
@@ -117,7 +134,7 @@ If you want to handle href you need to add
117134

118135
**kotlin:**
119136

120-
```java
137+
```kotlin
121138
String html=loadStringFromAssetFile(this,"example.html")
122139
htmlTextView.htmlText = html
123140
```
@@ -129,6 +146,29 @@ If you want to handle href you need to add
129146
SDHtmlTextView htmlTextView = (SDHtmlTextView) findViewById(R.id.text);
130147
htmlTextView.setHtmlText(html);
131148
```
149+
2.3 if you want to override html table header centering behaviour you need to add this attribute "tableHeaderCentered" you can add via xml
150+
151+
```xml
152+
<com.sysdata.kt.htmltextview.SDHtmlTextView
153+
android:id="@+id/textView"
154+
android:layout_width="wrap_content"
155+
android:layout_height="wrap_content"
156+
app:tableHeaderCentered="false"/>
157+
```
158+
159+
or in the code in this way
160+
161+
**kotlin:**
162+
163+
```kotlin
164+
textView.isTableHeaderCentered = false
165+
```
166+
167+
**java:**
168+
169+
```java
170+
textView.setTableHeaderCentered(false)
171+
```
132172

133173
## License
134174
Copyright (C) 2017 Sysdata S.p.A.

htmltextview/src/main/java/com/sysdata/kt/htmltextview/SDHtmlTextView.kt

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,39 @@ import com.sysdata.htmlspanner.HtmlSpanner
88

99
class SDHtmlTextView: TextView {
1010

11-
constructor(context: Context?):super(context, null)
11+
constructor(context: Context?):this(context, null)
1212

13-
constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs, 0)
13+
constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)
1414

15-
constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int): super(context, attrs, defStyleAttr)
15+
constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int): super(context, attrs, defStyleAttr){
16+
if(attrs != null) {
17+
context?.let { ctx ->
18+
val a = ctx.theme.obtainStyledAttributes(
19+
attrs,
20+
R.styleable.SDHtmlTextView,
21+
defStyleAttr, 0)
1622

17-
private var htmlSpanner: HtmlSpanner
23+
isTableHeaderCentered = a.getBoolean(R.styleable.SDHtmlTextView_tableHeaderCentered, true)
1824

19-
var htmlText:String? = null
20-
set(value) {
21-
text = htmlSpanner.fromHtml(value)
22-
}
23-
24-
init {
25+
a.recycle()
26+
}
27+
}
2528
this.movementMethod = LinkMovementMethod.getInstance()
29+
2630
htmlSpanner = HtmlSpanner.Builder()
2731
.textColor(textColors.defaultColor)
2832
.textSize(textSize)
2933
.backgroundColor(solidColor)
34+
.tableHeaderCenter(isTableHeaderCentered)
3035
.build()
3136
}
3237

38+
private var htmlSpanner: HtmlSpanner
39+
public var isTableHeaderCentered: Boolean = true
40+
41+
var htmlText:String? = null
42+
set(value) {
43+
text = htmlSpanner.fromHtml(value)
44+
}
45+
3346
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
4+
<declare-styleable name="SDHtmlTextView">
5+
<attr name="tableHeaderCentered" format="boolean"/>
6+
</declare-styleable>
7+
</resources>

0 commit comments

Comments
 (0)