Skip to content

Commit 989dec7

Browse files
author
Salvatore Ranieri
committed
updated lineheight to support em and percentage values
1 parent aabeb7f commit 989dec7

5 files changed

Lines changed: 36 additions & 15 deletions

File tree

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,8 @@ public void handleTagNode(TagNode node, SpannableStringBuilder builder, int star
109109

110110
//If we have a line height attribute, we apply it to the element
111111
if (useStyle.getLineHeight() != null && useStyle.getBackgroundColor()==null){
112-
StyleValue styleValue = useStyle.getLineHeight();
113-
if ( styleValue.getIntValue() > 0 ) {
114-
stack.pushSpan(new StyleCallback(getSpanner().getFontResolver()
115-
.getDefaultFont(),useStyle,start,builder.length()));
116-
}
112+
stack.pushSpan(new StyleCallback(getSpanner().getFontResolver()
113+
.getDefaultFont(),useStyle,start,builder.length()));
117114
}
118115

119116
if ( builder.length() > start ) {

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,34 @@
44
import android.text.style.LineHeightSpan;
55
import android.util.Log;
66

7+
import com.sysdata.htmlspanner.style.StyleValue;
8+
79
/**
810
* Created by Salvatore on 06/12/2017.
911
*/
1012

1113
public class LineHeightSpanImpl implements LineHeightSpan {
1214

13-
//private final int value;
1415
private int mSize;
15-
private static float sProportion = 0;
16-
private int ascent=0;
17-
private int descent=0;
1816

1917

2018
public LineHeightSpanImpl(int value) {
21-
//this.value = value;
2219
mSize=value;
2320
}
2421

22+
public LineHeightSpanImpl(float value, StyleValue.Unit unit, Float textSize){
23+
switch (unit){
24+
case PX:
25+
mSize = (int) value;
26+
break;
27+
case PERCENTAGE:
28+
case EM:
29+
if(textSize != null && textSize > 0){
30+
mSize = (int) (textSize * value);
31+
}
32+
}
33+
}
34+
2535
@Override
2636
public void chooseHeight(CharSequence text, int start, int end, int spanstartv, int v,
2737
Paint.FontMetricsInt fm) {

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,16 @@ public void applySpan(HtmlSpanner spanner, SpannableStringBuilder builder) {
8585
}
8686

8787
//If there's a line height, we use an implementation of LineHeightSpan to draw space behind the text
88-
if ( useStyle.getLineHeight() != null) {
88+
if (useStyle.getLineHeight() != null) {
8989
//Log.d("StyleCallback", "Applying LineHeightSpan with value " + useStyle.getLineHeight().getIntValue() + " from " + start + " to " + end + " on text " + builder.subSequence(start, end));
90-
builder.setSpan(new LineHeightSpanImpl((int)Math.ceil(useStyle.getLineHeight().getIntValue() * SCREEN_DENSITY)),start,end,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
90+
StyleValue lineHeight = useStyle.getLineHeight();
91+
LineHeightSpanImpl span;
92+
if(lineHeight.getUnit() == StyleValue.Unit.PX){
93+
span = new LineHeightSpanImpl((int) Math.ceil(lineHeight.getIntValue() * SCREEN_DENSITY));
94+
} else {
95+
span = new LineHeightSpanImpl(lineHeight.getFloatValue(), lineHeight.getUnit(), spanner.getTextSize());
96+
}
97+
builder.setSpan(span,start,end,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
9198
}
9299

93100
//If there is a border, the BorderSpan will also draw the background colour if needed.

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,13 @@ public static StyleValue parse( String value ) {
2424
return new StyleValue(0f, Unit.EM);
2525
}
2626

27+
// removing extra spaces
28+
value = value.replaceAll("\\s+","");
29+
2730
if ( value.endsWith("px") ) {
2831

2932
try {
30-
final Integer intValue = Integer.parseInt( value.substring(0, value.length() -2) );
33+
final int intValue = Integer.parseInt( value.substring(0, value.length() -2) );
3134
return new StyleValue(intValue);
3235
} catch (NumberFormatException nfe ) {
3336
Log.e("StyleValue", "Can't parse value: " + value );

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class SDHtmlTextView: TextView {
1414

1515
constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int): super(context, attrs, defStyleAttr)
1616

17-
private var htmlSpanner: HtmlSpanner = HtmlSpanner(textColors.defaultColor, textSize)
17+
private var htmlSpanner: HtmlSpanner
1818

1919
var htmlText:String? = null
2020
set(value) {
@@ -23,7 +23,11 @@ class SDHtmlTextView: TextView {
2323

2424
init {
2525
this.movementMethod = LinkMovementMethod.getInstance()
26-
htmlSpanner.setBackgroundColor(solidColor)
26+
htmlSpanner = HtmlSpanner.Builder()
27+
.textColor(textColors.defaultColor)
28+
.textSize(textSize)
29+
.backgroundColor(solidColor)
30+
.build()
2731
}
2832

2933
}

0 commit comments

Comments
 (0)