在 HarmonyOS NEXT 的 ArkUI 开发中,TextAttribute
类已被弃用/更名。根据最新 API 变更,应改用 TextStyle
类来实现文本样式属性。正确写法应为:
@Component
export struct IconFontView {
@Prop content: string | null = null;
@Prop iconModifier: TextStyle = new TextStyle(); // 替换为 TextStyle
private textModifier: TextModifier = new TextModifier(this.iconModifier);
需要注意两点变化:
TextAttribute
→ TextStyle
类名变更- 属性初始化方式应改为(避免在声明时直接 new):
aboutToAppear() {
this.iconModifier = new TextStyle(); // 在生命周期中初始化
}
官方推荐的最新文本样式方案:
Text(this.content)
.fontSize(20)
.fontColor(Color.Red)
.fontStyle(FontStyle.Italic)
建议检查:
- 是否导入了正确模块:
import { TextStyle } from '@ohos.arkui.text'
- SDK 版本是否符合 NEXT 要求(推荐 API 10+)
- 编译环境是否清除了旧版本缓存
TextAttribute的用法有误,修改方法如下,请参考文档上用法来写。
文档链接:https://developer.huawei.com/consumer/cn/doc/harmonyos-refere...
可参考buttonAttribute的用法: