HarmonyOS(鸿蒙)——Text(文本)组件介绍

本文详细介绍了HarmonyOS中Text组件的各种属性,包括text_size、text_color、text_font、italic、text_weight等,涵盖了字体大小、颜色、字体样式、字重等方面,通过代码示例展示了如何设置和使用这些属性,帮助开发者更好地理解和应用Text组件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

| text_size | 文本大小 | float类型 | 表示字体大小的float类型。可以是浮点数值,其默认单位为px;也可以是带px/vp/fp单位的浮点数值;也可以引用float资源。 | ohos:text_size=“30"ohos:text_size=“16fp"ohos:text_size=”$float:size_value” |

代码演示:

<Text

ohos:id=“$+id:text”

ohos:height=“match_content”

ohos:width=“match_content”

ohos:text=“150px”

ohos:text_size=“150px”/>

显示效果:

在这里插入图片描述

在这里插入图片描述

2.3 text_color

| 属性名称 | 中文描述 | 取值 | 取值说明 | 使用案例 |

| — | — | — | — | — |

| text_color | 文本颜色 | color类型 | 可以直接设置色值,也可以引用color资源。 | ohos:text_color=“#A8FFFFFF"ohos:text_color=”$color:black" |

代码演示:

<Text

ohos:id=“$+id:text”

ohos:height=“match_content”

ohos:width=“match_content”

ohos:text=“blue”

ohos:text_size=“100px”

ohos:text_color=“blue”

/>

演示效果:

在这里插入图片描述

使用HEX效果:

在这里插入图片描述

2.4 text_font

| 属性名称 | 中文描述 | 取值 | 取值说明 | 使用案例 |

| — | — | — | — | — |

| text_font | 字体 | sans-serif | 可以设置的字体如表中所列。 | ohos:text_font=“HwChinese-medium” |

| | | sans-serif-medium | | |

| | | HwChinese-medium | | |

| | | sans-serif-condensed | | |

| | | sans-serif-condensed-medium | | |

| | | monospace | | |

代码演示:

<Text

ohos:id=“$+id:text”

ohos:height=“match_content”

ohos:width=“match_content”

ohos:text=“李子捌”

ohos:text_size=“100px”

ohos:text_color=“#000000”

ohos:text_font=“HwChinese-medium”

/>

演示效果:

在这里插入图片描述

2.5 italic

| 属性名称 | 中文描述 | 取值 | 取值说明 | 使用案例 |

| — | — | — | — | — |

| italic | 文本是否斜体字体 | boolean类型 | 可以直接设置true/false,也可以引用boolean资源。 | ohos:italic=“true"ohos:italic=”$boolean:true" |

代码演示:

<Text

ohos:id=“$+id:text”

ohos:height=“match_content”

ohos:width=“match_content”

ohos:text=“李子捌”

ohos:text_size=“100px”

ohos:text_color=“#000000”

ohos:text_font=“sans-serif”

ohos:italic=“true”

/>

演示效果:

在这里插入图片描述

2.6 text_weight

| 属性名称 | 中文描述 | 取值 | 取值说明 | 使用案例 |

| — | — | — | — | — |

| text_weight | 字重 | integer类型 | 表示字体大小的integer类型。也可以引用integer资源。 | ohos:text_weight=“100"ohos:text_weight=”$integer:100" |

代码演示:

<Text

ohos:id=“$+id:text”

ohos:height=“match_content”

ohos:width=“match_content”

ohos:text=“Liziba”

ohos:text_size=“100px”

ohos:text_color=“#0000FF”

ohos:text_font=“serif”

ohos:italic=“true”

ohos:text_weight=“100”

/>

演示效果:

text_weight = 100

在这里插入图片描述

text_weight = 600

在这里插入图片描述

2.7 background_element

常用的背景如常见的文本背景、按钮背景,可以采用XML格式放置在graphic目录下。

