往期鸿蒙5.0全套实战文章必看:(文中附带全栈鸿蒙5.0学习资料)
属性字符串
方便灵活应用文本样式的对象,可通过TextController中的setStyledString方法与Text组件绑定,可通过RichEditorStyledStringController中的setStyledString方法与RichEditor组件绑定。
说明
从API Version 12开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
属性字符串目前不支持在worker线程中使用。
规则说明
- 当组件样式和属性字符串中的样式冲突时,冲突部分以属性字符串设置的样式为准,未冲突部分则生效组件的样式。
- 当属性字符串和Text子组件冲突时,属性字符串优先级高,即当Text组件中绑定了属性字符串,忽略Text组件下包含Span等子组件的情况。
- 不支持@State修饰。
StyledString
constructor(value: string | ImageAttachment | CustomSpan , styles?: Array<StyleOptions>)
元服务API: 从API version 12开始,该接口支持在元服务中使用。
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
value | string | ImageAttachment | CustomSpan | 是 | 属性字符串文本内容。 说明: 当value值为ImageAttachment或CustomSpan时,styles参数不生效。 |
styles | Array<StyleOptions> | 否 | 属性字符串初始化选项。 说明: start为异常值时,按默认值0处理。 当start的值合法且length为异常值时,length的值为属性字符串长度与start的值的差值。 StyledStringKey与StyledStringValue不匹配时,不生效。 styledKey参数无默认值。 |
属性
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
名称 | 类型 | 只读 | 可选 | 说明 |
---|---|---|---|---|
length | number | 是 | 否 | 属性字符串字符的长度。 说明: 当属性字符串中包含图片或者CustomSpan时,其返回的长度按1计算。 |
getString
getString(): string
获取字符串信息。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
返回值:
类型 | 说明 |
---|---|
string | 属性字符串文本内容。 说明: 当属性字符串中包含图片时,其返回的结果用空格表示。 |
equals
equals(other: StyledString): boolean
判断两个属性字符串是否相等。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
other | StyledString | 是 | StyledString类型的比较对象。 |
返回值:
类型 | 说明 |
---|---|
boolean | 两个属性字符串是否相等。 说明: 当属性字符串的文本及样式均一致,视为相等。 不比较GestureStyle,当属性字符串配置了不同事件,文本和其他样式相同时,亦视为相等。 当比较CustomSpan时,比较的是地址,地址相等,视为相等。 |
subStyledString
获取属性字符串的子字符串。
subStyledString(start: number , length?: number): StyledString
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
start | number | 是 | 子属性字符串开始位置的下标。 |
length | number | 否 | 子属性字符串的长度。 |
返回值:
类型 | 说明 |
---|---|
StyledString | 子属性字符串。 说明: 当start为合法入参时,length的默认值是被查询属性字符串对象的长度与start的值的差。 当start和length越界或者必填传入undefined时,会抛出异常。 |
错误码:
以下错误码详细介绍。
错误码ID | 错误信息 |
---|---|
401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed. |
getStyles
获取指定范围属性字符串的样式集合。
getStyles(start: number , length: number , styledKey?: StyledStringKey): Array<SpanStyle>
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
start | number | 是 | 指定范围属性字符串的下标。 |
length | number | 是 | 指定范围属性字符串的长度。 |
styledKey | StyledStringKey | 否 | 指定范围属性字符串样式的枚举值。 |
返回值:
类型 | 说明 |
---|---|
Array<SpanStyle> | 各样式对象的数组。 说明: 当指定范围属性字符串未设置任何样式,则返回空数组。 当start和length越界或者必填传入undefined时,会抛出异常; 当styledKey传入异常值或undefined时,会抛出异常。 当styledKey为CustomSpan时,返回的是创建CustomSpan时传入的样式对象,即修改该样式对象也会影响实际的显示效果。 |
错误码:
以下错误码详细介绍。
错误码ID | 错误信息 |
---|---|
401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3. Parameter verification failed. |
fromHtml
将HTML格式字符串转换成属性字符串,当前支持转换的HTML标签范围:<p>、<span>、<img>。仅支持将这三种标签中的style属性样式转换成对应的属性字符串样式。
static fromHtml(html: string): Promise<StyledString>
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
html | string | 是 | html格式的字符串。 |
返回值:
类型 | 说明 |
---|---|
StyledString | 属性字符串。 |
错误码:
以下错误码详细介绍。
错误码ID | 错误信息 |
---|---|
401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3. Parameter verification failed. |
MutableStyledString
继承于StyledString类。
说明
当start和length越界或者必填传入undefined时,会抛出异常;
当styledKey和styledValue传入异常值或者两者对应关系不匹配时,会抛出异常。
replaceString
替换指定范围的字符串。
replaceString(start: number , length: number , other: string): void
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
start | number | 是 | 指定范围的下标。 |
length | number | 是 | 指定范围的长度。 |
other | string | 是 | 替换的新文本内容。 说明: 替换的字符串使用的是start位置字符的样式。 |
错误码:
以下错误码详细介绍。
错误码ID | 错误信息 |
---|---|
401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3. Parameter verification failed. |
insertString
插入字符串。
insertString(start: number , other: string): void
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
start | number | 是 | 插入位置的下标。 |
other | string | 是 | 插入的新文本内容。 说明: 插入的字符串使用的是start-1位置字符的样式。若start-1位置字符未设置样式,则使用start位置字符样式。 |
错误码:
以下错误码详细介绍。
错误码ID | 错误信息 |
---|---|
401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3. Parameter verification failed. |
removeString
移除指定范围的字符串。
当属性字符串中包含图片时,同样生效。
removeString(start: number , length: number): void
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
start | number | 是 | 指定范围的下标。 |
length | number | 是 | 指定范围的长度。 |
错误码:
以下错误码详细介绍。
错误码ID | 错误信息 |
---|---|
401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3. Parameter verification failed. |
replaceStyle
替换指定范围内容为指定类型新样式。
replaceStyle(spanStyle: SpanStyle): void
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
spanStyle | SpanStyle | 是 | 样式对象。 说明: 默认清空原有样式,替换为新样式。 当SpanStyle的styledKey为IMAGE或CUSTOM_SPAN时,只有当start的位置当前是image或CustomSpan且长度为1,才会生效,其余情况无效果。 |
错误码:
以下错误码详细介绍。
错误码ID | 错误信息 |
---|---|
401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3. Parameter verification failed. |
setStyle
为指定范围内容设置指定类型新样式。
setStyle(spanStyle: SpanStyle): void
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
spanStyle | SpanStyle | 是 | 样式对象。 说明: 默认不清空原有样式,叠加新样式。若是已有样式,则更新。 当SpanStyle的styledKey为IMAGE或CUSTOM_SPAN时,只有当start的位置当前是image或者CustomSpan且长度为1,才会生效,其余情况无效果。 |
removeStyle
清除指定范围内容的指定类型样式。
被清空样式类型对象属性使用的是对应Text组件属性的设置值,若Text组件未设置值,则使用对应Text组件属性的默认值。
当属性字符串中包含图片时,同样生效。
removeStyle(start: number , length: number , styledKey: StyledStringKey): void
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
start | number | 是 | 指定范围开始位置的下标。 |
length | number | 是 | 指定范围的长度。 |
styledKey | StyledStringKey | 是 | 样式类型枚举值。 |
错误码:
以下错误码详细介绍。
错误码ID | 错误信息 |
---|---|
401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3. Parameter verification failed. |
removeStyles
清除指定范围内容的所有样式。
被清空样式类型对象属性使用的是对应Text组件属性的设置值,若Text组件未设置值,则使用对应Text组件属性的默认值。
当属性字符串中包含图片时,同样生效。
removeStyles(start: number , length: number): void
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
start | number | 是 | 指定范围开始位置的下标。 |
length | number | 是 | 指定范围的长度。 |
错误码:
以下错误码详细介绍。
错误码ID | 错误信息 |
---|---|
401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3. Parameter verification failed. |
clearStyles
清除属性字符串对象的所有样式。
被清空样式类型对象属性使用的是对应Text组件属性的设置值,若Text组件未设置值,则使用对应Text组件属性的默认值。
clearStyles(): void
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full