请问Text有没有相关属性,可以设置被标注的分词高亮标红显示

请问Text有没有相关属性,可以设置被标注的分词高亮标红显示。

HarmonyOS
2024-10-08 11:36:38
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
superinsect

参考demo:

export enum SpanType {  
  normal,  
  hyperlink,  
}  
export class CustomSpan {  
  public content: string | Resource  
  public type: SpanType  
  public event: () => void  
  
  constructor(content: string | Resource, type: SpanType = SpanType.normal, event: () => void = () => {  
  }) {  
    this.content = content  
    this.type = type  
    this.event = event  
  }  
  
  normal(): CustomSpan {  
    this.type = SpanType.normal  
    return this  
  }  
  
  hyperlink(event: () => void): CustomSpan {  
    this.type = SpanType.hyperlink  
    this.event = event  
    return this  
  }  
}  
  
export class CustomText {  
  public static readonly PLACEHOLDER = new RegExp('(%[1-9]{1}[0-9]{0,}\\$s)')  
  public spans: Array<CustomSpan>  
  
  constructor(spans: Array<CustomSpan> = []) {  
    this.spans = spans  
  }  
  
  append(span: CustomSpan): CustomText {  
    this.spans.push(span)  
    return this  
  }  
  
  appendNormal(text: string | Resource): CustomText {  
    this.append(new CustomSpan(text).normal())  
    return this  
  }  
  
  appendHyperlink(text: string | Resource, onClick: () => void): CustomText {  
    this.append(new CustomSpan(text).hyperlink(onClick))  
    return this  
  }  
  
  format(format: string | Resource, args: Array<CustomSpan> = []): CustomText {  
    if (!format || !args || args?.length === 0) {  
      this.appendNormal(format)  
      return this  
    }  
  
    if (typeof format === 'string') {  
      if (format.length !== 0) {  
        this.resolve(format, args)  
      }  
    } else {  
      try {  
        let value: string = getContext(this).resourceManager.getStringSync(format)  
        if (value && value.length !== 0) {  
          this.resolve(value, args)  
        }  
      } catch (e) {  
        console.warn(`CustomText getStringSync: ${JSON.stringify(format)} exception: ${e}`);  
      }  
    }  
    return this  
  }  
  
  private resolve(format: string, args: Array<CustomSpan>): CustomText {  
    let length: number = args.length  
  
    format.split(CustomText.PLACEHOLDER).forEach((value: string, index: number) => {  
      if (!value.match(CustomText.PLACEHOLDER)) {  
        this.appendNormal(value)  
        return  
      }  
      let argIndex: number = Number(value.replace('%', '').replace('$s', '')) - 1  
      if (argIndex >= length) {  
        return  
      }  
      let span: CustomSpan = args[argIndex++]  
      if (span.type === SpanType.normal) {  
        this.appendNormal(span.content)  
      } else if (span.type === SpanType.hyperlink) {  
        this.appendHyperlink(span.content, span.event)  
      }  
    })  
    return this  
  }  
}  
使用:  
// TextDemoPage.ets  
import { CustomSpan, CustomText, SpanType } from '../view/TestTen';  
  
class DeclareDemo {  
  format: string | Resource = ''  
  args: CustomSpan[] = []  
}  
  
@Entry  
@Component  
struct TextDemoPage {  
  urlRegex = /测试一下/g;  
  @State declare: DeclareDemo = {  
    format: '',  
    args: []  
  }  
  @State textDeclare: Array<CustomText> = []  
  
  aboutToAppear(): void {  
    let text: string = '这是一个示例文本测试一下。哈哈哈哈哈哈哈测试一下 哈哈哈哈哈测试一下'  
    let urls = text.match(this.urlRegex)  
    let newText: string = text  
    if(urls) {  
      let args: CustomSpan[] = []  
      urls.forEach((url, idx) => {  
        newText = newText.replace(url, `%${idx+1}$s`)  
        args.push(new CustomSpan(url).hyperlink(() => {  
          console.log('111111111111---url: ', url)  
        }),)  
      });  
      this.declare = {  
        format: newText,  
        args: args  
      }  
  
      this.textDeclare = [new CustomText().format(this.declare.format, this.declare.args)]  
      console.log('111 -this.textDeclare: ', JSON.stringify(this.textDeclare))  
    }  
  }  
  
  build() {  
    Row() {  
      Column() {  
        ForEach(this.textDeclare, (declare: CustomText, index) => {  
          Text() {  
            ForEach(declare.spans, (span: CustomSpan) => {  
              if (span.type == SpanType.normal) {  
                Span(span.content)  
                  .fontSize(14)  
                  .fontColor('#191919')  
              } else if (span.type == SpanType.hyperlink) {  
                Span(span.content)  
                  .fontSize(14)  
                  .fontColor('#007dff')  
                  .decoration({type: TextDecorationType.None, color: '#007dff'})  
                  .onClick(() => {  
                    if (span.event) {  
                      span.event()  
                    }  
                  })  
              }  
            })  
          }  
        })  
      }  
      .width('100%')  
    }  
    .height('100%')  
  }  
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.
  • 165.
  • 166.
  • 167.
分享
微博
QQ
微信
回复
2024-10-08 15:39:30
相关问题
HarmonyOS 文字实现
633浏览 • 1回复 待解决
Text匹配文字高亮显示
1418浏览 • 1回复 待解决
HarmonyOS 有没有UIScrollView组件
682浏览 • 1回复 待解决
HarmonyOS Text部分文字高亮设置
918浏览 • 1回复 待解决
有没有低代码相关文档可以阅读?
3613浏览 • 1回复 待解决
有没有NativeVSync相关使用文档
2699浏览 • 1回复 待解决
HarmonyOS 有没有类似clipChildren属性
780浏览 • 1回复 待解决
请问有没有支持.9图方案?
1109浏览 • 1回复 待解决
哪个属性可以改变Text组件字体粗细
2422浏览 • 1回复 待解决
HarmonyOS 有没有清除通知相关API
1019浏览 • 1回复 待解决
组件有没有支持事件穿透属性
2330浏览 • 1回复 待解决
HarmonyOS 中有没有类似tint属性
940浏览 • 1回复 待解决
鸿蒙有没有clipToPadding所对应属性
5593浏览 • 1回复 待解决
HarmonyOS ArkTS有没有相关资料?
1001浏览 • 1回复 待解决
请问鸿蒙中有没有@Keep注解
9389浏览 • 2回复 待解决
有没有maobox 鸿蒙版相关移植?
7369浏览 • 2回复 待解决
HarmonyOS有没有蓝牙相关操作demo
1147浏览 • 1回复 待解决