美文网首页
TextView加载富文本(图片对齐)

TextView加载富文本(图片对齐)

作者: 蜗蜗牛牛 | 来源:发表于2019-06-06 08:49 被阅读0次

/**
* TextView加载带图片html文字
*/
static class HtmlTask extends AsyncTask<String, Integer, Spanned> {
WeakReference<TextView> v;
int lineSpacingExtra;

    public HtmlTask(TextView v) {
        this.v = new WeakReference<>(v);
        lineSpacingExtra = (int) v.getLineSpacingExtra();
    }

    @Override
    protected Spanned doInBackground(String... strings) {
        Spanned spanned = Html.fromHtml(strings[0], new Html.ImageGetter() {
            @Override
            public Drawable getDrawable(String source) {
                InputStream is = null;
                try {
                    is = (InputStream) new URL(source).getContent();
                    Drawable d = Drawable.createFromStream(is, "src");
                    d.setBounds(0, 0, d.getIntrinsicWidth(),
                            d.getIntrinsicHeight());
                    is.close();
                    return d;
                } catch (Exception e) {
                    if (is != null) {
                        try {
                            is.close();
                        } catch (IOException e1) {
                            e1.printStackTrace();
                        }
                    }
                    return null;
                }
            }
        }, null);
        if (spanned instanceof SpannableStringBuilder) {
            ImageSpan[] imageSpans = spanned.getSpans(0, spanned.length(), ImageSpan.class);
            for (ImageSpan imageSpan : imageSpans) {
                int start = spanned.getSpanStart(imageSpan);
                int end = spanned.getSpanEnd(imageSpan);
                Drawable d = imageSpan.getDrawable();
                ImageSpan newImageSpan = new StickerSpan(d, ImageSpan.ALIGN_BASELINE, lineSpacingExtra);
                ((SpannableStringBuilder) spanned).setSpan(newImageSpan, start, end, Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
                ((SpannableStringBuilder) spanned).removeSpan(imageSpan);
            }
        }

        return spanned;
    }

    @Override
    protected void onPostExecute(Spanned spanned) {
        if (v.get() != null) {
            v.get().setText(spanned);
        }
    }
}

/**
 * 让html中图片和文字对齐
 */
static class StickerSpan extends ImageSpan {
    int lineSpacingExtra;

    StickerSpan(Drawable b, int verticalAlignment, int lineSpacingExtra) {
        super(b, verticalAlignment);
        this.lineSpacingExtra = lineSpacingExtra;
    }

    @Override
    public void draw(Canvas canvas, CharSequence text,
                     int start, int end, float x,
                     int top, int y, int bottom, Paint paint) {
        Drawable b = getDrawable();
        canvas.save();
        int transY = bottom - b.getBounds().bottom - lineSpacingExtra;
        if (mVerticalAlignment == ALIGN_BASELINE) {
            int textLength = text.length();
            for (int i = 0; i < textLength; i++) {
                if (Character.isLetterOrDigit(text.charAt(i))) {
                    transY -= paint.getFontMetricsInt().descent;
                    break;
                }
            }
        }
        canvas.translate(x, transY);
        b.draw(canvas);
        canvas.restore();
    }
}

相关文章

  • TextView加载富文本(图片对齐)

    /*** TextView加载带图片html文字*/static class HtmlTask extends A...

  • iOS 图文混排

    创建label 创建富文本 创建图片文本 富文本添加图片 图片和文字居中对齐 图片文本加入富文本中 图片插入文本中...

  • android学习之富文本

    android TextView可以用加载富文本,TextView本身不需要改动什么,只需要装入对加载的文本进行处...

  • UILabel中加载富文本时处理富文本图片的大小问题

    描述: 当UILabel加载富文本的时候,富文本中有图片,图片的大小超出屏幕的宽度时,可以调整图片的大小

  • Android富文本的学习一

    1TextView的富文本介绍: 什么是富文本,大家都知道TextView可以显示文字,设置文字样式,利用富文本可...

  • Android开发笔记

    1.使用Glide加载图片 2.TextView设置Html标签文本 3.ListView的条目加载动画 4.Po...

  • iOS NSAttributedString富文本总结

    eg: 1.最普遍的属性使用(如文本颜色) 2.文本附件和链接 先为textView设置富文本 IOS label加图片

  • android 文本内容对齐

    想实现文本内容可以采取自定义TextView实现文字两端对齐,也可以采用webview加载的方式实现。本文说的是采...

  • UITextView动态适应高度

    首先设置textView不能滚动, 设置textview的文字,富文本文字同样可以

  • 记录修改

    富文本加载html图片,出现图片显示不全的问题,网上查到,限制宽度就好 NSString * str = [NSS...

网友评论

      本文标题:TextView加载富文本(图片对齐)

      本文链接:https://www.haomeiwen.com/subject/ndtehqtx.html