crator中富文本图片怎么添加,急急急!!

  • Creator 版本:

  • 目标平台:

  • 详细报错信息,包含调用堆栈:

  • 重现方式:

  • 之前哪个版本是正常的 :

  • 手机型号 :

  • 手机浏览器 :

  • 编辑器操作系统 :

  • 编辑器之前是否有其它报错 :

  • 出现概率:

  • 额外线索:

http://docs.cocos.com/creator/manual/zh/components/richtext.html?h=富文本
给富文本添加图文混排功能,img 的 src 属性必须是 ImageAtlas 图集里面的一个有效的 spriteframe 名称
< img src=‘emoji1’ click=‘handler’ />
注意: 只有 这种写法是有效的。如果你指定一张很大的图片,那么该图片创建出来的精灵会被等比缩放,缩放的值等于富文本的行高除以精灵的高度。

tempSpriteAtlas: cc.SpriteAtlas;

setLoadImg(sp: cc.SpriteFrame) {

    let urlName = Math.random(); // uid必须唯一

    let spf: cc.SpriteFrame = sp;

    let tempSpriteAtlas = this.tempSpriteAtlas ?? (new cc.SpriteAtlas());

    this.tempSpriteAtlas = tempSpriteAtlas;

    tempSpriteAtlas["_spriteFrames"][`${urlName}`] = spf;

    // console.warn("tempSpriteAtlas", tempSpriteAtlas);

    let TempRichText = this.txt_des;  // RichText

    TempRichText.imageAtlas = tempSpriteAtlas;

    let tempSize = Utils.getAutoSizeWidth({ width: spf["_originalSize"].width, height: spf["_originalSize"].height }, TempRichText.node.width);

    let newtext = `<img src='${urlName}' width=${Math.floor(tempSize.width)} height=${Math.floor(tempSize.height)} />`;

    return newtext;

}

export const getAutoSizeWidth = (myVec2: { width: number, height: number }, targetWidth: number) => {

    let width: number = 0, height: number = 0;

    if (myVec2.width > targetWidth) {

        width = targetWidth;

        height = myVec2.height * (targetWidth / myVec2.width);

    } else {

        width = myVec2.width;

        height = myVec2.height;

    }

    // return zoom;

    const size = {

        width,

        height

    }

    return size;

}

老哥这么久的帖子咋被你翻出来了 :joy: :joy: