Android实现富文本时遇到的一些问题(2)字体效果的保存

一个富文本应该能保存各种各样的效果,之前已经说过了用HTML.toHtml,新的问题又来了,fromHtml的时候字体大小效果会消失,

这不知道算不算是个BUG吧。。因为官方的代码里就是没有处理

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的代码里都是这样。。。。

高手呢,很容易,自己写个解析器,把详解析的解析了,但是我是懒人。。只能用现有的了,

我的做法是从网站上把 HTML类下载下来,然后放到自己的项目里用,中间会涉及到一些内部类,也需要手动下载手动引用,

而且会报一些警告,无视就好

下面是我修改完的代码

private static void startFont(SpannableStringBuilder text,
                                  Attributes attributes) {
        String color = attributes.getValue("", "color");
        String face = attributes.getValue("", "face");
        //change by rockman
        String 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 rockman
            
            if (!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做处理, 所以我们也可以处理自定义标签,

开源的系统就是好。。不然错了都不知道怎么办。。。 PS:其实我也是在STACKOVERFLOW里找出来的 = =!

posted @ 2013-01-05 20:38  rockman12352  阅读(668)  评论(0编辑  收藏  举报