ts-basic-components-richtext.md 4.1 KB
Newer Older
E
add doc  
esterzhou 已提交
1 2 3 4
# RichText

The **\<RichText>** component parses and displays HTML text.

E
ester.zhou 已提交
5 6 7 8
>  **NOTE**
>
>  This component is supported since API version 8. Updates will be marked with a superscript to indicate their earliest API version.

E
add doc  
esterzhou 已提交
9 10 11 12 13 14 15 16 17 18
## Required Permissions

None

## Child Components

None

## APIs

E
ester.zhou 已提交
19
RichText(content:string)
E
add doc  
esterzhou 已提交
20

E
ester.zhou 已提交
21
**Parameters**
E
add doc  
esterzhou 已提交
22

E
ester.zhou 已提交
23 24 25
| Name| Type| Mandatory| Default Value| Description|
| -------- | -------- | -------- | -------- | -------- |
| content | string | Yes| - | String in HTML format.|
E
ester.zhou 已提交
26

E
add doc  
esterzhou 已提交
27 28 29 30

## Events


E
ester.zhou 已提交
31
| Name| Description|
E
add doc  
esterzhou 已提交
32
| -------- | -------- |
E
ester.zhou 已提交
33 34
| onStart(callback: () => void)    | Triggered when web page loading starts.  |
| onComplete(callback: () => void) | Triggered when web page loading is completed.|
E
add doc  
esterzhou 已提交
35 36 37

## Supported Tags

E
ester.zhou 已提交
38
| Name| Description| Example|
E
add doc  
esterzhou 已提交
39
| -------- | -------- | -------- |
E
ester.zhou 已提交
40 41 42 43 44 45 46 47 48 49
| \<h1>--\<h6> | Defines six levels of headings in the HTML document. \<h1> defines the most important heading, and \<h6> defines the least important heading.| \<h1>This is an H1 heading\</h1> \<h2>This is an H2 heading\</h2>|
| \<p>\</p> | Defines a paragraph.| \<p>This is a paragraph\</p>|
| \<br/> | Inserts a newline character.| \<p>This is a paragraph\<br/>This is a new paragraph\</p>|
| \<hr/> | Defines a thematic break (such as a shift of topic) on an HTML page and creates a horizontal line.| \<p>This is a paragraph\</p>\<hr/>\<p>This is a paragraph\</p> |
| \<div>\</div> | Defines a generic container that is generally used to group block-level elements. It allows you to apply CSS styles to multiple elements at the same time.| \<div style='color:#0000FF'>\<h3>This is the heading in a div element\</h3>\</div> |
| \<i>\</i> | Displays text in italic style.| \<i>This is in italic style\</i>|
| \<u>\</u> | Defines text that should be styled differently or have a non-textual annotation, such as misspelt words or a proper name in Chinese text. It is recommended that you avoid using the \<u> tag where it could be confused with a hyperlink.| \<p>\<u>This is an underlined paragraph\</u>\</p> |
| \<style>\</style> | Used to embed CSS within an HTML document.| \<style>h1{color:red;}p{color:blue;}\</style> |
| style | Defines the inline style of an element and is placed inside the tag. Use quotation marks (') to separate the styling text and use semicolons (;) to separate styles, for example, **style='width: 500px;height: 500px;border: 1px solid;margin: 0 auto;'**.| \<h1 style='color:blue;text-align:center'>This is a heading\</h1>\<p style='color:green'>This is a paragraph\</p> |
| \<script>\</script> | Used to embed or reference a client-side script, such as JavaScript.| \<script>document.write("Hello World!")\</script> |
E
add doc  
esterzhou 已提交
50 51

## Example
E
ester.zhou 已提交
52 53 54
You can preview how this component looks on a real device. The preview is not yet available in the DevEco Studio Previewer.
```ts
// xxx.ets
E
add doc  
esterzhou 已提交
55 56 57
@Entry
@Component
struct RichTextExample {
E
esterzhou 已提交
58 59 60 61 62 63 64 65 66 67
  @State data: string = "<h1 style='text-align: center;'>h1 heading</h1>" +
                        "<h1 style='text-align: center;'><i>h1 italic</i></h1>" +
                        "<h1 style='text-align: center;'><u>h1 underlined</u></h1>" +
                        "<h2 style='text-align: center;'>h2 heading</h2>" +
                        "<h3 style='text-align: center;'>h3 heading</h3>" +
                        "<p style='text-align: center;'>Regular paragraph</p><hr/>" +
                        "<div style='width: 500px;height: 500px;border: 1px solid;margin: 0auto;'>" +
                        "<p style='font-size: 35px;text-align: center;font-weight: bold; color: rgb(24,78,228)'>Font size: 35px; line height: 45px</p>" +
                        "<p style='background-color: #e5e5e5;line-height: 45px;font-size: 35px;text-indent: 2em;'>" +
                        "<p>This is a paragraph. This is a paragraph. This is a paragraph. This is a paragraph. This is a paragraph. This is a paragraph. This is a paragraph. This is a paragraph. This is a paragraph.</p>"
E
add doc  
esterzhou 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82

  build() {
    Flex({direction: FlexDirection.Column,alignItems: ItemAlign.Center,
    justifyContent: FlexAlign.Center }){
      RichText(this.data)
      .onStart(()=>{
        console.info("RichText onStart")
      })
      .onComplete(()=>{
        console.info("RichText onComplete")
      })
    }
  }
}
```