在Project窗口,打开“entry > src > main > resources > base”,右键点击“graphic”文件夹,选择“New > File”,命名为“background_text.xml”,在background_text.xml中定义文本的背景。

代码演示:

<?xml version="1.0" encoding="UTF-8" ?>

<shape xmlns:ohos=“http://schemas.huawei.com/res/ohos”

ohos:shape=“rectangle”>

演示效果:

在这里插入图片描述

2.8 text_alignment

| 属性名称 | 中文描述 | 取值 | 取值说明 | 使用案例 |

| — | — | — | — | — |

| text_alignment | 文本对齐方式 | left | 表示文本靠左对齐。 | 可以设置取值项如表中所列,也可以使用“ |

| | | top | 表示文本靠顶部对齐。 | |

| | | right | 表示文本靠右对齐。 | |

| | | bottom | 表示文本靠底部对齐。 | |

| | | horizontal_center | 表示文本水平居中对齐。 | |

| | | vertical_center | 表示文本垂直居中对齐。 | |

| | | center | 表示文本居中对齐。 | |

| | | start | 表示文本靠起始端对齐。 | |

| | | end | 表示文本靠结尾端对齐。 | |

代码演示:

<Text

ohos:id=“$+id:text”

ohos:width=“300vp”

ohos:height=“100vp”

ohos:text=“Liziba”

ohos:text_size=“100px”

ohos:text_color=“#0000FF”

ohos:text_font=“serif”

ohos:italic=“true”

ohos:text_weight=“100”

ohos:left_margin=“15vp”

ohos:bottom_margin=“15vp”

ohos:right_padding=“15vp”

ohos:left_padding=“15vp”

ohos:text_alignment=“bottom|horizontal_center”

ohos:background_element=“$graphic:background_text”

/>

演示效果:

在这里插入图片描述

2.9 multiple_lines

| 属性名称 | 中文描述 | 取值 | 取值说明 | 使用案例 |

| — | — | — | — | — |

| multiple_lines | 多行模式设置 | boolean类型 | 可以直接设置true/false,也可以引用boolean资源。 | ohos:multiple_lines=“true"ohos:multiple_lines=”$boolean:true" |

代码演示:

<Text

ohos:id=“$+id:text”

ohos:width=“300px”

ohos:height=“match_content”

ohos:text=“LizibaLiziba”

ohos:text_size=“100px”

ohos:text_color=“#0000FF”

ohos:text_font=“serif”

ohos:italic=“true”

ohos:text_weight=“100”

ohos:max_text_lines=“2”

ohos:multiple_lines=“true”

ohos:background_element=“$graphic:background_text”

/>

演示效果:

在这里插入图片描述

2.10 max_text_lines

| 属性名称 | 中文描述 | 取值 | 取值说明 | 使用案例 |

| — | — | — | — | — |

| max_text_lines | 文本最大行数 | integer类型 | 可以直接设置整型数值,也可以引用integer资源。 | ohos:max_text_lines=“2"ohos:max_text_lines=”$integer:two" |

代码演示:

<Text

ohos:id=“$+id:text”

ohos:width=“300px”

ohos:height=“match_content”

ohos:text=“LizibaLizibaLiziba”

ohos:text_size=“100px”

ohos:text_color=“#0000FF”

ohos:text_font=“serif”

ohos:italic=“true”

ohos:text_weight=“100”

ohos:multiple_lines=“true”

ohos:max_text_lines=“2”

ohos:background_element=“$graphic:background_text”

/>

演示效果:

在这里插入图片描述

2.11 auto_font_size

| 属性名称 | 中文描述 | 取值 | 取值说明 | 使用案例 |

| — | — | — | — | — |

| auto_font_size | 是否支持文本自动调整文本字体大小 | boolean类型 | 可以直接设置true/false,也可以引用boolean资源。 | ohos:auto_font_size=“true"ohos:auto_font_size=”$boolean:true" |

代码演示:

