HarmonyOS实战开发-如何通过Text实现部分文本高亮和超链接。

news/2024/9/22 23:08:58/

介绍

本示例通过自定义Span类型,在Text组件中使用ForEach遍历,根据不同的Span类型生成不同样式和功能的Span组件,实现部分文本高亮和超链接。

效果图预览

在这里插入图片描述

使用说明

  1. 点击超链接,根据链接类型出现相应提示弹窗。
  2. 长按消息卡片出现提示弹窗。

实现思路

1.定义 CustomSpanType 枚举类型,此处定义了 Normal、Hashtag、Mention、VideoLink 和 DetailLink 五种类型。

export enum CustomSpanType {Normal, // 普通文本,不含任何特殊格式或标记Hashtag, // 话题标签Mention, // @提及VideoLink, // 视频链接DetailLink // 正文详情
}

2.创建 CustomSpan 数据类,用于表示不同类型的 Span 对象。

export class CustomSpan {type: CustomSpanType; // 文本类型content: string; // 文本内容url?: string; // 跳转的链接地址constructor(type: CustomSpanType = CustomSpanType.Normal, content: string, url?: string) {this.type = type;this.content = content;if (url) {this.url = url;}}
}

3.使用 Text 组件结合 ForEach 方法遍历 spans 中的 CustomSpan 对象,根据不同的 Span 类型生成不同样式和功能的 Span 组件。

Text() {ForEach(this.spans, (item: CustomSpan) => {if (item.type === CustomSpanType.Normal) {Span(item.content).fontSize($r('app.string.ohos_id_text_size_body1'))} else if (item.type === CustomSpanType.Hashtag || item.type === CustomSpanType.Mention || item.type === CustomSpanType.DetailLink) {TextLinkSpan({ item: item })} else {VideoLinkSpan({ item: item })}})
}
.width($r('app.string.styled_text_layout_100'))
.fontSize($r('app.string.ohos_id_text_size_body1'))
.margin({ top: $r('app.string.ohos_id_card_margin_start') })

4.对于 Normal 类型的 Span,直接使用 Span 组件展示文本内容,并设置相应的样式。

Span(item.content).fontSize($r('app.string.ohos_id_text_size_body1'))

5.对于 Hashtag、Mention 和 DetailLink 类型的 Span,在 TextLinkSpan 组件中添加带有超链接功能的 Span 组件,根据 CustomSpan 的类型和内容,实现对应的样式和交互功能,例如显示提示信息或执行其他操作。

@Component
struct TextLinkSpan {@State linkBackgroundColor: Color | Resource = Color.Transparent; // 超链接背景色private item: CustomSpan = new CustomSpan(CustomSpanType.Normal, '');@State myItem: CustomSpan = this.item;aboutToAppear(): void {// LazyForEach中Text组件嵌套自定义组件会有数据初次不渲染问题,异步修改状态变量更新视图setTimeout(() => {this.myItem = this.item;})}build(){Span(this.myItem.content).fontColor($r('app.color.styled_text_link_font_color'))// 超链接字体颜色.fontSize($r('app.string.ohos_id_text_size_body1')).textBackgroundStyle({ color: this.linkBackgroundColor }).onClick(() => {this.linkBackgroundColor = $r('app.color.styled_text_link_clicked_background_color'); // 点击后的背景色setTimeout(() => {this.linkBackgroundColor = Color.Transparent;}, BACKGROUND_CHANGE_DELAY)// 根据文本超链接的类型做相应处理if (this.myItem.type === CustomSpanType.Hashtag) {promptAction.showToast({message: $r('app.string.styled_text_hashtag_toast_message')});} else if (this.myItem.type === CustomSpanType.Mention) {promptAction.showToast({message: $r('app.string.styled_text_user_page_toast_message')});} else {promptAction.showToast({message: $r('app.string.styled_text_content_details_toast_message')});}})}
}

6.对于 VideoLink 类型的 Span,使用 VideoLinkSpan 组件添加图标和超链接功能,在点击事件中显示提示信息或执行跳转视频页操作。

@Component
struct VideoLinkSpan {@State linkBackgroundColor: Color | Resource = Color.Transparent;private item: CustomSpan = new CustomSpan(CustomSpanType.Normal, '');@State myItem: CustomSpan = this.item;aboutToAppear(): void {// LazyForEach中Text组件嵌套自定义组件会有数据初次不渲染问题,异步修改状态变量更新视图setTimeout(() => {this.myItem = this.item;})}build() {ContainerSpan() {ImageSpan($r('app.media.styled_text_ic_public_video')).height($r('app.integer.styled_text_video_link_icon_height')).verticalAlign(ImageSpanAlignment.CENTER)Span(this.myItem.content).fontColor($r('app.color.styled_text_link_font_color')).fontSize($r('app.string.ohos_id_text_size_body1')).onClick(() => {this.linkBackgroundColor = $r('app.color.styled_text_link_clicked_background_color');setTimeout(() => {this.linkBackgroundColor = Color.Transparent;}, BACKGROUND_CHANGE_DELAY)promptAction.showToast({message: $r('app.string.styled_text_video_function_message')});})}.textBackgroundStyle({ color: this.linkBackgroundColor })}
}

高性能知识点

本示例使用了LazyForEach进行数据懒加载

工程结构&模块类型

styledtext                                   // har类型
|---/src/main/ets/mock                        
|   |---MockData.ets                         // mock数据
|---/src/main/ets/model                        
|   |---DataSource.ets                       // 列表数据模型                        
|   |---TextModel.ets                        // 数据类型定义
|---/src/main/ets/pages                        
|   |---StyledText.ets                       // 视图层-主页面

模块依赖

