[go: up one dir, main page]

CN120407871A - A method for highlighting rich text in a table display under the Harmony Next App - Google Patents

A method for highlighting rich text in a table display under the Harmony Next App

Info

Publication number
CN120407871A
CN120407871A CN202510487915.8A CN202510487915A CN120407871A CN 120407871 A CN120407871 A CN 120407871A CN 202510487915 A CN202510487915 A CN 202510487915A CN 120407871 A CN120407871 A CN 120407871A
Authority
CN
China
Prior art keywords
rich text
style
highlighting
label
node data
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN202510487915.8A
Other languages
Chinese (zh)
Inventor
傅仰乙
商茎正
王廖逊
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Xiamen Zhuansu Digital Technology Co ltd
Original Assignee
Xiamen Zhuansu Digital Technology Co ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Xiamen Zhuansu Digital Technology Co ltd filed Critical Xiamen Zhuansu Digital Technology Co ltd
Priority to CN202510487915.8A priority Critical patent/CN120407871A/en
Publication of CN120407871A publication Critical patent/CN120407871A/en
Pending legal-status Critical Current

Links

Classifications

    • GPHYSICS
    • G06COMPUTING OR CALCULATING; COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F16/00Information retrieval; Database structures therefor; File system structures therefor
    • G06F16/90Details of database functions independent of the retrieved data types
    • G06F16/903Querying
    • G06F16/90335Query processing
    • G06F16/90344Query processing by using string matching techniques
    • GPHYSICS
    • G06COMPUTING OR CALCULATING; COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F40/00Handling natural language data
    • G06F40/10Text processing
    • G06F40/103Formatting, i.e. changing of presentation of documents
    • G06F40/109Font handling; Temporal or kinetic typography
    • GPHYSICS
    • G06COMPUTING OR CALCULATING; COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F40/00Handling natural language data
    • G06F40/10Text processing
    • G06F40/194Calculation of difference between files

