鸿蒙UI(ArkUI-方舟UI框架)- 使用文本

返回主章节 → 鸿蒙UI(ArkUI-方舟UI框架)

文本使用

文本显示 (Text/Span)

Text是文本组件,通常用于展示用户视图,如显示文章的文字内容。Span则用于呈现显示行内文本。

创建文本

string字符串
	Text("我是一段文本")

在这里插入图片描述

引用Resource资源
资源引用类型可以通过**$r**创建Resource类型对象,文件位置为/resources/base/element/string.json。
	Text($r('app.string.module_desc'))
	  .baselineOffset(0)
	  .fontSize(30)
	  .border({ width: 1 })
	  .padding(10)
	  .width(300)

在这里插入图片描述

添加子组件

Span只能作为Text和RichEditor组件的子组件显示文本内容。可以在一个Text内添加多个Span来显示一段信息,例如产品说明书、承诺书等。

创建Span
Text('我是Text') {
  Span('我是Span')
}
.padding(10)
.borderWidth(1)

在这里插入图片描述

设置文本装饰线及颜色

通过decoration设置文本装饰线及颜色。

	Text() {
	  Span('我是Span1,').fontSize(16).fontColor(Color.Grey)
	    .decoration({ type: TextDecorationType.LineThrough, color: Color.Red })
	  Span('我是Span2').fontColor(Color.Blue).fontSize(16)
	    .fontStyle(FontStyle.Italic)
	    .decoration({ type: TextDecorationType.Underline, color: Color.Black })
	  Span(',我是Span3').fontSize(16).fontColor(Color.Grey)
	    .decoration({ type: TextDecorationType.Overline, color: Color.Green })
	}
	.borderWidth(1)
	.padding(10)

在这里插入图片描述

通过textCase设置文字一直保持大写或者小写状态。
	Text() {
	  Span('I am Upper-span').fontSize(12)
	    .textCase(TextCase.UpperCase)
	}
	.borderWidth(1)
	.padding(10)

在这里插入图片描述

添加事件

由于Span组件无尺寸信息,事件仅支持添加点击事件onClick。

	Text() {
	  Span('I am Upper-span').fontSize(12)
	    .textCase(TextCase.UpperCase)
	    .onClick(() => {
	      console.info('我是Span——onClick');
	    })
	}

自定义文本样式

通过textAlign属性设置文本对齐样式
	Text('左对齐')
	  .width(300)
	  .textAlign(TextAlign.Start)
	  .border({ width: 1 })
	  .padding(10)
	Text('中间对齐')
	  .width(300)
	  .textAlign(TextAlign.Center)
	  .border({ width: 1 })
	  .padding(10)
	Text('右对齐')
	  .width(300)
	  .textAlign(TextAlign.End)
	  .border({ width: 1 })
	  .padding(10)

在这里插入图片描述

通过textOverflow属性控制文本超长处理,textOverflow需配合maxLines一起使用(默认情况下文本自动折行)。
	Text('This is the setting of textOverflow to Clip text content This is the setting of textOverflow to None text content. This is the setting of textOverflow to Clip text content This is the setting of textOverflow to None text content.')
	  .width(250)
	  .textOverflow({ overflow: TextOverflow.None })
	  .maxLines(1)
	  .fontSize(12)
	  .border({ width: 1 })
	  .padding(10)
	Text('我是超长文本,超出的部分显示省略号。I am an extra long text, with ellipses displayed for any excess。')
	  .width(250)
	  .textOverflow({ overflow: TextOverflow.Ellipsis })
	  .maxLines(1)
	  .fontSize(12)
	  .border({ width: 1 })
	  .padding(10)
	Text('当文本溢出其尺寸时,文本将滚动显示。When the text overflows its dimensions, the text will scroll for displaying.')       
	  .width(250)
	  .textOverflow({ overflow: TextOverflow.MARQUEE })                 
	  .maxLines(1)       
	  .fontSize(12)
	  .border({ width: 1 })
	  .padding(10)                       

在这里插入图片描述

