富文本编辑器如何插入一段文字并自动换行?
发布于 7 年前 作者 cuiming 11917 次浏览 来自 官方Issues

如题

需要点击后自动在富文本编辑器中添加一段h2的标题,但是发现小程序editor api只有插入图片和分割线,我尝试了先format 成H2,再插入文字,代码如下    

this.editorCtx.format('header', 'H2');
this.editorCtx.insertText({ text: '这是一段二级标题'});
this.editorCtx.removeFormat()

发现如果在format之前没有手动换行的话,format会连同之前的内容一起修改掉样式。

尝试在插入文字之前添加分割线,可以正确实现换行。代码如下

this.editorCtx.insertDivider()
this.editorCtx.insertText({  text: '测试' });
this.editorCtx.format('header', 'H2');

但是需求设计上没有分割线,只有一些间距,所以我又尝试了在插入文字之前先插入一张图片,然后把图片的透明度设置成0,高度设置成1,

代码如下

this.editorCtx.insertImage({
     src: ‘xxx',

     mode: 'aspectFit',   

    extClass: 'strategy-image'

});
this.editorCtx.format('header', 'H2');
this.editorCtx.insertText({ text: ‘这是一段标题’) });
this.editorCtx.removeFormat()

发现就算是之前的内容换行了,也会被新的样式覆盖掉,这是为什么呢?

要怎么才能实现换行插入文字呢???

2 回复

能遇到句号自动换行吗

找到解决方案了,在插入目标文字之前先插入

 this.editorCtx.insertText({  text: '\n' });

可以实现换行

回到顶部