• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Rich Text (RichEditor)
2**RichEditor** is a component that supports interactive text editing and mixture of text and images. It is typically used in scenarios where mixed-content user input is expected, such as comment sections that accept both image and text submissions. For details, see [RichEditor](../reference/apis-arkui/arkui-ts/ts-basic-components-richeditor.md).
3
4## Creating a RichEditor Component
5You can create a **RichEditor** component with or without a styled string.
6
7### Creating a RichEditor Component Without a Styled String
8Use the **RichEditor(value: [RichEditorOptions](../reference/apis-arkui/arkui-ts/ts-basic-components-richeditor.md#richeditoroptions))** API to generate a **RichEditor** component from scratch. This approach is typically employed when you wish to present straightforward text and image content, for example, contact details. It can also serve in instances where a consistent content format is desired, such as in certain code editors.
9
10```ts
11controller: RichEditorController = new RichEditorController();
12options: RichEditorOptions = { controller: this.controller };
13
14RichEditor(this.options)
15  .onReady(() => {
16    this.controller.addTextSpan('Create a RichEditor component without using a styled string.', {
17      style: {
18        fontColor: Color.Black,
19        fontSize: 15
20      }
21    })
22  })
23```
24![alt text](figures/richeditor_image_options.gif)
25
26### Creating a RichEditor Component with a Styled String
27Alternatively, you can use the **RichEditor(options: [RichEditorStyledStringOptions](../reference/apis-arkui/arkui-ts/ts-basic-components-richeditor.md#richeditorstyledstringoptions12))** API to create a **RichEditor** component that is based on a styled string ([StyledString/MutableStyledString](arkts-styled-string.md)). This approach is particularly useful when you aim to enhance the GUI's aesthetics and emphasize the content,
28
29because it allows for a variety of text formatting options, including changing font size, adding font colors, making text interactive, and custom text rendering. In addition, this approach offers an array of style objects that cover a range of common text formatting styles, such as text with decorative lines, line height, and shadow effects.
30
31```ts
32fontStyle: TextStyle = new TextStyle({
33  fontColor: Color.Pink
34});
35// Define a text style object.
36
37mutableStyledString: MutableStyledString = new MutableStyledString("Create a RichEditor component using a styled string.",
38  [{
39    start: 0,
40    length: 5,
41    styledKey: StyledStringKey.FONT,
42    styledValue: this.fontStyle
43  }]);
44// Create a styled string.
45
46controller: RichEditorStyledStringController = new RichEditorStyledStringController();
47options: RichEditorStyledStringOptions = { controller: this.controller };
48
49RichEditor(this.options)
50  .onReady(() => {
51    this.controller.setStyledString(this.mutableStyledString);
52  })
53```
54![alt text](figures/richeditor_image_stylestringoptions.gif)
55
56## Setting Attributes
57
58### Setting the Custom Context Menu on Text Selection
59You can set a custom context menu on text selection using the [bindSelectionMenu](../reference/apis-arkui/arkui-ts/ts-basic-components-richeditor.md#bindselectionmenu) API.
60
61By default, the context menu on text selection includes the copy, cut, and select all options. You can offer additional options for enhanced interactions, for example, a translate option for multilingual support or a bold option for emphasizing selected text.
62
63If the custom menu is too long, consider embedding a **Scroll** component to prevent the keyboard from being blocked.
64
65```ts
66export interface SelectionMenuTheme {
67  imageSize: number;
68  buttonSize: number;
69  menuSpacing: number;
70  editorOptionMargin: number;
71  expandedOptionPadding: number;
72  defaultMenuWidth: number;
73  imageFillColor: Resource;
74  backGroundColor: Resource;
75  iconBorderRadius: Resource;
76  containerBorderRadius: Resource;
77  cutIcon: Resource;
78  copyIcon: Resource;
79  pasteIcon: Resource;
80  selectAllIcon: Resource;
81  shareIcon: Resource;
82  translateIcon: Resource;
83  searchIcon: Resource;
84  arrowDownIcon: Resource;
85  iconPanelShadowStyle: ShadowStyle;
86  iconFocusBorderColor: Resource;
87}
88// Custom SelectionMenuTheme API definition
89
90export const defaultTheme: SelectionMenuTheme = {
91  imageSize: 24,
92  buttonSize: 48,
93  menuSpacing: 8,
94  editorOptionMargin: 1,
95  expandedOptionPadding: 3,
96  defaultMenuWidth: 256,
97  imageFillColor: $r('sys.color.ohos_id_color_primary'),
98  backGroundColor: $r('sys.color.ohos_id_color_dialog_bg'),
99  iconBorderRadius: $r('sys.float.ohos_id_corner_radius_default_m'),
100  containerBorderRadius: $r('sys.float.ohos_id_corner_radius_card'),
101  cutIcon: $r("sys.media.ohos_ic_public_cut"),
102  copyIcon: $r("sys.media.ohos_ic_public_copy"),
103  pasteIcon: $r("sys.media.ohos_ic_public_paste"),
104  selectAllIcon: $r("sys.media.ohos_ic_public_select_all"),
105  shareIcon: $r("sys.media.ohos_ic_public_share"),
106  translateIcon: $r("sys.media.ohos_ic_public_translate_c2e"),
107  searchIcon: $r("sys.media.ohos_ic_public_search_filled"),
108  arrowDownIcon: $r("sys.media.ohos_ic_public_arrow_down"),
109  iconPanelShadowStyle: ShadowStyle.OUTER_DEFAULT_MD,
110  iconFocusBorderColor: $r('sys.color.ohos_id_color_focused_outline'),
111}
112// Define the defaultTheme variable.
113
114RichEditor(this.options)
115  .onReady(() => {
116    this.controller.addTextSpan('The component has a custom menu that can be triggered by a long press.', {
117      style: {
118        fontColor: Color.Black,
119        fontSize: 18
120      }
121    })
122  })
123  .bindSelectionMenu(RichEditorSpanType.TEXT, this.SystemMenu, ResponseType.LongPress, {
124    onDisappear: () => {
125      this.sliderShow = false
126    }
127  })
128// Bind a custom menu.
129  .width(300)
130  .height(300)
131
132@Builder
133SystemMenu() {
134  Column() {
135    Menu() {
136      if (this.controller) {
137        MenuItemGroup() {
138          MenuItem({
139            startIcon: this.theme.cutIcon,
140            content: "Cut",
141            labelInfo: "Ctrl+X",
142          })
143          MenuItem({
144            startIcon: this.theme.copyIcon,
145            content: "Copy",
146            labelInfo: "Ctrl+C"
147          })
148          MenuItem({
149            startIcon: this.theme.pasteIcon,
150            content: "Paste",
151            labelInfo: "Ctrl+V"
152          })
153        }
154      }
155    }
156    .radius(this.theme.containerBorderRadius)
157    .clip(true)
158    .backgroundColor(Color.White)
159    .width(this.theme.defaultMenuWidth)
160  }
161  .width(this.theme.defaultMenuWidth)
162}
163```
164
165![alt text](figures/richeditor_image_bindselectionmenu.gif)
166
167### Setting Caret and Selection Handle Colors in the Text Box
168You can set the caret and selection handle colors in the text box using the [caretColor](../reference/apis-arkui/arkui-ts/ts-basic-components-richeditor.md#caretcolor12) API.
169
170This feature allows for a more distinct visual representation of the caret and text selection, which can significantly aid users in navigating through complex UI that incorporate various input fields. It can also be particularly beneficial for text boxes that signify special features or states, such as a password text area.
171
172```ts
173RichEditor(this.options)
174  .onReady(() => {
175    this.controller.addTextSpan('The component has the color set for the caret and selection handle.', {
176      style: {
177        fontColor: Color.Black,
178        fontSize: 15
179      }
180    })
181  })
182  .caretColor(Color.Orange)
183  .width(300)
184  .height(300)
185```
186
187![alt text](figures/richeditor_image_caretcolor.gif)
188
189### Setting Placeholder Text
190You can set the placeholder text, which is displayed when there is no input, using the [placeholder](../reference/apis-arkui/arkui-ts/ts-basic-components-richeditor.md#placeholder12) API.
191
192Placeholder text serves as a useful cue, assisting users in navigating through your application's UI, particularly in scenarios where text areas require specific attention or instructions, such as a login screen, or a text editing box with a character limit.
193
194```ts
195RichEditor(this.options)
196  .placeholder("Enter your content here", {
197    fontColor: Color.Gray,
198    font: {
199      size: 15,
200      weight: FontWeight.Normal,
201      family: "HarmonyOS Sans",
202      style: FontStyle.Normal
203    }
204  })
205  .width(300)
206  .height(50)
207```
208
209![alt text](figures/richeditor_image_placeholder.gif)
210
211For details about all available attributes, see [RichEditor Attributes](../reference/apis-arkui/arkui-ts/ts-basic-components-richeditor.md#attributes).
212
213## Adding Events
214
215### Adding a Callback for Component Initialization
216Use the [onReady](../reference/apis-arkui/arkui-ts/ts-basic-components-richeditor.md#onready) API to add a callback that is invoked after the component has been initialized.
217
218This callback can be used to effectively display rich content including images, texts, and emoticons after the component is initialized. When the **RichEditor** component is used to display news, this callback can initiate the process of obtaining image and text data from the server. The obtained data is then populated into the component, ensuring that the complete news content is promptly displayed on the page after initialization.
219
220```ts
221RichEditor(this.options)
222  .onReady(() => {
223    this.controller.addTextSpan('The onReady callback content is preset text within the component.', {
224      style: {
225        fontColor: Color.Black,
226        fontSize: 15
227      }
228    })
229  })
230```
231
232![alt text](figures/richeditor_image_onReady.gif)
233
234### Adding a Callback for Content Selection
235Use the [onSelect](../reference/apis-arkui/arkui-ts/ts-basic-components-richeditor.md#onselect) API to add a callback that is invoked when content within the component is selected.
236
237This callback can be used to enhance the user experience following text selection For example, it can be used to trigger a context menu, allowing users to modify text styles, or to perform content analysis and processing on the selected text, providing input suggestions and thereby improving the efficiency and convenience of text editing.
238
239The callback can be triggered in two ways: by pressing and releasing the left mouse button to select content, or by selecting content with a finger touch and releasing the finger.
240
241```ts
242RichEditor(this.options)
243  .onReady(() => {
244    this.controller.addTextSpan('Select this text to invoke the onSelect callback.', {
245      style: {
246        fontColor: Color.Black,
247        fontSize: 15
248      }
249    })
250  })
251  .onSelect((value: RichEditorSelection) => {
252    this.controller1.addTextSpan(JSON.stringify(value), {
253      style: {
254        fontColor: Color.Gray,
255        fontSize: 10
256      }
257    })
258  })
259  .width(300)
260  .height(50)
261Text('View callback content:').fontSize(10).fontColor(Color.Gray).width(300)
262RichEditor(this.options1)
263  .width(300)
264  .height(70)
265```
266
267![alt text](figures/richeditor_image_onSelect.gif)
268
269### Adding Callbacks for Before and After Text and Image Changes
270Use the [onWillChange](../reference/apis-arkui/arkui-ts/ts-basic-components-richeditor.md#onwillchange12) API to add a callback invoked before text or image changes. This callback is applicable to real-time data verification and notification. For example, it can be used to enable features such as detecting sensitive words and displaying an alert dialog box immediately, as well as real-time character count statistics and limitation.
271
272Use the [onDidChange](../reference/apis-arkui/arkui-ts/ts-basic-components-richeditor.md#ondidchange12) API to add a callback invoked after text or image changes. This callback applies to content saving and synchronization. For example, it can be used to automatically save the latest content to the local host or synchronizing it to the server, and for updating content status and rendering.
273
274Note that the **RichEditor** component constructed with [RichEditorStyledStringOptions](../reference/apis-arkui/arkui-ts/ts-basic-components-richeditor.md#richeditorstyledstringoptions12) does not support these two types of callbacks.
275
276```ts
277RichEditor(this.options)
278  .onReady(() => {
279    this.controller.addTextSpan('The callback is invoked before the text or image change.\nThe callback is invoked after the text or image change.', {
280      style: {
281        fontColor: Color.Black,
282        fontSize: 15
283      }
284    })
285  })
286  .onWillChange((value: RichEditorChangeValue) => {
287    this.controller1.addTextSpan('The callback is invoked before the text or image change: \n' + JSON.stringify(value), {
288      style: {
289        fontColor: Color.Gray,
290        fontSize: 10
291      }
292    })
293    return true;
294  })
295  .onDidChange((rangeBefore: TextRange, rangeAfter: TextRange) => {
296    this.controller1.addTextSpan('\nThe callback is invoked after the text or image change: \nrangeBefore:' + JSON.stringify(rangeBefore) +
297      '\nrangeAfter: ' + JSON.stringify(rangeBefore), {
298      style: {
299        fontColor: Color.Gray,
300        fontSize: 10
301      }
302    })
303    return true;
304  })
305  .width(300)
306  .height(50)
307Text('View callback content:').fontSize(10).fontColor(Color.Gray).width(300)
308RichEditor(this.options1)
309  .width(300)
310  .height(70)
311```
312
313![alt text](figures/richeditor_image_ondid.gif)
314
315### Adding Callbacks for Before and After Content Input in the Input Method
316To facilitate intelligent input assistance, use [aboutToIMEInput](../reference/apis-arkui/arkui-ts/ts-basic-components-richeditor.md#abouttoimeinput) to trigger a callback before adding input content, and [onIMEInputComplete](../reference/apis-arkui/arkui-ts/ts-basic-components-richeditor.md#onimeinputcomplete) to trigger a callback after the input is complete.
317
318These callbacks can be used to provide text prediction before user input and to perform automatic error correction or format conversion after input completion.
319
320Note that the **RichEditor** component constructed with [RichEditorStyledStringOptions](../reference/apis-arkui/arkui-ts/ts-basic-components-richeditor.md#richeditorstyledstringoptions12) does not support these two types of callbacks.
321
322```ts
323RichEditor(this.options)
324  .onReady(() => {
325    this.controller.addTextSpan(''The callback is invoked before content input in the input method.\nThe callback is invoked when text input in the input method is complete.', {
326      style: {
327        fontColor: Color.Black,
328        fontSize: 15
329      }
330    })
331  })
332  .aboutToIMEInput((value: RichEditorInsertValue) => {
333    this.controller1.addTextSpan('The callback is invoked before content input in the input method: \n' + JSON.stringify(value), {
334      style: {
335        fontColor: Color.Gray,
336        fontSize: 10
337      }
338    })
339    return true;
340  })
341  .onIMEInputComplete((value: RichEditorTextSpanResult) => {
342    this.controller1.addTextSpan('The callback is invoked when text input in the input method is complete: \n' + JSON.stringify(value), {
343      style: {
344        fontColor: Color.Gray,
345        fontSize: 10
346      }
347    })
348    return true;
349  })
350  .width(300)
351  .height(50)
352Text('View callback content:').fontSize(10).fontColor(Color.Gray).width(300)
353RichEditor(this.options1)
354  .width(300)
355  .height(70)
356```
357
358![alt text](figures/richeditor_image_aboutToIMEInput2.0.gif)
359
360### Adding a Callback for Before Paste Completion
361Use the [onPaste](../reference/apis-arkui/arkui-ts/ts-basic-components-richeditor.md#onpaste11) API to add a callback invoked when the paste is about to be completed.
362
363This is useful for content format processing, such as converting text containing HTML tags to a format supported by the **RichEditor** component and removing unnecessary tags or retaining only plain text content.
364
365You can use this API to override the default pasting behavior, which is limited to plain text, so that both images and text can be pasted.
366
367```ts
368RichEditor(this.options)
369  .onReady(() => {
370    this.controller.addTextSpan('Copy and paste operations on this text trigger the corresponding callbacks.', {
371      style: {
372        fontColor: Color.Black,
373        fontSize: 15
374      }
375    })
376  })
377  .onPaste(() => {
378    this.controller1.addTextSpan('The onPaste callback is invoked.\n', {
379      style: {
380        fontColor: Color.Gray,
381        fontSize: 10
382      }
383    })
384  })
385  .width(300)
386  .height(70)
387```
388
389### Adding a Callback for Before Cut Completion
390Use the [onCut](../reference/apis-arkui/arkui-ts/ts-basic-components-richeditor.md#oncut12) API to add a callback invoked when text is about to be cut.
391
392This can be used to temporarily store the cut content, ensuring accurate restoration in subsequent pasting operations.
393
394You can use this API to override the default cutting behavior, which is limited to plain text, so that both images and text can be cut.
395
396```ts
397RichEditor(this.options)
398  .onReady(() => {
399    this.controller.addTextSpan('Copy and paste operations on this text trigger the corresponding callbacks.', {
400      style: {
401        fontColor: Color.Black,
402        fontSize: 15
403      }
404    })
405  })
406  .onCut(() => {
407    this.controller1.addTextSpan('The onCut callback is invoked.\n', {
408      style: {
409        fontColor: Color.Gray,
410        fontSize: 10
411      }
412    })
413  })
414  .width(300)
415  .height(70)
416```
417
418### Adding a Callback for Before Copy Completion
419Use the [onCopy](../reference/apis-arkui/arkui-ts/ts-basic-components-richeditor.md#oncopy12) API to add a callback invoked when text is about to be copied.
420
421This callback applies to content backup and sharing. For example, the following operations can be performed in the callback: saving the copied content and its format information to a local backup folder, or automatically generating copywriting that includes the copied content and a product purchase link for users to paste and share.
422
423You can use this API to override the default copying behavior, which is limited to plain text, so that both images and text can be copied.
424
425```ts
426RichEditor(this.options)
427  .onReady(() => {
428    this.controller.addTextSpan('Copy and paste operations on this text trigger the corresponding callbacks.', {
429      style: {
430        fontColor: Color.Black,
431        fontSize: 15
432      }
433    })
434  })
435  .onCopy(() => {
436    this.controller1.addTextSpan('The onCopy callback is invoked.\n', {
437      style: {
438        fontColor: Color.Gray,
439        fontSize: 10
440      }
441    })
442  })
443  .width(300)
444  .height(70)
445```
446
447![alt text](figures/richeditor_image_oncut_paste_copy.gif)
448
449
450For details about all available events, see [RichEditor Events](../reference/apis-arkui/arkui-ts/ts-basic-components-richeditor.md#events).
451
452## Setting the Typing Style
453Use the [setTypingStyle](../reference/apis-arkui/arkui-ts/ts-basic-components-richeditor.md#settypingstyle11) API to set the typing style.
454
455This allows you to deliver a personalized writing experience. For example, you might want to use this API to automatically apply corresponding formats to different levels of headings (such as level-1 and level-2 headings) as users type.
456
457```ts
458RichEditor(this.options)
459  .onReady(() => {
460    this.controller.addTextSpan('Click the button to change the preset typing style.', {
461      style: {
462        fontColor: Color.Black,
463        fontSize: 15
464      }
465    })
466  })
467  .width(300)
468  .height(60)
469Button('setTypingStyle', {
470  buttonStyle: ButtonStyleMode.NORMAL
471})
472  .height(30)
473  .fontSize(13)
474  .onClick(() => {
475    this.controller.setTypingStyle({
476      fontWeight: 'medium',
477      fontColor: Color.Pink,
478      fontSize: 15,
479      fontStyle: FontStyle.Italic,
480      decoration: {
481        type: TextDecorationType.Underline,
482        color: Color.Gray
483      }
484    })
485  })
486```
487
488![alt text](figures/richeditor_image_setTypingStyle.gif)
489
490## Setting Highlight for Selected Content
491Use the [setSelection](../reference/apis-arkui/arkui-ts/ts-basic-components-richeditor.md#setselection11) API to configure the component to highlight the background of the selected portion.
492
493This API enables the implementation of text focus effects. For example, clicking a title or abstract can trigger the automatic selection and highlighting of the corresponding text content.
494
495If this API is called when the text box is not focused, the selection effect is not displayed.
496
497```ts
498RichEditor(this.options)
499  .onReady(() => {
500    this.controller.addTextSpan('Click the button to select the text at positions 0 to 2 here.', {
501      style: {
502        fontColor: Color.Black,
503        fontSize: 15
504      }
505    })
506  })
507  .width(300)
508  .height(60)
509Button('setSelection(0,2)', {
510  buttonStyle: ButtonStyleMode.NORMAL
511})
512  .height(30)
513  .fontSize(13)
514  .onClick(() => {
515    this.controller.setSelection(0, 2)
516  })
517```
518
519![alt text](figures/richeditor_image_set_selection.gif)
520
521## Adding a Text Span
522In addition to directly entering content into the component, you can add a text span using the [addTextSpan](../reference/apis-arkui/arkui-ts/ts-basic-components-richeditor.md#addtextspan) API.
523
524This API offers a way to diversify text styles. For example, you can use it to create mixed text styles.
525
526When the component is focused and the cursor is blinking, adding text content through **addTextSpan** updates the cursor position to the right of the newly added text.
527
528```ts
529RichEditor(this.options)
530  .onReady(() => {
531    this.controller.addTextSpan('Click the button to add text here.', {
532      style: {
533        fontColor: Color.Black,
534        fontSize: 15
535      }
536    })
537  })
538  .width(300)
539  .height(100)
540Button('addTextSpan', {
541  buttonStyle: ButtonStyleMode.NORMAL
542})
543  .height(30)
544  .fontSize(13)
545  .onClick(() => {
546    this.controller.addTextSpan('Add text.')
547  })
548```
549
550![alt text](figures/richeditor_image_add_text.gif)
551
552## Adding an Image Span
553Use the [addImageSpan](../reference/apis-arkui/arkui-ts/ts-basic-components-richeditor.md#addimagespan) API to add an image span.
554
555This API is useful in enriching and visualizing content. For example, you can use it to add images to news or data visualization graphics to documents.
556
557When the component is focused and the cursor is blinking, adding image content through **addImageSpan** updates the cursor position to the right of the newly added image.
558
559```ts
560RichEditor(this.options)
561  .onReady(() => {
562    this.controller.addTextSpan('Click the button to add an image here.', {
563      style: {
564        fontColor: Color.Black,
565        fontSize: 15
566      }
567    })
568  })
569  .width(300)
570  .height(100)
571Button('addImageSpan', {
572  buttonStyle: ButtonStyleMode.NORMAL
573})
574  .height(30)
575  .fontSize(13)
576  .onClick(() => {
577    this.controller.addImageSpan($r("app.media.startIcon"), {
578      imageStyle: {
579        size: ["57px", "57px"]
580      }
581    })
582  })
583```
584
585![alt text](figures/richeditor_image_add_image.gif)
586
587## Adding @Builder Decorated Content
588Use [addBuilderSpan](../reference/apis-arkui/arkui-ts/ts-basic-components-richeditor.md#addbuilderspan11) to add the content decorated by the @Builder decorator.
589
590This approach applies when you need to integrate customized complex components, such as custom charts.
591
592With this API, you can specify the addition position using [RichEditorBuilderSpanOptions](../reference/apis-arkui/arkui-ts/ts-basic-components-richeditor.md#richeditorbuilderspanoptions11). If no position is specified or an abnormal value is provided, the builder is appended to the end of all content.
593
594```ts
595private my_builder: CustomBuilder = undefined
596
597@Builder
598TextBuilder() {
599  Row() {
600    Image($r('app.media.startIcon')).width(50).height(50).margin(16)
601    Column() {
602      Text("Text.txt").fontWeight(FontWeight.Bold).fontSize(16)
603      Text("123.45KB").fontColor('#8a8a8a').fontSize(12)
604    }.alignItems(HorizontalAlign.Start)
605  }.backgroundColor('#f4f4f4')
606  .borderRadius("20")
607  .width(220)
608}
609
610Button('addBuilderSpan', {
611  buttonStyle: ButtonStyleMode.NORMAL
612})
613  .height(30)
614  .fontSize(13)
615  .onClick(() => {
616    this.my_builder = () => {
617      this.TextBuilder()
618    }
619    this.controller.addBuilderSpan(this.my_builder)
620  })
621```
622![alt text](figures/richeditor_image_add_builder_span2.0.gif)
623
624## Adding a Symbol Span
625Use the [addSymbolSpan](../reference/apis-arkui/arkui-ts/ts-basic-components-richeditor.md#addsymbolspan11) API to add symbol content. This API enables the addition and display of special characters, such as mathematical symbols in academic papers.
626
627When the component is focused and the cursor is blinking, adding symbol content through **addSymbolSpan** updates the cursor position to the right of the newly added symbol.
628Currently, gestures, copying, and dragging are not supported for the symbol content.
629
630```ts
631RichEditor(this.options)
632  .onReady(() => {
633    this.controller.addTextSpan('Click the button to add a symbol here.', {
634      style: {
635        fontColor: Color.Black,
636        fontSize: 15
637      }
638    })
639  })
640  .width(300)
641  .height(100)
642Button('addSymbolSpan', {
643  buttonStyle: ButtonStyleMode.NORMAL
644})
645  .height(30)
646  .fontSize(13)
647  .onClick(() => {
648    this.controller.addSymbolSpan($r("sys.symbol.basketball_fill"), {
649      style: {
650        fontSize: 30
651      }
652    })
653  })
654```
655![alt text](figures/richeditor_image_add_SymbolSpan.gif)
656
657## Obtaining the Image and Text Information in a Component
658Use the [getSpans](../reference/apis-arkui/arkui-ts/ts-basic-components-richeditor.md#getspans) API to obtain information about all images and text in the component, including content, ID, style, and position. After obtaining the position information, you can update the style of the content in the specified range.
659
660This API is useful for obtaining and checking existing content styles, such as in template use cases, and for content parsing and processing, such as in text analysis applications.
661
662```ts
663controller: RichEditorController = new RichEditorController();
664options: RichEditorOptions = { controller: this.controller }
665controller1: RichEditorController = new RichEditorController();
666options1: RichEditorOptions = { controller: this.controller1 }
667// Create two RichEditor components.
668
669RichEditor(this.options)
670  .onReady(() => {
671    this.controller.addTextSpan('Click the button to obtain the span information.', {
672      style: {
673        fontColor: Color.Black,
674        fontSize: 15
675      }
676    })
677  })
678  .width(300)
679  .height(50)
680Text('View the return value of getSpans: ').fontSize(10).fontColor(Color.Gray).width(300)
681RichEditor(this.options1)
682  .width(300)
683  .height(50)
684Button('getSpans', {
685  buttonStyle: ButtonStyleMode.NORMAL
686})
687  .height(30)
688  .fontSize(13)
689  .onClick(() => {
690    this.controller1.addTextSpan(JSON.stringify(this.controller.getSpans()), {
691      style: {
692        fontColor: Color.Gray,
693        fontSize: 10
694      }
695    })
696  })
697```
698![alt text](figures/richeditor_image_getspan.gif)
699<!--RP1--><!--RP1End-->
700