HarmonyOS 鸿蒙Next Span组件不支持margin/padding/border

HarmonyOS 鸿蒙Next Span组件不支持margin/padding/border

目前业务需要以下布局
即文本超长后从父组件最左侧开始换行

目前使用的解决方案包含Span组件和Text组件
问题分别如下:
1、Span组件
Span组件不支持margin/padding/border。不符合业务诉求
2、Text组件
Row内部套用Text组件后,Text组件独占一列。导致换行后从第三列开始换行

 


更多关于HarmonyOS 鸿蒙Next Span组件不支持margin/padding/border的实战教程也可以访问 https://www.itying.com/category-93-b0.html

2 回复
可计算组件宽度,前面两个Text使用定位,第三Text使用文本缩进:
[@Entry](/user/Entry)
[@Component](/user/Component)
struct TextSpanTag {
[@State](/user/State) Text1Width: number = 0
[@State](/user/State) Text2Width: number = 0

build() {
Column() {
Row() {
Text('8888新')
.fontSize(12)
.fontColor("#ff0000")
.borderColor("#ff0000")
.borderWidth('1vp')
.position({ x: 0, y: 2 })
.padding({
top: '1vp',
bottom: "1vp",
left: "8vp",
right: '8vp'
})
.margin({ right: '5vp' })
.borderRadius('2vp')
.onAreaChange((oldValue, newValue: Area) => {
this.Text1Width = Number(newValue.width)
console.log("TextWidth", this.Text1Width)
})
Text('B1')
.fontSize(12)
.fontColor("#ffffff")
.backgroundColor("#ff0000")
.position({ x: this.Text1Width + 5, y: 2 })
.padding({
top: '2vp',
bottom: "2vp",
left: "8vp",
right: '8vp'
})
.margin({ right: '5vp' })
.borderRadius('2vp')
.onAreaChange((oldValue, newValue: Area) => {
this.Text2Width = Number(newValue.width)
console.log("TextWidth", this.Text2Width)
})
Text('这里是标题这里是标题这里是标题这里是标题这里是标题这里是标题这里是标题')
.textIndent(this.Text1Width + 5 + this.Text2Width + 5)//2个Text宽度+右边距
.lineHeight('22vp')
}
.width('100%')
}
.height('100%')
.alignItems(HorizontalAlign.Start)
}
}

更多关于HarmonyOS 鸿蒙Next Span组件不支持margin/padding/border的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


HarmonyOS 鸿蒙Next的Span组件确实不支持margin、padding以及border属性的设置。这一限制是由于Span组件的设计规格所决定的,它主要作为Text或ContainerSpan组件的子组件,专注于文本内容的展示。

具体来说,Span组件支持文本通用属性,如fontColor、fontSize、fontStyle、fontWeight、decoration、letterSpacing、textCase、fontFamily和textShadow,这些属性允许开发者调整文本的样式和外观。然而,对于布局相关的属性,如margin、padding和border,Span组件并不提供支持。

如果你需要在文本周围添加间距或边框效果,可以考虑使用其他布局组件或Text组件的样式属性来实现。例如,你可以使用View组件来包裹Text或Span组件,并通过设置View的margin、padding和border属性来达到所需的布局效果。

如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html

回到顶部