通过lineHeight属性设置文本行高
	Text('This is the text with the line height set. This is the text with the line height set.')
	  .width(300).fontSize(12).border({ width: 1 }).padding(10)
	Text('This is the text with the line height set. This is the text with the line height set.')
	  .width(300).fontSize(12).border({ width: 1 }).padding(10)
	  .lineHeight(20)

在这里插入图片描述

通过decoration属性设置文本装饰线样式及其颜色
	Text('This is the text')
	  .decoration({
	    type: TextDecorationType.LineThrough,
	    color: Color.Red
	  })
	  .borderWidth(1).padding(10).margin(5)
	Text('This is the text')
	  .decoration({
	    type: TextDecorationType.Overline,
	    color: Color.Red
	  })
	  .borderWidth(1).padding(10).margin(5)
	Text('This is the text')
	  .decoration({
	    type: TextDecorationType.Underline,
	    color: Color.Red
	  })
	  .borderWidth(1).padding(10).margin(5)

在这里插入图片描述

通过baselineOffset属性设置文本基线的偏移量
	Text('This is the text content with baselineOffset 0.')
	  .baselineOffset(0)
	  .fontSize(12)
	  .border({ width: 1 })
	  .padding(10)
	  .width('100%')
	  .margin(5)
	Text('This is the text content with baselineOffset 30.')
	  .baselineOffset(30) //基线往上偏移
	  .fontSize(12)
	  .border({ width: 1 })
	  .padding(10)
	  .width('100%')
	  .margin(5)
	Text('This is the text content with baselineOffset -20.')
	  .baselineOffset(-20) //基线往下偏移
	  .fontSize(12)
	  .border({ width: 1 })
	  .padding(10)
	  .width('100%')
	  .margin(5)

在这里插入图片描述

通过letterSpacing属性设置文本字符间距
	Text('This is the text content with letterSpacing 0.')
	  .letterSpacing(0)
	  .fontSize(12)
	  .border({ width: 1 })
	  .padding(10)
	  .width('100%')
	  .margin(5)
	Text('This is the text content with letterSpacing 3.')
	  .letterSpacing(3) //字符间距变大
	  .fontSize(12)
	  .border({ width: 1 })
	  .padding(10)
	  .width('100%')
	  .margin(5)
	Text('This is the text content with letterSpacing -1.')
	  .letterSpacing(-1) //字符间距变小
	  .fontSize(12)
	  .border({ width: 1 })
	  .padding(10)
	  .width('100%')
	  .margin(5)

在这里插入图片描述

通过minFontSize与maxFontSize自适应字体大小

minFontSize用于设置文本的最小显示字号maxFontSize用于设置文本的最大显示字号。这两个属性必须同时设置才能生效,并且需要与maxLines属性或布局大小限制配合使用,单独设置任一属性将不会产生效果。

	Text('我的最大字号为30,最小字号为5,宽度为250,maxLines为1')
	  .width(250)
	  .maxLines(1)
	  .maxFontSize(30)
	  .minFontSize(5)
	  .border({ width: 1 })
	  .padding(10)
	  .margin(5)
	Text('我的最大字号为30,最小字号为5,宽度为250,maxLines为2')
	  .width(250)
	  .maxLines(2)
	  .maxFontSize(30)
	  .minFontSize(5)
	  .border({ width: 1 })
	  .padding(10)
	  .margin(5)
	Text('我的最大字号为30,最小字号为15,宽度为250,高度为50')
	  .width(250)
	  .height(50)
	  .maxFontSize(30)
	  .minFontSize(15)
	  .border({ width: 1 })
	  .padding(10)
	  .margin(5)
	Text('我的最大字号为30,最小字号为15,宽度为250,高度为100')
	  .width(250)
	  .height(100)
	  .maxFontSize(30)
	  .minFontSize(15)
	  .border({ width: 1 })
	  .padding(10)
	  .margin(5)

在这里插入图片描述

通过textCase属性设置文本大小写
	Text('This is the text content with textCase set to Normal.')
	  .textCase(TextCase.Normal)
	  .padding(10)
	  .border({ width: 1 })
	  .padding(10)
	  .margin(5)
	// 文本全小写展示
	Text('This is the text content with textCase set to LowerCase.')
	  .textCase(TextCase.LowerCase)
	  .border({ width: 1 })
	  .padding(10)
	  .margin(5)
	// 文本全大写展示
	Text('This is the text content with textCase set to UpperCase.')
	  .textCase(TextCase.UpperCase)
	  .border({ width: 1 })
	  .padding(10)
	  .margin(5)

