立聪堂德州十三局店 发表于 2024-12-6 11:01:09

HarmonyOS NEXT 使用Text实现部分文本高亮和超链接样式

本示例通过自定义Span范例,在Text组件中使用ForEach遍历,根据差别的Span范例生成差别样式和功能的Span组件,实现部分文本高亮和超链接。
效果图预览


https://i-blog.csdnimg.cn/blog_migrate/104a413eb5912f6689c3375d7b7a25e5.gif
使用说明

[*]点击超链接,根据链接范例出现相应提示弹窗。
[*]长按消息卡片出现提示弹窗。
实现思绪


[*]定义 CustomSpanType 枚举范例,此处定义了 Normal、Hashtag、Mention、VideoLink 和 DetailLink 五种范例。
export enum CustomSpanType {
Normal, // 普通文本,不含任何特殊格式或标记
Hashtag, // 话题标签
Mention, // @提及
VideoLink, // 视频链接
DetailLink // 正文详情
}
[*]创建 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;
    }
}
}
[*]使用 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') })
[*]对于 Normal 范例的 Span,直接使用 Span 组件展示文本内容,并设置相应的样式。
Span(item.content)
.fontSize($r('app.string.ohos_id_text_size_body1'))
[*]对于 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')
          });
      }
      })
}
}
[*]对于 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                     // 视图层-主页面 模块依赖


[*]本实例依赖common模块中的资源文件。
[*]本示例依赖动态路由模块来实现页面的动态加载。
末了

有很多小同伴不知道学习哪些鸿蒙开发技术?不知道必要重点掌握哪些鸿蒙应用开发知识点?而且学习时频繁踩坑,最终浪费大量时间。以是有一份实用的鸿蒙(HarmonyOS NEXT)资料用来跟着学习黑白常有必要的。 
这份鸿蒙(HarmonyOS NEXT)资料包罗了鸿蒙开发必掌握的焦点知识要点,内容包罗了(ArkTS、ArkUI开发组件、Stage模型、多端摆设、分布式应用开发、音频、视频、WebGL、OpenHarmony多媒体技术、Napi组件、OpenHarmony内核、Harmony南向开发、鸿蒙项目实战等等)鸿蒙(HarmonyOS NEXT)技术知识点。
希望这一份鸿蒙学习资料可以或许给大家带来帮助,有必要的小同伴自行领取,限时开源,先到先得~无套路领取!!
如果你是一名有经验的资深Android移动开发、Java开发、前端开发、对鸿蒙感兴趣以及转行职员,可以直接领取这份资料
 获取这份完整版高清学习门路,请点击→纯血版全套鸿蒙HarmonyOS学习资料
鸿蒙(HarmonyOS NEXT)最新学习门路

https://i-blog.csdnimg.cn/blog_migrate/15a41c202968249002b9e71d35d047b0.png


[*]  HarmonOS底子技能
https://i-blog.csdnimg.cn/blog_migrate/d15791a8d9157b12cf470e6e20e199cd.png


[*]HarmonOS就业必备技能 https://i-blog.csdnimg.cn/blog_migrate/219a3f80b24947602f5ce80869c0f0d1.png
[*] HarmonOS多媒体技术
https://i-blog.csdnimg.cn/blog_migrate/f76470c4171a878d5f6071f6d009b828.png


[*]鸿蒙NaPi组件进阶
https://i-blog.csdnimg.cn/blog_migrate/8755cfeac3a887ec19fb168f7164234c.png


[*]HarmonOS高级技能
https://i-blog.csdnimg.cn/blog_migrate/1e210328c1ebf43f08d3b6c5f5c13b48.png


[*]初识HarmonOS内核 https://i-blog.csdnimg.cn/blog_migrate/24384aae472f78c055f7bf545467db7f.png
[*]实战就业级装备开发
https://i-blog.csdnimg.cn/blog_migrate/9b0c4eb9a30ca059d28eceb4727b593e.png
 有了门路图,怎么能没有学习资料呢,小编也预备了一份联合鸿蒙官方发布条记整理收纳的一套系统性的鸿蒙(OpenHarmony )学习手册(共计1236页)与鸿蒙(OpenHarmony )开发入门讲授视频,内容包罗:ArkTS、ArkUI、Web开发、应用模型、资源分类…等知识点。
获取以上完整版高清学习门路,请点击→纯血版全套鸿蒙HarmonyOS学习资料
《鸿蒙 (OpenHarmony)开发入门讲授视频》

https://i-blog.csdnimg.cn/blog_migrate/9b893c2a4956d6ce72b63212f9216eab.png
《鸿蒙生态应用开发V2.0白皮书》

https://i-blog.csdnimg.cn/blog_migrate/343917b8910aff13b48316fe3db39da6.jpeg
《鸿蒙 (OpenHarmony)开发底子到实战手册》

OpenHarmony北向、南向开发环境搭建
https://i-blog.csdnimg.cn/blog_migrate/2e05bbd2cb0f05ec297905ba57f803c2.png
 《鸿蒙开发底子》



[*]ArkTS语言
[*]安装DevEco Studio
[*]运用你的第一个ArkTS应用
[*]ArkUI声明式UI开发
[*].……
https://i-blog.csdnimg.cn/blog_migrate/48e16dbee395ba8de1264149330856d4.png
 《鸿蒙开发进阶》



[*]Stage模型入门
[*]网络管理
[*]数据管理
[*]电话服务
[*]分布式应用开发
[*]通知与窗口管理
[*]多媒体技术
[*]安全技能
[*]任务管理
[*]WebGL
[*]国际化开发
[*]应用测试
[*]DFX面向将来设计
[*]鸿蒙系统移植和裁剪定制
[*]……
https://i-blog.csdnimg.cn/blog_migrate/adf72eca696abc1415cd768fceda1af7.png
《鸿蒙进阶实战》



[*]ArkTS实践
[*]UIAbility应用
[*]网络案例
[*]……
https://i-blog.csdnimg.cn/blog_migrate/c077a46bb540fc657713d99f39e9c74c.png
 获取以上完整鸿蒙HarmonyOS学习资料,请点击→纯血版全套鸿蒙HarmonyOS学习资料
总结

总的来说,华为鸿蒙不再兼容安卓,对中年程序员来说是一个挑战,也是一个时机。只有积极应对变革,不断学习和提升自己,他们才能在这个变革的时代中立于不败之地。 
https://i-blog.csdnimg.cn/blog_migrate/926f3f942f098baebec15ecf79df9ca9.png


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: HarmonyOS NEXT 使用Text实现部分文本高亮和超链接样式