简单的富文本展示组件: 支持的标签:span、font、br、a、img。 支持的属性:color、font-color、size、font-size、background、href。
通过 ohpm
ohpm install simplehtmlrender
注意如果需要展示img
标签,需要添加网络权限,可以参考这里申请应用权限
HtmlRender({htmlContent:this.htmlContent})
HtmlRender({htmlContent:this.htmlContent,defaultStyle:{fontSize:14,hrefFontColor:Color.Red,fontColor:Color.Brown} as Style, onLinkClick:(link:string)=>{
promptAction.showToast({message:`点击了链接 ${link}`})
}})
需要注意的是:buildHtml
方法实际是在HtmlRender
中执行,需要注意this
指向
build() {
HtmlRender({ htmlContent: this.htmlContent, builder: this.buildHtml }).margin(10)
}
@Builder
buildHtml(vNode: VNode) {
Text() {
ForEach(vNode.child, (child: VNode) => {
if (child.style.backgroundColor != Color.Transparent) {
ContainerSpan() {
if (child.style.href) {
Span(child.text).configSpanStyle(child).onClick((_) => {
promptAction.showToast({ message: child.style.href })
}).fontColor(Color.Green)
} else {
Span(child.text).configSpanStyle(child).margin(20)
}
}.textBackgroundStyle({ color: child.style.backgroundColor })
} else {
if (child.style.href) {
Span(child.text).configSpanStyle(child).onClick((_) => {
promptAction.showToast({ message: child.style.href })
}).fontColor(Color.Green).margin(20)
} else {
Span(child.text).configSpanStyle(child)
}
}
})
}
}
@Extend(Span)
function configSpanStyle(vNode: VNode) {
.backgroundColor(vNode.style.backgroundColor)
.fontColor(vNode.style.href ? vNode.style.hrefFontColor : vNode.style.fontColor)
.fontSize(vNode.style.fontSize)
}
- 目前只支持了上面说的那些标签级属性:span、font、br、a标签 和 color、font-color、size、font-size、background、href这些属性
- 富文本的解析使用的是
xml.XmlPullParser
,因此对富文本内容中的标签要求比较严格,一定要严格闭合才行,否则解析会失败。 - 为了解决某些元素没有包含在标签内的问题,在解析时在富文本最外层添加没有属性的span标签
- 对于颜色值,只支持了有限了英文名字,建议使用
#xxxxxx
表示 - 为了避免不必要的错误,富文本中只能出现默认支持和自定义支持的标签
解析结果的展示是Text
中嵌套Span
和ContainerSpan
实现的:
Span
不支持背景色,要么只能依赖父级控件Text
或者父级控件ContainerSpan
来设置背景色ContainerSpan
只能包含Span
、ImageSpan
子组件。Span
、ImageSpan
没有子控件ImageSpan
只支持本地资源,因此暂时没有处理图片相关标签ImageSpan
支持网络资源,但不支持fitOriginalSize
属性,因此设置它的高度为字体大小,宽度按比例缩放;但alt
属性只支持PixelMap,所以就暂时不支持了
也就是说解析结果只有一个Text
控件,内容样式都由Span
、ImageSpan
和ContainerSpan
完成
详细解释在这里 :鸿蒙-做一个简单的、大家都能学会的富文本解析渲染控件