方法一:

<Text

ohos:id=“$+id:text”

ohos:width=“300px”

ohos:height=“match_content”

ohos:text=“LizibaLizibaLiziba”

ohos:text_size=“100px”

ohos:text_color=“#0000FF”

ohos:text_font=“serif”

ohos:italic=“true”

ohos:text_weight=“100”

ohos:max_text_lines=“1”

ohos:auto_font_size=“true”

ohos:background_element=“$graphic:background_text”

/>

演示效果:

在这里插入图片描述

方法二:

### HarmonyOS 父子组件间传递参数 在 HarmonyOS 中,父子组件间的参数传递主要通过 `@Prop` 装饰器以及特定的状态管理机制来完成。对于单向数据流而言,父组件可以通过属性的形式将数据传递给子组件,在此过程中,子组件利用 `@Prop` 来接收来自父级的数据。 #### 单项数据流动 为了使父组件能向子组件传递消息并让其响应变化,可以在父组件定义一个可变变量 message 并将其作为 prop 属性传入子组件中[^2]: ```xml <!-- ParentComponent.ets --> <template> <div> <!-- 子组件实例化时传递message值 --> <ChildComponent message="{{parentMessage}}"/> <button onClick="updateMessage">Update Message</button> </div> </template> <script> export default { data: () => ({ parentMessage: 'Hello from Parent' }), methods:{ updateMessage(){ this.parentMessage = "Updated message" } } } </script> ``` 而对应的子组件则需声明接受该属性,并可通过监听属性的变化来进行相应的界面刷新或其他操作: ```typescript // ChildComponent.ts import { Prop } from '@ohos/ace'; @Component({ props: ['message'] }) export default class ChildComponent extends Vue { @Prop() readonly message!: string; render() { return ( <div>{this.message}</div> // 显示接收到的消息 ); } } ``` 上述代码展示了如何设置一个简单的单项通信链路,其中父组件负责维护状态并通过属性分发至子组件;每当父组件内部 state 发生变动,则会触发重新渲染并将最新版的 message 值下发给孩子节点。 #### 双向绑定 如果希望构建更加紧密耦合的关系——即允许子组件修改由父组件提供的初始值,并且这种更改能够反馈回源头形成闭环控制结构的话,那么可以采用双向绑定模式。这通常涉及到使用额外的状态管理和事件通知机制,比如借助于 `@Observed`, `@State` 或者 `@Link` 这样的装饰器来增强交互能力[^1]: ```typescript // ParentComponent.ts import { State, Observed } from '@ohos/ability_feature'; export default class ParentComponent extends ComponentBase { @Observed() public parentMessage: string = 'Initial Value'; private handleChildChange(newValue:string){ this.parentMessage=newValue; } build() { return ( <div> {/* 将handleChildChange方法也作为一个prop传递 */} <ChildComponent value={this.parentMessage} onChange={(newValue)=>{this.handleChildChange(newValue)}} /> <Text>{this.parentMessage}</Text> </div> ) } } // ChildComponent.ts import { Prop } from '@ohos/ace'; import { Link } from '@ohos/ability_feature'; @Component({ props: ['value', 'onChange'] // 接收两个props:一个是当前显示的内容,另一个是用来更新内容的方法 }) export default class ChildComponent extends Vue { @Prop({ type: String }) readonly value!: string; @Prop({type: Function}) readonly onChange!:(newVal:String) => void; private handleChange(event:Event){ const target=event.target as HTMLInputElement; this.onChange(target.value); } render() { return ( <> <input onInput={(event:Event)=>this.handleChange(event)} defaultValue={this.value}></input> </> ); } } ``` 在这个例子中,不仅实现了从父到子的信息流通渠道,还建立了反方向上的回调路径,从而构成了完整的双向互动链条。当用户编辑 `<input>` 字段内的文本时,新的字符串会被实时同步回到父组件所持有的原始副本之中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值