From b16d2d50be1ba814c5648e8dd5f3e5a7c2bc2d9b Mon Sep 17 00:00:00 2001 From: SilverKnife Date: Tue, 21 Jul 2020 10:17:18 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E6=96=B0=E5=A2=9Espan=E6=94=AF=E6=8C=81?= =?UTF-8?q?=EF=BC=8C=E6=96=B0=E5=A2=9Estyle=E6=94=AF=E6=8C=81=EF=BC=8C?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=AD=97=E4=BD=93=E8=83=8C=E6=99=AF=E8=89=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 4 ++- gradle/wrapper/gradle-wrapper.properties | 4 +-- .../java/me/wcy/htmltext/HtmlTagHandler.java | 30 ++++++++++++++-- sample/build.gradle | 8 ++--- .../me/wcy/htmltext/sample/MainActivity.java | 2 ++ sample/src/main/res/raw/sample.html | 35 ++----------------- 6 files changed, 40 insertions(+), 43 deletions(-) diff --git a/build.gradle b/build.gradle index 412eb10..f7a8c0a 100644 --- a/build.gradle +++ b/build.gradle @@ -3,9 +3,10 @@ buildscript { repositories { jcenter() + google() } dependencies { - classpath 'com.android.tools.build:gradle:2.3.3' + classpath 'com.android.tools.build:gradle:4.0.0' classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5' } } @@ -13,6 +14,7 @@ buildscript { allprojects { repositories { jcenter() + google() } } diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index e29e483..d3b2a41 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Thu May 25 10:41:46 CST 2017 +#Tue Jul 21 09:34:40 CST 2020 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip diff --git a/htmltext/src/main/java/me/wcy/htmltext/HtmlTagHandler.java b/htmltext/src/main/java/me/wcy/htmltext/HtmlTagHandler.java index bcc31c0..67a8194 100644 --- a/htmltext/src/main/java/me/wcy/htmltext/HtmlTagHandler.java +++ b/htmltext/src/main/java/me/wcy/htmltext/HtmlTagHandler.java @@ -29,6 +29,7 @@ import android.text.TextPaint; import android.text.style.AbsoluteSizeSpan; import android.text.style.AlignmentSpan; +import android.text.style.BackgroundColorSpan; import android.text.style.BulletSpan; import android.text.style.ForegroundColorSpan; import android.text.style.LeadingMarginSpan; @@ -56,6 +57,7 @@ class HtmlTagHandler implements Html.TagHandler { private static final String LIST_ITEM = "HTML_TEXT_TAG_LI"; private static final String FONT = "HTML_TEXT_TAG_FONT"; private static final String DIV = "HTML_TEXT_TAG_DIV"; + private static final String SPAN = "HTML_TEXT_TAG_SPAN"; private Context mContext; private TextPaint mTextPaint; @@ -108,6 +110,8 @@ String overrideTags(String html) { html = html.replace("", ""); html = html.replace("", ""); + html = html.replace("", ""); return html; } @@ -136,6 +140,8 @@ public void handleTag(final boolean opening, final String tag, Editable output, } } else if (tag.equalsIgnoreCase(FONT)) { startFont(output, xmlReader); + } else if (tag.equalsIgnoreCase(SPAN)) { + startFont(output, xmlReader); } else if (tag.equalsIgnoreCase(DIV)) { handleDiv(output); } else if (tag.equalsIgnoreCase("code")) { @@ -195,6 +201,8 @@ public void handleTag(final boolean opening, final String tag, Editable output, } } else if (tag.equalsIgnoreCase(FONT)) { endFont(output); + } else if (tag.equalsIgnoreCase(SPAN)) { + endFont(output); } else if (tag.equalsIgnoreCase(DIV)) { handleDiv(output); } else if (tag.equalsIgnoreCase("code")) { @@ -238,10 +246,12 @@ private static class Td { } private static class Font { + public String background_color; public String color; public String size; - public Font(String color, String size) { + public Font(String background_color, String color, String size) { + this.background_color = background_color; this.color = color; this.size = size; } @@ -283,9 +293,10 @@ private void end(Editable output, Class kind, boolean paragraphStyle, Object... private void startFont(Editable output, XMLReader xmlReader) { int len = output.length(); Map attributes = getAttributes(xmlReader); + String background_color = attributes.get("background-color"); String color = attributes.get("color"); String size = attributes.get("size"); - output.setSpan(new Font(color, size), len, len, Spannable.SPAN_MARK_MARK); + output.setSpan(new Font(background_color, color, size), len, len, Spannable.SPAN_MARK_MARK); } private void endFont(Editable output) { @@ -297,9 +308,13 @@ private void endFont(Editable output) { if (where != len) { Font f = (Font) obj; + int background_color = parseColor(f.background_color); int color = parseColor(f.color); int size = parseSize(f.size); + if (background_color != -1) { + output.setSpan(new BackgroundColorSpan(background_color | 0xFF000000), where, len, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + } if (color != -1) { output.setSpan(new ForegroundColorSpan(color | 0xFF000000), where, len, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } @@ -342,8 +357,17 @@ private HashMap getAttributes(XMLReader xmlReader) { * This is as tight as things can get :) * The data index is "just" where the keys and values are stored. */ - for (int i = 0; i < len; i++) + for (int i = 0; i < len; i++) { + if ("style".equals(data[i * 5 + 1])) { + String[] styleAttrs = data[i * 5 + 4].split(";"); + int styleLen = styleAttrs.length; + for (int j = 0; j < styleLen; j++) { + String[] styleAttr = styleAttrs[j].split(":"); + attributes.put(styleAttr[0].trim(), styleAttr[1].trim()); + } + } attributes.put(data[i * 5 + 1], data[i * 5 + 4]); + } } catch (Exception ignored) { } return attributes; diff --git a/sample/build.gradle b/sample/build.gradle index 0159701..7237ccd 100644 --- a/sample/build.gradle +++ b/sample/build.gradle @@ -30,8 +30,8 @@ android { } dependencies { - compile fileTree(dir: 'libs', include: ['*.jar']) - compile project(':htmltext') - compile 'com.android.support:appcompat-v7:25.3.1' - compile 'com.github.bumptech.glide:glide:3.7.0' + implementation fileTree(dir: 'libs', include: ['*.jar']) + implementation project(':htmltext') + implementation 'com.android.support:appcompat-v7:25.4.0' + implementation 'com.github.bumptech.glide:glide:3.7.0' } diff --git a/sample/src/main/java/me/wcy/htmltext/sample/MainActivity.java b/sample/src/main/java/me/wcy/htmltext/sample/MainActivity.java index 21497e0..a6cdd01 100644 --- a/sample/src/main/java/me/wcy/htmltext/sample/MainActivity.java +++ b/sample/src/main/java/me/wcy/htmltext/sample/MainActivity.java @@ -10,6 +10,7 @@ import android.support.v7.app.AppCompatActivity; import android.text.method.LinkMovementMethod; import android.util.DisplayMetrics; +import android.util.Log; import android.widget.TextView; import android.widget.Toast; @@ -38,6 +39,7 @@ protected void onCreate(Bundle savedInstanceState) { textView = (TextView) findViewById(R.id.text); textView.setMovementMethod(LinkMovementMethod.getInstance()); String sample = getSample(); + Log.d("debug",sample); HtmlText.from(sample) .setImageLoader(new HtmlImageLoader() { @Override diff --git a/sample/src/main/res/raw/sample.html b/sample/src/main/res/raw/sample.html index ac3edd8..cf8ec9e 100644 --- a/sample/src/main/res/raw/sample.html +++ b/sample/src/main/res/raw/sample.html @@ -1,33 +1,2 @@ -

Hello world

-

Font size

-
    -
  • Blog
  • -
  • Github, welcome to star or fork, - if you have issues, please tell me. -
  • -
-
-
    -
  1. first
  2. -
  3. second -
      -
    1. second - first -
      - newline -
    2. -
    -
  4. -
-
- -
-

- With billions of Android devices around the world, Android has surpassed our wildest - expectations. Today at Google I/O, we showcased a number of ways we’re pushing - Android forward, with the - O Release, new - tools for developers to help create more performant apps, and an early preview of a - project we call Android Go -- a new experience that we’re building for entry-level - devices. -

\ No newline at end of file +

大萨达112

+

大萨达112

From b483f0424bcaf322174b62203552172f57c77d34 Mon Sep 17 00:00:00 2001 From: SilverKnife Date: Tue, 21 Jul 2020 17:05:01 +0800 Subject: [PATCH 2/5] =?UTF-8?q?data-mce-style=E6=A3=80=E6=B5=8B=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/me/wcy/htmltext/HtmlTagHandler.java | 46 +++++++++++++++---- .../me/wcy/htmltext/sample/MainActivity.java | 1 - sample/src/main/res/layout/activity_main.xml | 4 +- sample/src/main/res/raw/sample.html | 3 +- 4 files changed, 41 insertions(+), 13 deletions(-) diff --git a/htmltext/src/main/java/me/wcy/htmltext/HtmlTagHandler.java b/htmltext/src/main/java/me/wcy/htmltext/HtmlTagHandler.java index 67a8194..7c8293e 100644 --- a/htmltext/src/main/java/me/wcy/htmltext/HtmlTagHandler.java +++ b/htmltext/src/main/java/me/wcy/htmltext/HtmlTagHandler.java @@ -35,6 +35,8 @@ import android.text.style.LeadingMarginSpan; import android.text.style.StrikethroughSpan; import android.text.style.TypefaceSpan; +import android.text.style.UnderlineSpan; +import android.util.Log; import android.widget.TextView; import org.xml.sax.XMLReader; @@ -249,11 +251,15 @@ private static class Font { public String background_color; public String color; public String size; + public String text_decoration; + public String text_align; - public Font(String background_color, String color, String size) { + public Font(String background_color, String color, String size, String text_decoration, String text_align) { this.background_color = background_color; this.color = color; this.size = size; + this.text_decoration = text_decoration; + this.text_align = text_align; } } @@ -295,15 +301,16 @@ private void startFont(Editable output, XMLReader xmlReader) { Map attributes = getAttributes(xmlReader); String background_color = attributes.get("background-color"); String color = attributes.get("color"); - String size = attributes.get("size"); - output.setSpan(new Font(background_color, color, size), len, len, Spannable.SPAN_MARK_MARK); + String size = attributes.get("font-size"); + String text_decoration = attributes.get("text-decoration"); + String text_align = attributes.get("text-align"); + output.setSpan(new Font(background_color, color, size, text_decoration, text_align), len, len, Spannable.SPAN_MARK_MARK); } private void endFont(Editable output) { int len = output.length(); Object obj = getLast(output, Font.class); int where = output.getSpanStart(obj); - output.removeSpan(obj); if (where != len) { @@ -311,7 +318,8 @@ private void endFont(Editable output) { int background_color = parseColor(f.background_color); int color = parseColor(f.color); int size = parseSize(f.size); - + String text_decoration = f.text_decoration; + String text_align = f.text_align; if (background_color != -1) { output.setSpan(new BackgroundColorSpan(background_color | 0xFF000000), where, len, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } @@ -319,8 +327,15 @@ private void endFont(Editable output) { output.setSpan(new ForegroundColorSpan(color | 0xFF000000), where, len, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } if (size > 0) { + Log.d("font-size", new AbsoluteSizeSpan(size, true).getSize() + ""); output.setSpan(new AbsoluteSizeSpan(size, true), where, len, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } + if ("underline".equals(text_decoration)) { + output.setSpan(new UnderlineSpan(), where, len, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + } + if ("line-through".equals(text_decoration)) { + output.setSpan(new StrikethroughSpan(), where, len, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + } } } @@ -358,13 +373,14 @@ private HashMap getAttributes(XMLReader xmlReader) { * The data index is "just" where the keys and values are stored. */ for (int i = 0; i < len; i++) { - if ("style".equals(data[i * 5 + 1])) { + if ("style".equals(data[i * 5 + 1]) || "data-mce-style".equals(data[i * 5 + 1])) { String[] styleAttrs = data[i * 5 + 4].split(";"); int styleLen = styleAttrs.length; for (int j = 0; j < styleLen; j++) { String[] styleAttr = styleAttrs[j].split(":"); attributes.put(styleAttr[0].trim(), styleAttr[1].trim()); } + continue; } attributes.put(data[i * 5 + 1], data[i * 5 + 4]); } @@ -391,8 +407,15 @@ private static Object getLast(Editable text, Class kind) { } private static int parseColor(String colorString) { + String hexColorString = "#"; try { - return Color.parseColor(colorString); + if (colorString.contains("rgb")) { + String[] rgb = colorString.replace("rgb(", "").replace(")", "").split(","); + for (int i = 0; i < 3; i++) { + hexColorString += Integer.toHexString(Integer.parseInt(rgb[i].trim())); + } + } + return Color.parseColor(hexColorString); } catch (Exception ignored) { return -1; } @@ -402,13 +425,20 @@ private static int parseColor(String colorString) { * dpValue */ private int parseSize(String size) { + if (size != null) { + size = size.replace("px", "").replace("pt", ""); + } int s; try { s = Integer.parseInt(size); } catch (NumberFormatException ignored) { return 0; } - + if (s == 36) { + s = 6; + } else { + s = 1; + } s = Math.max(s, 1); s = Math.min(s, 7); diff --git a/sample/src/main/java/me/wcy/htmltext/sample/MainActivity.java b/sample/src/main/java/me/wcy/htmltext/sample/MainActivity.java index a6cdd01..42fe8c5 100644 --- a/sample/src/main/java/me/wcy/htmltext/sample/MainActivity.java +++ b/sample/src/main/java/me/wcy/htmltext/sample/MainActivity.java @@ -39,7 +39,6 @@ protected void onCreate(Bundle savedInstanceState) { textView = (TextView) findViewById(R.id.text); textView.setMovementMethod(LinkMovementMethod.getInstance()); String sample = getSample(); - Log.d("debug",sample); HtmlText.from(sample) .setImageLoader(new HtmlImageLoader() { @Override diff --git a/sample/src/main/res/layout/activity_main.xml b/sample/src/main/res/layout/activity_main.xml index 5545140..27fceea 100644 --- a/sample/src/main/res/layout/activity_main.xml +++ b/sample/src/main/res/layout/activity_main.xml @@ -5,8 +5,8 @@ diff --git a/sample/src/main/res/raw/sample.html b/sample/src/main/res/raw/sample.html index cf8ec9e..d92cf0f 100644 --- a/sample/src/main/res/raw/sample.html +++ b/sample/src/main/res/raw/sample.html @@ -1,2 +1 @@ -

大萨达112

-

大萨达112

+
  1. 同时插入第二个辑器,配置相互独立

同时插入第二个辑器,配置相互独立

同时插入第二个辑器,配置相互独立

  • 同时插入第二个辑器,配置相互独立
\ No newline at end of file From c72e2e920e5b9d87ca653f1f484d18b1e3d83e12 Mon Sep 17 00:00:00 2001 From: SilverKnife Date: Wed, 22 Jul 2020 16:55:42 +0800 Subject: [PATCH 3/5] =?UTF-8?q?span=E5=B5=8C=E5=A5=97=E6=A0=B7=E5=BC=8F?= =?UTF-8?q?=E8=A2=AB=E8=A6=86=E7=9B=96=E9=97=AE=E9=A2=98=E8=A7=A3=E5=86=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/me/wcy/htmltext/HtmlTagHandler.java | 74 +++++++++++++++++-- sample/src/main/res/raw/sample.html | 2 +- 2 files changed, 67 insertions(+), 9 deletions(-) diff --git a/htmltext/src/main/java/me/wcy/htmltext/HtmlTagHandler.java b/htmltext/src/main/java/me/wcy/htmltext/HtmlTagHandler.java index 7c8293e..a676947 100644 --- a/htmltext/src/main/java/me/wcy/htmltext/HtmlTagHandler.java +++ b/htmltext/src/main/java/me/wcy/htmltext/HtmlTagHandler.java @@ -43,6 +43,7 @@ import java.lang.reflect.Field; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.Stack; @@ -60,6 +61,7 @@ class HtmlTagHandler implements Html.TagHandler { private static final String FONT = "HTML_TEXT_TAG_FONT"; private static final String DIV = "HTML_TEXT_TAG_DIV"; private static final String SPAN = "HTML_TEXT_TAG_SPAN"; + private static final String P = "HTML_TEXT_TAG_P"; private Context mContext; private TextPaint mTextPaint; @@ -75,6 +77,11 @@ class HtmlTagHandler implements Html.TagHandler { */ private Stack olNextIndex = new Stack<>(); + /** + * Span嵌套反加载 + */ + private Stack SpanFontNextIndex = new Stack<>(); + private static final int indent = 10; private static final int listItemIndent = indent * 2; private static final BulletSpan bullet = new BulletSpan(indent); @@ -204,7 +211,7 @@ public void handleTag(final boolean opening, final String tag, Editable output, } else if (tag.equalsIgnoreCase(FONT)) { endFont(output); } else if (tag.equalsIgnoreCase(SPAN)) { - endFont(output); + endSpan(output); } else if (tag.equalsIgnoreCase(DIV)) { handleDiv(output); } else if (tag.equalsIgnoreCase("code")) { @@ -219,6 +226,8 @@ public void handleTag(final boolean opening, final String tag, Editable output, end(output, Th.class, false); } else if (tag.equalsIgnoreCase("td")) { end(output, Td.class, false); + } else if (tag.equalsIgnoreCase("html")) { + setCss(output); } } } @@ -253,6 +262,8 @@ private static class Font { public String size; public String text_decoration; public String text_align; + public int where; + public int len; public Font(String background_color, String color, String size, String text_decoration, String text_align) { this.background_color = background_color; @@ -315,11 +326,56 @@ private void endFont(Editable output) { if (where != len) { Font f = (Font) obj; + f.where = where; + f.len = len; + int background_color = parseColor(f.background_color); + int color = parseColor(f.color); + int size = parseSize(f.size); + String text_decoration = f.text_decoration; + String text_align = f.text_align; + if (background_color != -1) { + output.setSpan(new BackgroundColorSpan(background_color | 0xFF000000), where, len, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + } + if (color != -1) { + output.setSpan(new ForegroundColorSpan(color | 0xFF000000), where, len, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + } + if (size > 0) { + Log.d("font-size", new AbsoluteSizeSpan(size, true).getSize() + ""); + output.setSpan(new AbsoluteSizeSpan(size, true), where, len, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + } + if ("underline".equals(text_decoration)) { + output.setSpan(new UnderlineSpan(), where, len, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + } + if ("line-through".equals(text_decoration)) { + output.setSpan(new StrikethroughSpan(), where, len, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + } + } + } + + private void endSpan(Editable output) { + int len = output.length(); + Object obj = getLast(output, Font.class); + int where = output.getSpanStart(obj); + output.removeSpan(obj); + + if (where != len) { + Font f = (Font) obj; + f.where = where; + f.len = len; + SpanFontNextIndex.push(f); + } + } + + private void setCss(Editable output) { + while (!SpanFontNextIndex.empty()) { + Font f = SpanFontNextIndex.pop(); int background_color = parseColor(f.background_color); int color = parseColor(f.color); int size = parseSize(f.size); String text_decoration = f.text_decoration; String text_align = f.text_align; + int where = f.where; + int len = f.len; if (background_color != -1) { output.setSpan(new BackgroundColorSpan(background_color | 0xFF000000), where, len, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } @@ -414,6 +470,8 @@ private static int parseColor(String colorString) { for (int i = 0; i < 3; i++) { hexColorString += Integer.toHexString(Integer.parseInt(rgb[i].trim())); } + }else { + hexColorString = colorString; } return Color.parseColor(hexColorString); } catch (Exception ignored) { @@ -434,13 +492,13 @@ private int parseSize(String size) { } catch (NumberFormatException ignored) { return 0; } - if (s == 36) { - s = 6; - } else { - s = 1; - } - s = Math.max(s, 1); - s = Math.min(s, 7); +// if (s == 36) { +// s = 6; +// } else { +// s = 1; +// } +// s = Math.max(s, 1); +// s = Math.min(s, 7); int baseSize = px2dp(mTextPaint.getTextSize()); diff --git a/sample/src/main/res/raw/sample.html b/sample/src/main/res/raw/sample.html index d92cf0f..7b6cc82 100644 --- a/sample/src/main/res/raw/sample.html +++ b/sample/src/main/res/raw/sample.html @@ -1 +1 @@ -
  1. 同时插入第二个辑器,配置相互独立

同时插入第二个辑器,配置相互独立

同时插入第二个辑器,配置相互独立

  • 同时插入第二个辑器,配置相互独立
\ No newline at end of file +

0元办会员高端配置任你玩

\ No newline at end of file From fa92e7146523633e11b685adc90b04e284c7dcca Mon Sep 17 00:00:00 2001 From: SilverKnife Date: Wed, 22 Jul 2020 17:17:19 +0800 Subject: [PATCH 4/5] udpate --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b398564..07a8a21 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # html-text -[![](https://jitpack.io/v/wangchenyan/html-text.svg)](https://jitpack.io/#wangchenyan/html-text) +[![](https://jitpack.io/v/SilverIceKey/html-text.svg)](https://jitpack.io/#SilverIceKey/html-text) html-text 是 android.text.Html 的一个扩展,可以加载 HTML 并将其转换成 Spannable 显示在 TextView 上,支持网络图片,图片加载器无绑定,支持图片和链接点击事件,扩展了更多标签。 From 65cae86517b86420f71c287409ffab492d294e8b Mon Sep 17 00:00:00 2001 From: SilverKnife Date: Wed, 22 Jul 2020 18:07:49 +0800 Subject: [PATCH 5/5] =?UTF-8?q?p=E6=A0=87=E7=AD=BE=E5=A4=84=E7=90=86?= =?UTF-8?q?=EF=BC=8C=E6=96=87=E5=AD=97=E5=A4=A7=E5=B0=8F=E5=A4=84=E7=90=86?= =?UTF-8?q?=EF=BC=8C=E5=B1=85=E4=B8=AD=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/me/wcy/htmltext/HtmlTagHandler.java | 42 +++++++++++++------ sample/src/main/res/raw/sample.html | 2 +- 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/htmltext/src/main/java/me/wcy/htmltext/HtmlTagHandler.java b/htmltext/src/main/java/me/wcy/htmltext/HtmlTagHandler.java index a676947..b16ac2c 100644 --- a/htmltext/src/main/java/me/wcy/htmltext/HtmlTagHandler.java +++ b/htmltext/src/main/java/me/wcy/htmltext/HtmlTagHandler.java @@ -121,6 +121,8 @@ String overrideTags(String html) { html = html.replace("", ""); html = html.replace("", ""); + html = html.replace("", ""); return html; } @@ -151,6 +153,9 @@ public void handleTag(final boolean opening, final String tag, Editable output, startFont(output, xmlReader); } else if (tag.equalsIgnoreCase(SPAN)) { startFont(output, xmlReader); + } else if (tag.equalsIgnoreCase(P)) { + handleDiv(output); + startFont(output, xmlReader); } else if (tag.equalsIgnoreCase(DIV)) { handleDiv(output); } else if (tag.equalsIgnoreCase("code")) { @@ -212,6 +217,9 @@ public void handleTag(final boolean opening, final String tag, Editable output, endFont(output); } else if (tag.equalsIgnoreCase(SPAN)) { endSpan(output); + } else if (tag.equalsIgnoreCase(P)) { + handleDiv(output); + endP(output); } else if (tag.equalsIgnoreCase(DIV)) { handleDiv(output); } else if (tag.equalsIgnoreCase("code")) { @@ -332,7 +340,6 @@ private void endFont(Editable output) { int color = parseColor(f.color); int size = parseSize(f.size); String text_decoration = f.text_decoration; - String text_align = f.text_align; if (background_color != -1) { output.setSpan(new BackgroundColorSpan(background_color | 0xFF000000), where, len, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } @@ -352,7 +359,15 @@ private void endFont(Editable output) { } } + private void endP(Editable output) { + endSpan(output, true); + } + private void endSpan(Editable output) { + endSpan(output, false); + } + + private void endSpan(Editable output, boolean isP) { int len = output.length(); Object obj = getLast(output, Font.class); int where = output.getSpanStart(obj); @@ -360,6 +375,9 @@ private void endSpan(Editable output) { if (where != len) { Font f = (Font) obj; + if (isP && f.text_align == null) { + f.text_align = "left"; + } f.where = where; f.len = len; SpanFontNextIndex.push(f); @@ -392,6 +410,15 @@ private void setCss(Editable output) { if ("line-through".equals(text_decoration)) { output.setSpan(new StrikethroughSpan(), where, len, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } + if ("left".equals(text_align)) { + output.setSpan(new AlignmentSpan.Standard(Layout.Alignment.ALIGN_NORMAL), where, len, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + } + if ("center".equals(text_align)) { + output.setSpan(new AlignmentSpan.Standard(Layout.Alignment.ALIGN_CENTER), where, len, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + } + if ("right".equals(text_align)) { + output.setSpan(new AlignmentSpan.Standard(Layout.Alignment.ALIGN_OPPOSITE), where, len, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + } } } @@ -470,7 +497,7 @@ private static int parseColor(String colorString) { for (int i = 0; i < 3; i++) { hexColorString += Integer.toHexString(Integer.parseInt(rgb[i].trim())); } - }else { + } else { hexColorString = colorString; } return Color.parseColor(hexColorString); @@ -492,17 +519,8 @@ private int parseSize(String size) { } catch (NumberFormatException ignored) { return 0; } -// if (s == 36) { -// s = 6; -// } else { -// s = 1; -// } -// s = Math.max(s, 1); -// s = Math.min(s, 7); - - int baseSize = px2dp(mTextPaint.getTextSize()); - return (s - 3) + baseSize; + return px2dp(s); } private int px2dp(float pxValue) { diff --git a/sample/src/main/res/raw/sample.html b/sample/src/main/res/raw/sample.html index 7b6cc82..52cc345 100644 --- a/sample/src/main/res/raw/sample.html +++ b/sample/src/main/res/raw/sample.html @@ -1 +1 @@ -

0元办会员高端配置任你玩

\ No newline at end of file +

0元办会员高端配置任你玩

首充300元

200元


\ No newline at end of file