在这里插入图片描述

通过copyOption属性设置文本是否可复制粘贴
	Text("这是一段可复制文本")
	  .fontSize(30)
	  .copyOption(CopyOptions.InApp)

在这里插入图片描述

添加事件

Text组件可以添加通用事件,可以绑定onClickonTouch等事件来响应操作。

Text('点我')
  .onClick(() => {
      console.info('我是Text的点击响应事件');
   })

场景示例

该示例通过maxLines、textOverflow、textAlign、constraintSize属性展示了热搜榜的效果。

// xxx.ets
@Entry
@Component
struct TextExample {
  build() {
    Column() {
      Row() {
        Text("1").fontSize(14).fontColor(Color.Red).margin({ left: 10, right: 10 })
        Text("我是热搜词条1")
          .fontSize(12)
          .fontColor(Color.Blue)
          .maxLines(1)
          .textOverflow({ overflow: TextOverflow.Ellipsis })
          .fontWeight(300)
        Text("爆")
          .margin({ left: 6 })
          .textAlign(TextAlign.Center)
          .fontSize(10)
          .fontColor(Color.White)
          .fontWeight(600)
          .backgroundColor(0x770100)
          .borderRadius(5)
          .width(15)
          .height(14)
      }.width('100%').margin(5)

      Row() {
        Text("2").fontSize(14).fontColor(Color.Red).margin({ left: 10, right: 10 })
        Text("我是热搜词条2 我是热搜词条2 我是热搜词条2 我是热搜词条2 我是热搜词条2")
          .fontSize(12)
          .fontColor(Color.Blue)
          .fontWeight(300)
          .constraintSize({ maxWidth: 200 })
          .maxLines(1)
          .textOverflow({ overflow: TextOverflow.Ellipsis })
        Text("热")
          .margin({ left: 6 })
          .textAlign(TextAlign.Center)
          .fontSize(10)
          .fontColor(Color.White)
          .fontWeight(600)
          .backgroundColor(0xCC5500)
          .borderRadius(5)
          .width(15)
          .height(14)
      }.width('100%').margin(5)

      Row() {
        Text("3").fontSize(14).fontColor(Color.Orange).margin({ left: 10, right: 10 })
        Text("我是热搜词条3")
          .fontSize(12)
          .fontColor(Color.Blue)
          .fontWeight(300)
          .maxLines(1)
          .constraintSize({ maxWidth: 200 })
          .textOverflow({ overflow: TextOverflow.Ellipsis })
        Text("热")
          .margin({ left: 6 })
          .textAlign(TextAlign.Center)
          .fontSize(10)
          .fontColor(Color.White)
          .fontWeight(600)
          .backgroundColor(0xCC5500)
          .borderRadius(5)
          .width(15)
          .height(14)
      }.width('100%').margin(5)

      Row() {
        Text("4").fontSize(14).fontColor(Color.Grey).margin({ left: 10, right: 10 })
        Text("我是热搜词条4 我是热搜词条4 我是热搜词条4 我是热搜词条4 我是热搜词条4")
          .fontSize(12)
          .fontColor(Color.Blue)
          .fontWeight(300)
          .constraintSize({ maxWidth: 200 })
          .maxLines(1)
          .textOverflow({ overflow: TextOverflow.Ellipsis })
      }.width('100%').margin(5)
    }.width('100%')
  }
}

在这里插入图片描述

文本输入 (TextInput/TextArea)

TextInput、TextArea是输入框组件,通常用于响应用户的输入操作,比如评论区的输入、聊天框的输入、表格的输入等,也可以结合其它组件构建功能页面,例如登录注册页面。具体用法请参考TextInput、TextArea。

创建输入框

设置输入框类型

自定义样式

添加事件

场景示例

富文本(RichEditor)

RichEditor是支持图文混排和文本交互式编辑的组件,通常用于响应用户对图文混合内容的输入操作,例如可以输入图文的评论区。具体用法参考RichEditor。

