Android实现富文本时遇到的一些问题(2)字体效果的保存
一個(gè)富文本應(yīng)該能保存各種各樣的效果,之前已經(jīng)說(shuō)過(guò)了用HTML.toHtml,新的問(wèn)題又來(lái)了,fromHtml的時(shí)候字體大小效果會(huì)消失,
這不知道算不算是個(gè)BUG吧。。因?yàn)楣俜降拇a里就是沒有處理
private static void startFont(SpannableStringBuilder text,Attributes attributes) {String color = attributes.getValue("", "color");String face = attributes.getValue("", "face");text.setSpan(new Font(color, face), len, len, Spannable.SPAN_MARK_MARK);}HTML里面只處理了color 和 face,?而且從1.5->4.0的代碼里都是這樣。。。。
高手呢,很容易,自己寫個(gè)解析器,把詳解析的解析了,但是我是懶人。。只能用現(xiàn)有的了,
我的做法是從網(wǎng)站上把?HTML類下載下來(lái),然后放到自己的項(xiàng)目里用,中間會(huì)涉及到一些內(nèi)部類,也需要手動(dòng)下載手動(dòng)引用,
而且會(huì)報(bào)一些警告,無(wú)視就好
下面是我修改完的代碼
private static void startFont(SpannableStringBuilder text,Attributes attributes) {String color = attributes.getValue("", "color");String face = attributes.getValue("", "face");//change by rockmanString size = attributes.getValue("", "size");int len = text.length();text.setSpan(new Font(color, face, size), len, len, Spannable.SPAN_MARK_MARK);} private static void endFont(SpannableStringBuilder text) {int len = text.length();Object obj = getLast(text, Font.class);int where = text.getSpanStart(obj);text.removeSpan(obj);if (where != len) {Font f = (Font) obj;if (!TextUtils.isEmpty(f.mColor)) {if (f.mColor.startsWith("@")) {Resources res = Resources.getSystem();String name = f.mColor.substring(1);int colorRes = res.getIdentifier(name, "color", "android");if (colorRes != 0) {ColorStateList colors = res.getColorStateList(colorRes);text.setSpan(new TextAppearanceSpan(null, 0, 0, colors, null),where, len,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);}} else {int c = getHtmlColor(f.mColor);if (c != -1) {text.setSpan(new ForegroundColorSpan(c | 0xFF000000),where, len,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);}}}if (f.mFace != null) {text.setSpan(new TypefaceSpan(f.mFace), where, len,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);}//add by rockmanif (!TextUtils.isEmpty(f.mSize)) {int s = Integer.parseInt(f.mSize)*6;text.setSpan(new AbsoluteSizeSpan(s),where, len,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);}}}還要處理FONT的閉合
官方代碼的處理是用startXXX? endXXX做處理, 所以我們也可以處理自定義標(biāo)簽,
開源的系統(tǒng)就是好。。不然錯(cuò)了都不知道怎么辦。。。 PS:其實(shí)我也是在STACKOVERFLOW里找出來(lái)的 = =!
轉(zhuǎn)載于:https://www.cnblogs.com/rockman12352/archive/2013/01/05/2846658.html
《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
以上是生活随笔為你收集整理的Android实现富文本时遇到的一些问题(2)字体效果的保存的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 2013 年规划
- 下一篇: Android 通过 XMPP 实现聊天