  1. 本实例依赖common模块中的资源文件。
  2. 本示例依赖动态路由模块来实现页面的动态加载。

如果大家还没有掌握鸿蒙,现在想要在最短的时间里吃透它,我这边特意整理了《鸿蒙语法ArkTS、TypeScript、ArkUI等…视频教程》以及《鸿蒙开发学习手册》(共计890页),希望对大家有所帮助:https://docs.qq.com/doc/DZVVBYlhuRkZQZlB3

鸿蒙语法ArkTS、TypeScript、ArkUI等…视频教程:https://docs.qq.com/doc/DZVVBYlhuRkZQZlB3

在这里插入图片描述

OpenHarmony APP开发教程步骤:https://docs.qq.com/doc/DZVVBYlhuRkZQZlB3

在这里插入图片描述

鸿蒙开发学习手册》:

如何快速入门:https://docs.qq.com/doc/DZVVBYlhuRkZQZlB3

1.基本概念
2.构建第一个ArkTS应用
3.……

在这里插入图片描述

开发基础知识:https://docs.qq.com/doc/DZVVBYlhuRkZQZlB3

1.应用基础知识
2.配置文件
3.应用数据管理
4.应用安全管理
5.应用隐私保护
6.三方应用调用管控机制
7.资源分类与访问
8.学习ArkTS语言
9.……

在这里插入图片描述

基于ArkTS 开发:https://docs.qq.com/doc/DZVVBYlhuRkZQZlB3

1.Ability开发
2.UI开发
3.公共事件与通知
4.窗口管理
5.媒体
6.安全
7.网络与链接
8.电话服务
9.数据管理
10.后台任务(Background Task)管理
11.设备管理
12.设备使用信息统计
13.DFX
14.国际化开发
15.折叠屏系列
16.……

在这里插入图片描述

鸿蒙生态应用开发白皮书V2.0PDF:https://docs.qq.com/doc/DZVVkRGRUd3pHSnFG

在这里插入图片描述


http://www.ppmy.cn/news/1456464.html

相关文章

订单超时自动取消的实践方案

1、定时任务方案 方案流程: 每隔 30 秒查询数据库,取出最近的 N 条未支付的订单。 遍历查询出来的订单列表,判断当前时间减去订单的创建时间是否超过了支付超时时间,如果超时则对该订单执行取消操作。 定时任务方案工程实现相…

js宏任务微任务输出解析

第一种情况 setTimeout(function () {console.log(setTimeout 1) //11 宏任务new Promise(function (resolve) {console.log(promise 1) //12 同步函数resolve()}).then(function () {console.log(promise then) //13 微任务})})async function async1() {console.log(async1 s…

设计模式 基本认识

文章目录 设计模式的作用设计模式三原则设计模式与类图设计模式的分类 设计模式的作用 设计模式是在软件设计过程中针对常见问题的解决方案的一种通用、可重用的解决方案。设计模式提供了一种经过验证的方法,可以帮助开发人员解决特定类型的问题,并在软…

东郊到家:传承文化瑰宝,持续高质发展

中医推拿作为非物质文化的一种,是历史长河中世代相传的宝贵财富。随着科技进步及社会发展,不少非物质文化遗产逐渐淡出了我们的视线,但中医推拿始终与我们的生活朝夕相伴,为缓解身体的亚健康状态做出巨大贡献。 在高速发展的大健…

JavaScript:Web APIs(三)

本篇文章的内容包括: 一,事件流 二,移除事件监听 三,其他事件 四,元素尺寸与位置 一,事件流 事件流是什么呢? 事件流是指事件执行过程中的流动路径。 我们发现,一个完整的事件执行…

机械类外文 翻译

随着科技的日新月异,机械工程领域也在不断蜕变,为了更好地与世界接轨,对外文资料的准确翻译显得尤为重要。那么,那么,关于机械类的外文翻译,如何保证译文的质量,哪个翻译公司在北京更为专业呢&a…

dockerk8s常用知识点

1、什么是docker 容器化和虚拟化对比 ▪开源的应用容器引擎,基于 Go 语言开发 ▪容器是完全使用沙箱机制,容器开销极低 ▪Docker就是容器化技术的代名词 ▪Docker也具备一定虚拟化职能 docker三大核心: Docker Engine: 提供了一个可以用来运行和管…

Zabbix+Grafana-常见报错及异常处理方式记录

文章目录 Zabbix安装篇Zabbix Web页面连接数据库失败 Zabbix使用篇中文显示不全 Zabbix报警篇新建的用户,配置报警后,无法收到报警 Grafana安装篇Windows系统安装时,添加zabbix报错:An error occurred within the plugin Zabbix安…