图标小符号 (SymbolGlyph/SymbolSpan)

属性字符串(StyledString/MutableStyledString)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/966111.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

科技赋能数字内容体验的核心技术探索

内容概要 在数字化时代,科技的迅猛发展为我们的生活和工作带来了深刻的变革。数字内容体验已经成为人们获取信息和娱乐的重要途径,而这背后的技术支持则扮演着至关重要的角色。尤其是在人工智能、虚拟现实和区块链等新兴技术的推动下,数字内…

详细教程 | 如何使用DolphinScheduler调度Flink实时任务

Apache DolphinScheduler 非常适用于实时数据处理场景,尤其是与 Apache Flink 的集成。DolphinScheduler 提供了丰富的功能,包括任务依赖管理、动态调度、实时监控和日志管理,能够有效简化 Flink 实时任务的管理和部署。通过 DolphinSchedule…

棋盘(二维差分)

题目: 5396. 棋盘 题目 提交记录 讨论 题解 视频讲解 小蓝拥有 nnnn 大小的棋盘,一开始棋盘上全都是白子。 小蓝进行了 mm 次操作,每次操作会将棋盘上某个范围内的所有棋子的颜色取反(也就是白色棋子变为黑色,黑色棋子变…

MySQL数据库基础(创建/删除 数据库/表)

一、数据库的操作 1.1 显示当前数据库 语法&#xff1a;show databases&#xff1b; <1>show 是一个关键字&#xff0c;表示要执行的操作类型 <2>databases 是复数&#xff0c;表示显示所有数据库 上面的数据库中&#xff0c;除了java113&#xff0c;其它的数据库…

【WebLogic】Oracle发布WebLogic 14c最新版本-14.1.2.0

根据Oracle官方产品经理的博客&#xff0c;Oracle于2024年12月20日正式对外发布了WebLogic 14c的第二个正式版本&#xff0c;版本号为 14.1.2.0.0 &#xff0c;目前官方已开放客户端下载。该版本除继续支持 Jakarta EE 8 版本外&#xff0c;还增加了对 Java SE 17&#xff08;J…

SQL Server 数据库迁移到 MySQL 的完整指南

文章目录 引言一、迁移前的准备工作1.1 确定迁移范围1.2 评估兼容性1.3 备份数据 二、迁移工具的选择2.1 使用 MySQL Workbench2.2 使用第三方工具2.3 手动迁移 三、迁移步骤3.1 导出 SQL Server 数据库结构3.2 转换数据类型和语法3.3 导入 MySQL 数据库3.4 迁移数据3.5 迁移存…

springboot+vue导入ruoyi项目的框架

一、介绍 RuoYi-Vue版本&#xff0c;采用了前后端分离的单体架构设计软件环境&#xff1a;JDK、Mysql、Redis、Maven、Node技术选型: Spring Boot、Spring Security、MyBatis、Jwt、Vue3、Element-Plus官方地址: https://gitee.com/y_project/RuoYi-Vue 官方推荐的版本如下&a…

jvm 篇

字节码的作用 ‌跨平台性‌&#xff1a;字节码是Java实现跨平台特性的关键。Java源代码编译成字节码后&#xff0c;可以在任何安装了Java虚拟机&#xff08;JVM&#xff09;的设备上运行&#xff0c;这使得Java应用程序能够在不同的操作系统和硬件平台上运行而无需重新编译。‌…

【Springboot】Springboot 自定义线程池的参数配置最优是多少

博主介绍&#xff1a;✌全网粉丝22W&#xff0c;CSDN博客专家、Java领域优质创作者&#xff0c;掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java技术领域✌ 技术范围&#xff1a;SpringBoot、SpringCloud、Vue、SSM、HTML、Nodejs、Python、MySQL、PostgreSQL、大数据、物…

【2】Cisco SD-WAN 组件介绍

1. 概述 Cisco SD-WAN 是一套基于软件定义的广域网(SD-WAN)解决方案,能够提供安全、可扩展且高效的网络连接。它通过集中的控制和智能路径选择,实现跨多个站点的可靠性、可见性和优化。 在 Cisco SD-WAN 体系架构中,主要由四个核心组件构成: vManage(管理平面) vSmart…

测试中的第一性原理:回归本质的质量思维革命

在软件工程领域&#xff0c;测试活动常被惯性思维和经验主义所主导——测试用例库无限膨胀、自动化脚本维护成本居高不下、测试策略与业务目标渐行渐远。要突破这种困境&#xff0c;第一性原理&#xff08;First Principles Thinking&#xff09;提供了独特的解题视角&#xff…

《chatwise:DeepSeek的界面部署》

ChatWise&#xff1a;DeepSeek的界面部署 摘要 本文详细描述了DeepSeek公司针对其核心业务系统进行的界面部署工作。从需求分析到技术实现&#xff0c;再到测试与优化&#xff0c;全面阐述了整个部署过程中的关键步骤和解决方案。通过本文&#xff0c;读者可以深入了解DeepSee…

机器学习在癌症分子亚型分类中的应用

学习笔记&#xff1a;机器学习在癌症分子亚型分类中的应用——Cancer Cell 研究解析 1. 文章基本信息 标题&#xff1a;Classification of non-TCGA cancer samples to TCGA molecular subtypes using machine learning发表期刊&#xff1a;Cancer Cell发表时间&#xff1a;20…

什么是AIOps?

AIOps&#xff08;人工智能运维&#xff0c;Artificial Intelligence for IT Operations&#xff09;是通过使用人工智能&#xff08;AI&#xff09;技术来增强 IT 运维&#xff08;IT Operations&#xff09;的智能化、自动化和效率的概念。它结合了机器学习、数据分析、自动化…

使用deepseek快速创作ppt

目录 1.在DeekSeek生成PPT脚本2.打开Kimi3.最终效果 DeepSeek作为目前最强大模型&#xff0c;其推理能力炸裂&#xff0c;但是DeepSeek官方没有提供生成PPT功能&#xff0c;如果让DeepSeek做PPT呢&#xff1f; 有个途径&#xff1a;在DeepSeek让其深度思考做出PPT脚本&#xf…

DeepSeek 引领的 AI 范式转变与存储架构的演进

近一段时间&#xff0c;生成式 AI 技术经历了飞速的进步&#xff0c;尤其是在强推理模型&#xff08;Reasoning-LLM&#xff09;的推动下&#xff0c;AI 从大模型训练到推理应用的范式发生了剧变。以 DeepSeek 等前沿 AI 模型为例&#xff0c;如今的 AI 技术发展已不局限于依赖…

vscode 设置在编辑器的标签页超出可视范围时自动换行(workbench.editor.wrapTabs)

“workbench.editor.wrapTabs”: true 是 VS Code&#xff08;Visual Studio Code&#xff09; 的一个设置项&#xff0c;它的作用是 在编辑器的标签页超出可视范围时自动换行&#xff0c;而不是显示滚动条。 需要修改settings.json 参考&#xff1a;settings.json 默认值&a…

高端入门:Ollama 本地高效部署DeepSeek模型深度搜索解决方案

目录 一、Ollama 介绍 二、Ollama下载 2.1 官网下载 2.2 GitHub下载 三、模型库 四、Ollmal 使用 4.1 模型运行&#xff08;下载&#xff09; 4.2 模型提问 五、Ollama 常用命令 相关推荐 一、Ollama 介绍 Ollama是一个专为在本地机器上便捷部署和运行大型语言模型&…

前端组件标准化专家Prompt指令的最佳实践

前端组件标准化专家Prompt 提示词可作为项目自定义提示词使用&#xff0c;本次提示词偏向前端开发的使用&#xff0c;如有需要可适当修改关键词和示例 推荐使用 Cursor 中作为自定义指令使用Cline 插件中作为自定义指令使用在力所能及的范围内使用最好的模型&#xff0c;可以…

介绍10个比较优秀好用的Qt相关的开源库

记录下比较好用的一些开源库 1. Qt中的日志库“log4qt” log4qt 是一个基于 Apache Log4j 设计理念的 Qt 日志记录库&#xff0c;它为 Qt 应用程序提供了强大而灵活的日志记录功能。Log4j 是 Java 领域广泛使用的日志框架&#xff0c;log4qt 借鉴了其优秀的设计思想&#xff…