Landscapes

  • Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • Computational Linguistics (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Audiology, Speech & Language Pathology (AREA)
  • General Health & Medical Sciences (AREA)
  • Health & Medical Sciences (AREA)
  • Artificial Intelligence (AREA)
  • Databases & Information Systems (AREA)
  • Data Mining & Analysis (AREA)
  • Document Processing Apparatus (AREA)

Abstract

本发明涉及富文本处理展示技术领域,尤其为一种Harmony Next App下列表展示处理富文本高亮的方法,首先获取包含HTML格式标签的富文本内容,通过正则表达式匹配等方式解析富文本,生成树形结构的节点数据,引入动态权重分配算法优化标签解析优先级,利用节点相似度合并算法减少冗余数据。基于节点数据,使用Harmony OS原生组件进行细粒度渲染,根据业务逻辑或用户输入关键词动态调整Span组件字体颜色属性,实现高亮展示,且样式与渲染逻辑解耦以灵活配置高亮策略。本发明采用预解析与缓存机制及轻量化渲染等策略优化性能,适用于富文本搜索关键词高亮、特定字段样式强调、多主题切换实时样式适配等多种场景。

The present invention relates to the field of rich text processing and display technology, and in particular to a method for processing rich text highlighting in a table display under the Harmony Next App. First, the rich text content containing HTML format tags is obtained, and the rich text is parsed by regular expression matching and other methods to generate node data in a tree structure. A dynamic weight allocation algorithm is introduced to optimize the tag parsing priority, and a node similarity merging algorithm is used to reduce redundant data. Based on the node data, fine-grained rendering is performed using Harmony OS native components, and the font color attributes of the Span component are dynamically adjusted according to the business logic or user input keywords to achieve highlighted display, and the style is decoupled from the rendering logic to flexibly configure the highlighting strategy. The present invention adopts pre-parsing and caching mechanisms and lightweight rendering strategies to optimize performance, and is suitable for a variety of scenarios such as rich text search keyword highlighting, specific field style emphasis, and multi-theme switching and real-time style adaptation.

Description

Method for processing highlighting rich text through Harmony Next App lower list display
Technical Field
The invention relates to the technical field of rich text processing and displaying, in particular to a method for processing rich text highlighting through Harmony Next App list displaying.
Background
The Harmony OS Next provides a Rich Text component in APP development for parsing and displaying HTML formatted Text (Rich Text) presentations, the Rich Text component being adapted to load and render a segment of HTML strings, supporting only limited generic properties and events. The custom HTML string customization is not supported by official recommendation Web components that modify background color, font size, dynamically change content by setting attributes and events.
In addition, the Rich Text component consumes more memory, and in some scenes where the Rich Text component is repeatedly executed, the phenomena of blocking, slow sliding response and the like can occur, and under the phenomena, the official recommendation of the Rich Editor component is used, so that the Rich Editor is more suitable for scenes needing editing.
In summary, in a scene where a highlight is searched in the list, when the highlight and the Rich Text are shown, none of the Web component, the Rich Text, and the Rich Editor are applicable to the above scene. For the above technical conditions. There are also drawbacks.
Therefore, a method for handling rich text highlighting by Harmony Next App under list display is proposed to address the above problems.
Disclosure of Invention
The invention aims to provide a method for processing highlighting of rich text by Harmony Next App under list display, so as to solve the problems in the background technology.
In order to achieve the above purpose, the present invention provides the following technical solutions:
a method for handling rich text highlighting by a Harmony Next App following list presentation, comprising the steps of:
s1, acquiring rich text content containing HTML format tags;
s2, analyzing the rich text content containing the HTML format tag, and generating node data of a tree structure, wherein the node data comprises the text content and corresponding style attributes;
And S3, carrying out fine-grained rendering by using the Harmony OS native component based on the node data, and realizing highlight display by setting the general attribute of the Harmony OS native component.
As a preferred embodiment, the parsing of the rich text content including the HTML format tag includes:
tags in rich text containing HTML format tags are matched with text content by specific rules as follows:
Matching starts with a left bracket, followed by a tag name consisting of non-right bracket characters, and then starts with a right bracket ending;
after matching the start tag, text content consisting of non-left bracket characters, the text content being selectable;
Matching a label name consisting of a left angle bracket and a diagonal stroke, followed by a label name consisting of non-right angle bracket characters, and ending a label ending with a right angle bracket;
Extracting custom style attributes in the labels, and storing the custom style attributes and text content in node data of a tree structure in an associated manner;
introducing a dynamic weight distribution algorithm to optimize the tag analysis priority, wherein the dynamic weight distribution algorithm calculates the tag analysis weight through the following logic:
the analysis weight of the label is obtained by weighting and summing a complexity factor and a nesting depth, wherein the complexity factor is a logarithmic value which is based on 2 and added with 1 in the number of nesting layers of the label, and the nesting depth is the number of nesting layers of the label;
The sum of the complexity factor and the weight coefficient of the nesting depth is 1, and the default values are 0.7 and 0.3 respectively.
As a preferred scheme, a node similarity merging algorithm is introduced in step S2, for optimizing the label resolution priority, and the logic is as follows:
Calculating the style similarity of two node data, specifically, the ratio of the number of common style attributes of the two node data to the number of all style attributes of the two node data;
When the ratio is greater than or equal to 0.8, the two node data are combined to reduce redundant data.
As a preferred approach, the Harmony OS native components include a Text component and a Span component, wherein:
The Text component is used as a parent container and is used for bearing a plurality of Span sub-components;
the Span component dynamically sets at least one of font color, font size or background color according to style attributes in the node data of the tree structure to achieve highlighting.
As a preferred embodiment, the present invention further includes:
In the list rendering process, the parsed node data is directly embedded into a list item template, so that repeated parsing of the same rich text content is avoided;
The real-time rendering calculation overhead is reduced through a pre-analysis and caching mechanism, and the memory occupation during list sliding is reduced through a lightweight native component.
As a preferred solution, the implementation of highlighting includes:
Dynamically adjusting font color attributes of Span components according to business logic or keywords input by a user;
Flexible configuration of highlighting policies is supported by decoupling of style attributes from rendering logic.
As a preferred embodiment, the present invention further includes:
introducing a state machine analyzer to process a nested label structure and a multi-level style inheritance relationship;
The global default font size, color and background parameters are defined, and the local style is overlaid by the global configuration to reduce the repetition code.
As can be seen from the technical scheme provided by the invention, the method for processing the highlighting of the rich text by the Harmony Next App list display has the following beneficial effects:
From the viewpoint of rich text analysis, distinguishing text nodes wrapped by plain text nodes and labels through regular expression matching, extracting custom style attributes and storing text content in an associated mode, and accurately analyzing rich text; introducing a dynamic weight distribution algorithm to optimize the label analysis priority, calculating weight according to the label complexity factor and the nesting depth, and preferentially analyzing the complex label, thereby improving the analysis efficiency; the node similarity merging algorithm reduces redundant data, merges nodes when the node style similarity reaches a threshold value, and optimizes a node data structure;
At the rendering level, carrying out fine-grained rendering by using a Harmony OS native component, and realizing highlight display by setting attributes of Text and Span components; the method comprises the steps of dynamically adjusting the font color of Span components based on business logic or key words input by users, decoupling style attributes from rendering logic, supporting flexible configuration of highlight strategies, meeting diversified highlight requirements under different scenes, directly embedding analyzed node data into a list item template in list rendering, avoiding repeated analysis, and improving rendering efficiency;
For complex HTML labels, a state machine parser is introduced to process nested label structures and multi-level style inheritance relations, so that the parsing accuracy and the style inheritance accuracy are ensured;
In the aspect of performance optimization, a pre-analysis and caching mechanism of node data is adopted, the real-time rendering calculation cost is reduced, the rendering delay is reduced, in the process of sliding a list, memory occupation is reduced through strategies such as light-weight rendering of a native component, component multiplexing, on-demand loading and the like, smooth sliding of the list is ensured, and user experience is improved.
Drawings
FIG. 1 is a flowchart showing the steps of a method for handling rich text highlighting in Harmony Next App according to the present invention.
Detailed Description
The present invention will be described in further detail with reference to the drawings and examples, in order to make the objects, technical solutions and advantages of the present invention more apparent. It should be understood that the specific embodiments described herein are for purposes of illustration only and are not intended to limit the scope of the invention.
In order to better understand the above technical solutions, the following detailed description will be given with reference to the accompanying drawings and the specific embodiments.
As shown in fig. 1, an embodiment of the present invention provides a method for processing highlighting of rich text by Harmony Next App following list display, which includes the following steps:
s1, acquiring rich text content containing HTML format tags;
s2, analyzing the rich text content containing the HTML format tag, and generating node data of a tree structure, wherein the node data comprises the text content and corresponding style attributes;
the step of parsing the rich text content containing the HTML format tags includes:
tags in rich text containing HTML format tags are matched with text content by specific rules as follows:
Matching starts with a left bracket, followed by a tag name consisting of non-right bracket characters, and then starts with a right bracket ending;
after matching the start tag, text content consisting of non-left bracket characters, the text content being selectable;
Matching a label name consisting of a left angle bracket and a diagonal stroke, followed by a label name consisting of non-right angle bracket characters, and ending a label ending with a right angle bracket;
Extracting custom style attributes in the labels, and storing the custom style attributes and text content in node data of a tree structure in an associated manner;
introducing a dynamic weight distribution algorithm to optimize the tag analysis priority, wherein the dynamic weight distribution algorithm calculates the tag analysis weight through the following logic:
the analysis weight of the label is obtained by weighting and summing a complexity factor and a nesting depth, wherein the complexity factor is a logarithmic value which is based on 2 and added with 1 in the number of nesting layers of the label, and the nesting depth is the number of nesting layers of the label;
The sum of the complexity factor and the weight coefficient of the nesting depth is 1, and the default values are 0.7 and 0.3 respectively;
introducing a node similarity merging algorithm for optimizing the label analysis priority, wherein the logic is as follows:
Calculating the style similarity of two node data, specifically, the ratio of the number of common style attributes of the two node data to the number of all style attributes of the two node data;
When the ratio is greater than or equal to 0.8, combining the two node data to reduce redundant data;
And S3, carrying out fine-grained rendering by using the Harmony OS native component based on the node data, and realizing highlight display by setting the general attribute of the Harmony OS native component.
In this embodiment, the step S1 is used to obtain the rich text content including the HTML format tag, and provides an original data base for subsequent parsing and related processing of the rich text, and all subsequent operations depend on the accurately obtained rich text content, which includes the following detailed steps:
step S1-1, determining a rich text source:
Defining a rich text content acquisition channel, which may include user input, such as direct input of rich text in a text input box of an application program, or reading of rich text content from an external file, such as from a locally stored HTML file, or from a web request, acquiring response data containing rich text from a server, determining the purpose of a source in order to acquire rich text in a correct manner, and having different acquisition methods and interface calls from different sources;
step S1-2, establishing connection (if needed):
When the rich text is obtained from the server, the connection with the server is required to be established by using a network communication protocol (such as HTTP, HTTPS and the like), necessary parameters such as a request head, a request method and the like are set, so that the request can be sent to the server smoothly and the response can be received;
step S1-3, obtaining rich text:
if the user input is the user input, the rich text content input by the user is directly extracted from the input control of the application program, if the user input is the user input, the server response is waited after the request is transmitted, and the rich text part containing the HTML format label is extracted from the response data;
step S1-4, data verification:
the method comprises the steps of obtaining rich text content, carrying out preliminary check on the obtained rich text content, checking whether the obtained rich text content meets the basic requirement of HTML format specification, carrying out preliminary check on whether obvious format errors exist, such as whether labels appear in pairs or not through simple regular expressions, if the format errors are found, recording error information, and deciding whether to prompt a user to re-input (if the format errors are sources of user input) or try to obtain the rich text from other sources or carrying out a certain degree of format repair attempt (if the format errors are feasible) according to the design of an application program so as to ensure that the rich text content which is processed later has basic usability in format.
In this embodiment, the function of step S2 is to parse the rich text content and generate the node data of the tree structure, where the node data includes the text content and the style attribute corresponding to the text content, and provide basic data for the subsequent fine-grained rendering using the native OS component, so that the rendering can achieve effects such as highlighting according to the style attribute, and the following steps are detailed:
step S2-1, data preprocessing:
preprocessing the rich text content containing the HTML format tag obtained in the step S1 to remove possible redundant blank spaces, line feed symbols, notes and other interference information, which is helpful for simplifying the subsequent analysis process and improving the analysis efficiency;
step S2-2, matching regular expressions:
The method comprises the steps of matching tag pairs in rich texts and text contents wrapped by the tag pairs through a regular expression < [ >]+>[<]*</[> ] + >, wherein the matching rule of the regular expression is that < is a start symbol of an HTML tag, [ > ] + is used for matching any character except for the tag name and tag attribute and at least one time, is that, [ < ] + is used for matching any character except for the tag name and can be matched zero times or multiple times and is used for matching the text contents wrapped by the tag, </is a start symbol of the HTML closed tag, [ > ] + is used for matching any character except for the tag name and at least one time, and the last > is an end symbol of the HTML closed tag;
Step S2-3, extracting custom style attributes:
Extracting the attribute related to the style from the attribute part of the label by using a character string segmentation and searching method, such as information of font color, font size, background color and the like in the style attribute;
Step S2-4, introducing a dynamic weight distribution algorithm:
In the process of analyzing the label, a dynamic weight distribution algorithm is introduced to optimize the label analysis priority, the formula is W (t i)=α·C(ti)+β·D(ti), wherein W (t i) represents the analysis weight of the label t i, C (t i) represents the complexity factor of the label t i, the complexity factor is calculated by the number of label nesting layers and the number of attributes, and the calculation formula is that Wherein, N attr(ti) represents the attribute number of the label t i, N max is a preset maximum attribute number threshold value, L nest(ti) represents the nesting layer number of the label t i, D (t i) represents the nesting depth of the label t i, alpha and beta are adjusting coefficients, alpha+beta=1 is satisfied, the default value is alpha=0.7, beta=0.3, and according to the calculated analysis weight, the label with higher analysis weight is preferentially analyzed, so that the analysis efficiency is improved;
step S2-5, generating node data:
Generating node data according to the matched labels, text contents and associated style attributes and a tree structure, wherein each node comprises node types (such as plain text nodes and label nodes), text contents and style attributes, constructing corresponding parent-child node relations for nested labels to form the tree structure, for example, for nested labels such as < div > < p > Hello </div >, div nodes are parent nodes, and p nodes are child nodes;
Step S2-6, introducing a node similarity merging algorithm:
in the process of generating node data, a node similarity merging algorithm is introduced to reduce redundant data, and the formula is Wherein S (n i,nj) represents the style similarity between the node n i and the node n j, A (n i) represents the style attribute set of the node n i, A (n j) represents the style attribute set of the node n j, and when S (n i,nj) is more than or equal to theta (the threshold value theta is set to 0.8), the nodes n i and n j are combined, and the text content and the style attribute of the nodes are integrated to form a new node, so that the node data structure is optimized;
step S2-7, outputting node arrays:
The generated and optimized node data are arranged into a node array to be output, the array contains information of all nodes, including node types, text contents and style attributes, the hierarchical relationship of a tree structure is maintained, and the node array is used as basic data for carrying out fine-grained rendering by using Harmony OS native components.
In this embodiment, the native Harmony OS component includes a Text component and a Span component, where:
The Text component is used as a parent container and is used for bearing a plurality of Span sub-components;
the Span component dynamically sets at least one of font color, font size or background color according to style attributes in the node data of the tree structure to achieve highlighting.
In this embodiment, the function of step S3 is to perform fine-grained rendering by using the native component of the Harmony OS based on the node data generated in step S2, and implement highlighting by setting the general attribute of the native component, so as to present rich text content with highlighting effect in the list of the Harmony Next App, which includes the following detailed steps:
step S3-1, initializing a rendering environment:
Before starting rendering, initializing the rendering environment of the Harmony OS, which comprises creating necessary rendering context, loading relevant rendering libraries and resources of the Harmony OS, ensuring that the rendering process can be normally performed;
Step S3-2, constructing a component hierarchy structure:
Constructing a corresponding Harmony OS native component hierarchical structure according to the node data of the tree structure generated in the step S2, and for each node, creating a corresponding native component according to the node type, wherein the method comprises the following steps:
When encountering a parent node containing a plurality of child nodes, creating a Text component, and taking the Text component as a parent container;
The Span component is used for creating a Span component for each text node according to the style attribute in the node data, and dynamically setting at least one of font color, font size or background color according to the style attribute so as to realize highlight display;
step S3-3, setting a component style attribute:
For Span components, according to the style information stored in the node data, setting the attributes of font color, font size, background color and the like of the Span components, for example, if the font color of a Text section specified in the node data is red, setting the font color attribute of the Span components to red when the corresponding Span components are created;
Step S3-4, realizing highlighting logic:
According to business logic or keywords input by a user, the font color attribute of the Span component is dynamically adjusted to realize highlight display, and the specific steps are as follows:
Determining a highlight keyword, namely acquiring the keyword to be highlighted from service logic or user input;
searching a matching text, namely searching texts containing highlight keywords in all Span components;
Adjusting style attributes, namely adjusting font color attributes of the matched Span components to highlight colors such as yellow or red;
Step S3-5 Pattern and logic decoupling configuration
By decoupling the style attribute and the rendering logic, flexible configuration of the highlighting strategy is supported, and the specific method comprises the following steps:
defining pattern configuration files, namely defining different highlight patterns (such as different font colors, background colors and the like) in one configuration file, so that unified management and modification are convenient;
dynamically loading patterns, namely dynamically loading different highlight patterns according to service requirements in the rendering process without modifying rendering logic codes;
Step S3-6 List item template embedding
In the list rendering process, the parsed node data is directly embedded into a list item template, so that repeated parsing of the same rich text content can be avoided, the rendering efficiency is improved, and the method comprises the following specific operations:
Creating a list item template, namely designing a template structure of the list item, wherein the template structure comprises a layout and a style;
Embedding node data, namely embedding the node data generated in the step S2 into a list item template to ensure that each list item can correctly display rich text content;
step S3-7, rendering the components to a screen:
After the creation, style setting and highlighting logic processing of the component are completed, the constructed component hierarchical structure is rendered on a screen, a rendering engine of the Harmony OS draws the component to a designated rendering area according to the attribute and layout information of the component, and finally, rich text content with a highlighting effect is displayed in a list of the Harmony Next App;
step S3-8, performance optimization and resource management:
In the rendering process, adopting some performance optimization strategies to ensure the fluency of the rendering process and the effective utilization of resources, wherein the specific measures comprise:
Caching the analyzed node data, so as to avoid repeated analysis;
And in the list sliding process, the memory occupation is reduced through the light-weight rendering of the native component, and the sliding smoothness is improved.
In this embodiment, the present invention further includes:
In the list rendering process, the parsed node data is directly embedded into a list item template, so that repeated parsing of the same rich text content is avoided;
Real-time rendering calculation overhead is reduced through a pre-analysis and caching mechanism, and memory occupation during list sliding is reduced through a lightweight native component;
The specific operation steps are as follows:
1. directly embedding the parsed node data into the list item template to avoid repeated parsing:
In the list rendering process, if the list items are rendered each time to analyze the rich text content, a large amount of repeated work is generated to reduce the performance, and the analyzed node data is directly embedded into the list item template, so that the problem can be effectively solved, and the following detailed steps are carried out:
1. data preparation:
After the analysis of the rich text content is completed in the step S2, generating node data of a tree structure, wherein the data comprises the text content and corresponding style attributes thereof;
2. and (3) template design:
the list item template is designed, the template prescribes the layout and structure of the list items, some positions are reserved in the template for inserting node data, for example, placeholders or specific marks can be used for representing the positions;
3. data embedding:
When rendering list items, traversing each data item in the list, and embedding the corresponding parsed node data into a list item template, wherein the specific method is to apply Text content and style attributes in the node data to corresponding components according to reserved positions in the template;
4. avoiding repeated resolution:
Because the node data is already analyzed in the previous step, the embedding process only simply applies the data to the template, and does not need to analyze the rich text content again;
2. Real-time rendering computation overhead is reduced through a preresolved and caching mechanism:
The preresolved and buffer mechanism is an effective performance optimization strategy, can carry out calculation work in advance and store results, avoids repeated calculation during real-time rendering, and comprises the following specific implementation steps:
1. Pre-analyzing:
The pre-parsing has the advantages of dispersing calculation work to a non-critical period, avoiding a large amount of calculation during real-time rendering, and reducing rendering delay;
2. Caching mechanism:
The node data obtained by pre-analysis is stored in a cache, the cache can use a memory cache or a disk cache, and the specific selection depends on the size and the use frequency of the data;
3. cache hit processing:
If the cache hits, namely node data of the rich text content exists in the cache, directly acquiring the node data from the cache and embedding the node data into a list item template for rendering;
4. cache update and elimination:
The method comprises the steps of storing the data in a cache, updating node data in the cache when the rich text content changes, analyzing the data in the cache again by monitoring a data change event, and storing the new node data in the cache;
3. Memory occupation during list sliding is reduced through the lightweight native component:
In the list sliding process, a large amount of components are rendered to occupy a large amount of memory, so that sliding is not smooth, the memory occupation can be effectively reduced by using the lightweight native components, the sliding performance is improved, and the following specific methods are adopted:
1. selecting an appropriate component:
For example, for Text display, text components and Span components are preferably used, and the components are optimized in Harmon yOS, so that less memory is occupied;
2. Multiplexing components:
In the process of list sliding, when a list item slides out of a screen, components are not destroyed immediately and are reused, a component pool can be used for managing the reusable components, when a new list item needs to be rendered, available components are obtained from the component pool, and data and patterns of the available components are updated;
3. Loading as required:
For some complex components or resources, a strategy of loading according to the need is adopted, for example, for rich text content containing pictures, the pictures are loaded when a list item slides into a screen, but not all the pictures are loaded when the list is initialized;
4. Memory monitoring and optimizing:
In the running process of the application, the use condition of the memory is monitored in real time, when the memory occupation is too high, corresponding optimization measures such as cleaning the cache, releasing the unused resources and the like are adopted, and the memory monitoring tool provided by Harmon yOS can be used for assisting in memory management so as to ensure that the application keeps stable performance in the list sliding process.
In this embodiment, the implementation of highlighting includes:
Dynamically adjusting font color attributes of Span components according to business logic or keywords input by a user;
through decoupling of style attributes and rendering logic, flexible configuration of a highlight strategy is supported;
Specific:
1. dynamically adjusting font color attributes of Span components according to business logic or keywords input by a user:
keyword acquisition and analysis:
Business logic driving, wherein in an application program, the business logic can preset certain text modes or content categories needing to be highlighted, for example, in news information applications, the business logic can prescribe all keywords related to breaking news, such as breaking, urgent and the like, and the keywords need to be highlighted in news headline rich texts displayed in a list;
When the user submits the input, the system analyzes the input content, removes unnecessary space, special character and other interference information, and ensures that accurate keywords are acquired;
span component traversal and matching:
Based on the node data generated in the step S2, a structure comprising a plurality of Span components is constructed in the rendering process;
The text content of each Span component is subjected to keyword matching operation, and the obtained keywords are compared with the text of the Span component one by adopting a character string matching algorithm, such as a naive character string matching algorithm or a more efficient KMP (Knuth-Morris-Pratt) algorithm, for example, if the keywords are "apples" and the text of a certain Span component is "I like eating apples and bananas", the Span component is successfully matched;
Font color attribute dynamic adjustment:
In a native component system of the Harmony OS, the Span component provides an API for setting font color, for example, the font color is adjusted to a preset highlight color by calling a method such as Span. SetTextcolor (color. YELLOW) to realize the highlight display of the text where the keyword is located (assuming that the Span is a matched Span component example and the color. YELLOW is the preset highlight color);
2. flexible configuration of highlighting policies is supported through decoupling of style attributes from rendering logic:
style attribute independent storage and management:
creating a special style configuration file which can adopt JSON, XML and other formats, and independently storing various highlight style attributes in the file, for example, defining a plurality of color schemes for different service scenes or highlight requirements preferred by users;
When an application program is started, loading the style configuration file and storing the style configuration file in a data structure in a memory, such as a dictionary, so that the subsequent quick reading and use are convenient;
Rendering logic is separate from style calls:
in the code that previously adjusted Span component font color according to keyword matching, for example, color values are not directly specified, but colors in the style configuration are referenced through one intermediate variable;
when the highlighting strategy needs to be changed, for example, the default red highlighting is changed into yellow highlighting, the corresponding color value is only required to be modified in the style configuration file, and rendering logic codes are not required to be modified;
flexible configuration implementation:
The method comprises the steps of switching business scenes, namely dynamically loading different style configurations according to actual demands by an application program under different business scenes, for example, using a relatively soft highlight color under a daytime mode and switching to a highlight color with higher contrast under a night mode to improve readability;
The method comprises the steps of customizing a user, providing a user with a function of customizing a highlight style, modifying related attribute values in a style configuration file by the user through controls such as a color selector, font size adjustment and the like in a setting interface of an application program, and displaying the highlight effect by the application program according to the style configuration customized by the user in a subsequent rendering process, so that the flexibility of a highlight strategy is greatly improved.
In this embodiment, the method further includes:
introducing a state machine analyzer to process a nested label structure and a multi-level style inheritance relationship;
Defining global default font size, color and background parameters, and overlaying local styles through global configuration to reduce repeated codes;
Specific:
1. A state machine parser is introduced to process nested label structures and multi-level style inheritance relationships:
1. basic principle of state machine parser:
the state machine parser is an algorithm based on state transition, is used for processing complex text structures, is very effective in parsing nested label structures in rich texts, gradually parses texts by defining different states and transition rules among the states, and recognizes the start and end of labels and the nesting relation among the labels;
2. State definition:
Initial state, namely, the state when the rich text starts to be analyzed, waiting for encountering a first label;
A label starting state, which is to enter the state when the < symbol is analyzed, and start to identify the label name and the attribute;
A tag attribute state, which is to enter the state after the tag name is identified, and is used for analyzing the attribute of the tag, such as style information in the style attribute;
A tag content state, which is to enter the state when encountering the > symbol, and start to process the text content of the tag package;
A label ending state, which is to enter the state when the symbol is analyzed, and is used for matching the corresponding starting label and processing label ending logic;
3. state transition rules:
Encountering a < symbol from an initial state, transitioning to a tag start state;
When the label starts to be in a state, after the label signature is identified, the label encounters an attribute and is transferred to a label attribute state;
in the tag attribute state, after all attributes are processed, encountering a > symbol, and transferring to the tag content state;
in the tag content state, encountering a < symbol, entering a new tag start state (nested tag case) if the tag is a start tag, and entering a tag end state if the tag is an end tag;
after the label end state is matched with the corresponding starting label, returning to the previous state according to the nesting level;
4. Processing nested tag structures:
For example, for < div > < p > Hello </div >, resolving to < div > enter the label starting state, enter the label content state after finishing processing the attribute of div label, then enter the new label starting state after encountering < p >, process p label and its content, finally encounter </p > return to the content state of div label, then encounter </div > finish the resolution of the whole label structure;
in the processing process, a stack is used for recording the current label level, the current label level is pushed into the stack every time when a start label is encountered, and the corresponding start label is popped out of the stack when an end label is encountered, so that the correct nesting relationship of the labels is ensured;
5. Processing a multi-level style inheritance relationship:
Extracting style information when analyzing the tag attribute, and inheriting partial styles of a parent tag by child tags when encountering a nested tag, for example, if the parent tag div has style= "color: red; font-size:16px", the child tag p does not define color attribute, then text in the p tag inherits the color style of the div tag by default;
When each label is processed, the state machine analyzer maintains a pattern stack, merges the pattern of the label with the pattern of the stack top (if inheritance relation exists) every time a new label is entered, removes the pattern of the label from the stack when the label is encountered, and ensures that the inheritance and coverage relation of the pattern are correct;
2. Defining global default font size, color and background parameters, and overlaying the local style by global configuration to reduce duplicate codes:
1. Creation of global style configuration module:
creating a global style profile, such as globalstylecon fig. json, for storing default font size, color and background parameters;
when the application is started, the configuration file is loaded and stored in a global variable in the memory, so that the subsequent access is facilitated;
2. Combination of local and global styles:
In the process of rendering, for each node, firstly, applying a local style of the node;
Then, the global style configuration is checked, and if some style attributes (such as font size, color or background color) are not defined in the local style, default values in the global style are used, for example, if font size is not defined in the local style of a node, text of the node will be used defaultFontSize in the global configuration;
3. The global configuration overlays the local style:
Providing a switching mechanism or priority setting for realizing the coverage of the global configuration to the local patterns;
When overrideLocalStyle is true, the global style will cover the same attributes in the local style, e.g., if the font color in the local style is red, and the font color in the global configuration is black, and overrideLocalStyle is true, the final rendered font color is black;
4. Reducing repetition codes:
The method has the advantages that the repeated definition of the same style attribute in the local style of each node is avoided through the global style configuration, for example, if a plurality of nodes are required to use the same font size and color and are only required to be defined once in the global style, the nodes can be automatically inherited or covered into the global style, thereby reducing the redundancy of codes and improving the maintainability of the codes, and when the global style is required to be modified, all affected node styles can be automatically updated only by modifying corresponding parameters in the global configuration file.
Although embodiments of the present invention have been shown and described, it will be understood by those skilled in the art that various changes, modifications, substitutions and alterations can be made therein without departing from the principles and spirit of the invention, the scope of which is defined in the appended claims and their equivalents.

Claims (7)

1. The method for processing rich text highlighting by Harmony Next App list display is characterized by comprising the following steps:
s1, acquiring rich text content containing HTML format tags;
s2, analyzing the rich text content containing the HTML format tag, and generating node data of a tree structure, wherein the node data comprises the text content and corresponding style attributes;
And S3, carrying out fine-grained rendering by using the Harmony OS native component based on the node data, and realizing highlight display by setting the general attribute of the Harmony OS native component.
2. The method for handling rich text highlighting as recited in claim 1, wherein parsing rich text content comprising HTML format tags comprises:
tags in rich text containing HTML format tags are matched with text content by specific rules as follows:
Matching starts with a left bracket, followed by a tag name consisting of non-right bracket characters, and then starts with a right bracket ending;
after matching the start tag, text content consisting of non-left bracket characters, the text content being selectable;
Matching a label name consisting of a left angle bracket and a diagonal stroke, followed by a label name consisting of non-right angle bracket characters, and ending a label ending with a right angle bracket;
Extracting custom style attributes in the labels, and storing the custom style attributes and text content in node data of a tree structure in an associated manner;
introducing a dynamic weight distribution algorithm to optimize the tag analysis priority, wherein the dynamic weight distribution algorithm calculates the tag analysis weight through the following logic:
the analysis weight of the label is obtained by weighting and summing a complexity factor and a nesting depth, wherein the complexity factor is a logarithmic value which is based on 2 and added with 1 in the number of nesting layers of the label, and the nesting depth is the number of nesting layers of the label;
The sum of the complexity factor and the weight coefficient of the nesting depth is 1, and the default values are 0.7 and 0.3 respectively.
3. The method for processing highlighting rich text according to claim 2, wherein a node similarity merging algorithm is introduced in step S2, for optimizing the label resolution priority, and the logic is as follows:
Calculating the style similarity of two node data, specifically, the ratio of the number of common style attributes of the two node data to the number of all style attributes of the two node data;
When the ratio is greater than or equal to 0.8, the two node data are combined to reduce redundant data.
4. The method for handling rich Text highlighting by Harmony Next App following list presentation of claim 1, wherein the Harmony OS native components comprise a Text component and a Span component, wherein:
the Text component is used as a parent container and is used for bearing a plurality of Span sub-components;
and the Span component dynamically sets at least one of font color, font size or background color according to the style attribute in the node data of the tree structure so as to realize highlight display.
5. The method for handling rich text highlighting in accordance with claim 1, further comprising:
In the list rendering process, the parsed node data is directly embedded into a list item template, so that repeated parsing of the same rich text content is avoided;
The real-time rendering calculation overhead is reduced through a pre-analysis and caching mechanism, and the memory occupation during list sliding is reduced through a lightweight native component.
6. The method for handling rich text highlighting in Harmony Next App under list presentation of claim 1, wherein the highlighting is implemented by:
Dynamically adjusting font color attributes of Span components according to business logic or keywords input by a user;
Flexible configuration of highlighting policies is supported by decoupling of style attributes from rendering logic.
7. The method for handling rich text highlighting in accordance with claim 1, further comprising:
introducing a state machine analyzer to process a nested label structure and a multi-level style inheritance relationship;
The global default font size, color and background parameters are defined, and the local style is overlaid by the global configuration to reduce the repetition code.
CN202510487915.8A 2025-04-18 2025-04-18 A method for highlighting rich text in a table display under the Harmony Next App Pending CN120407871A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202510487915.8A CN120407871A (en) 2025-04-18 2025-04-18 A method for highlighting rich text in a table display under the Harmony Next App

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202510487915.8A CN120407871A (en) 2025-04-18 2025-04-18 A method for highlighting rich text in a table display under the Harmony Next App

Publications (1)

Publication Number Publication Date
CN120407871A true CN120407871A (en) 2025-08-01

Family

ID=96503807

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202510487915.8A Pending CN120407871A (en) 2025-04-18 2025-04-18 A method for highlighting rich text in a table display under the Harmony Next App

Country Status (1)

Country Link
CN (1) CN120407871A (en)

Similar Documents

Publication Publication Date Title
US6941521B2 (en) Method for dynamically generating a user interface from XML-based documents
US7117436B1 (en) Generating a Web page by replacing identifiers in a preconstructed Web page
US7076728B2 (en) Method and apparatus for end-to-end content publishing system using XML with an object dependency graph
US7197510B2 (en) Method, system and program for generating structure pattern candidates
US8166053B2 (en) Method and apparatus for schema-driven XML parsing optimization
US7085999B2 (en) Information processing system, proxy server, web page display method, storage medium, and program transmission apparatus
CN112001153B (en) Text processing method, device, computer equipment and storage medium
US20120110437A1 (en) Style and layout caching of web content
US8959428B2 (en) Method and apparatus for generating an integrated view of multiple databases
US20060294459A1 (en) Method and apparatus for end-to-end content publishing system using xml with an object dependency graph
CN111913739B (en) Service interface primitive defining method and system
US20080301545A1 (en) Method and system for the intelligent adaption of web content for mobile and handheld access
US20100199167A1 (en) Document processing apparatus
JP2005521159A (en) Dynamic generation of schema information for data description languages
US20100036860A1 (en) Data managment apparatus, data editing apparatus, and data reading apparatus and respective methods thereof
US20090019064A1 (en) Document processing device and document processing method
US9229920B1 (en) Compound undo/redo manager for mixed model edits
US20100115394A1 (en) Document processing device and document processing method
US20240296350A1 (en) Computed values for knowledge graph
US20100169333A1 (en) Document processor
US20080209572A1 (en) Data Processing System, Data Processing Method, and Management Server
US20090083300A1 (en) Document processing device and document processing method
CN120407871A (en) A method for highlighting rich text in a table display under the Harmony Next App
US7827195B2 (en) Document management device and document management method
CN118194827A (en) Method, device and medium for dynamically generating report based on Word file

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination