1e41f4b71Sopenharmony_ci# Rich Text (RichEditor) 2e41f4b71Sopenharmony_ci**RichEditor** is a component that supports interactive text editing and mixture of text and imagery. It is typically used to handle user inputs with mixed content, such as comment sections that allow for image input. For details, see RichEditor](../reference/apis-arkui/arkui-ts/ts-basic-components-richeditor.md). 3e41f4b71Sopenharmony_ci 4e41f4b71Sopenharmony_ci## Creating a RichEditor Component 5e41f4b71Sopenharmony_ciYou create a **RichEditor** component by calling an API, which varies depending on the type of component you want to create. 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ci### Creating a RichEditor Component Without Using a Styled String 8e41f4b71Sopenharmony_ci```ts 9e41f4b71Sopenharmony_ciRichEditor(value: RichEditorOptions) 10e41f4b71Sopenharmony_ci``` 11e41f4b71Sopenharmony_ciCreates a **RichEditor** component with the initialization options specified by **RichEditorOptions**. 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci```ts 14e41f4b71Sopenharmony_cicontroller: RichEditorController = new RichEditorController(); 15e41f4b71Sopenharmony_cioptions: RichEditorOptions = { controller: this.controller }; 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ciRichEditor(this.options) 18e41f4b71Sopenharmony_ci .onReady(() => { 19e41f4b71Sopenharmony_ci this.controller.addTextSpan('Create a RichEditor component without using a styled string.', { 20e41f4b71Sopenharmony_ci style: { 21e41f4b71Sopenharmony_ci fontColor: Color.Black, 22e41f4b71Sopenharmony_ci fontSize: 15 23e41f4b71Sopenharmony_ci } 24e41f4b71Sopenharmony_ci }) 25e41f4b71Sopenharmony_ci }) 26e41f4b71Sopenharmony_ci``` 27e41f4b71Sopenharmony_ci 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ci### Creating a RichEditor Component Using a Styled String 30e41f4b71Sopenharmony_ci```ts 31e41f4b71Sopenharmony_ciRichEditor(options: RichEditorStyledStringOptions) 32e41f4b71Sopenharmony_ci``` 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_ciCreates a **RichEditor** component with the initialization options specified by **RichEditorStyledStringOptions**. 35e41f4b71Sopenharmony_ci 36e41f4b71Sopenharmony_ci```ts 37e41f4b71Sopenharmony_cimutableStyledString: MutableStyledString = new MutableStyledString("Create a RichEditor component using a styled string.", 38e41f4b71Sopenharmony_ci [{ 39e41f4b71Sopenharmony_ci start: 0, 40e41f4b71Sopenharmony_ci length: 5, 41e41f4b71Sopenharmony_ci styledKey: StyledStringKey.FONT, 42e41f4b71Sopenharmony_ci styledValue: this.fontStyle 43e41f4b71Sopenharmony_ci }]); 44e41f4b71Sopenharmony_ci 45e41f4b71Sopenharmony_cicontroller: RichEditorStyledStringController = new RichEditorStyledStringController(); 46e41f4b71Sopenharmony_cioptions: RichEditorStyledStringOptions = {controller: this.controller}; 47e41f4b71Sopenharmony_ci 48e41f4b71Sopenharmony_ciRichEditor(this.options) 49e41f4b71Sopenharmony_ci .onReady(() => { 50e41f4b71Sopenharmony_ci this.controller.setStyledString(this.mutableStyledString); 51e41f4b71Sopenharmony_ci }) 52e41f4b71Sopenharmony_ci``` 53e41f4b71Sopenharmony_ci 54e41f4b71Sopenharmony_ci 55e41f4b71Sopenharmony_ci## Setting Attributes 56e41f4b71Sopenharmony_ci 57e41f4b71Sopenharmony_ci### Setting the Custom Context Menu on Text Selection 58e41f4b71Sopenharmony_ci 59e41f4b71Sopenharmony_ciYou can set custom context menu on text selection using the [bindSelectionMenu](../reference/apis-arkui/arkui-ts/ts-basic-components-richeditor.md#bindselectionmenu) API, which includes the following parameters: 60e41f4b71Sopenharmony_ci 61e41f4b71Sopenharmony_ci**spanType**: type of the menu, with the default being text.<br>**content**: content of the menu.<br>**responseType**: response type of the menu, with the default being a long press.<br>**options**: options of the menu, allowing you to set callbacks for when the menu appears or disappears. 62e41f4b71Sopenharmony_ci 63e41f4b71Sopenharmony_ciIf the custom menu is too long, embed a **Scroll** component to prevent the keyboard from being blocked. 64e41f4b71Sopenharmony_ci 65e41f4b71Sopenharmony_ci```ts 66e41f4b71Sopenharmony_ciRichEditor(this.options) 67e41f4b71Sopenharmony_ci .onReady(() => { 68e41f4b71Sopenharmony_ci this.controller.addTextSpan('The component has a custom menu that can be triggered by a long press.', { 69e41f4b71Sopenharmony_ci style: { 70e41f4b71Sopenharmony_ci fontColor: Color.Black, 71e41f4b71Sopenharmony_ci fontSize: 18 72e41f4b71Sopenharmony_ci } 73e41f4b71Sopenharmony_ci }) 74e41f4b71Sopenharmony_ci }) 75e41f4b71Sopenharmony_ci .bindSelectionMenu(RichEditorSpanType.TEXT, this.SystemMenu, ResponseType.LongPress, { 76e41f4b71Sopenharmony_ci onDisappear: () => { 77e41f4b71Sopenharmony_ci this.sliderShow = false 78e41f4b71Sopenharmony_ci } 79e41f4b71Sopenharmony_ci }) 80e41f4b71Sopenharmony_ci .width(300) 81e41f4b71Sopenharmony_ci .height(300) 82e41f4b71Sopenharmony_ci 83e41f4b71Sopenharmony_ci@Builder 84e41f4b71Sopenharmony_ciSystemMenu() { 85e41f4b71Sopenharmony_ci Column() { 86e41f4b71Sopenharmony_ci Menu() { 87e41f4b71Sopenharmony_ci if (this.controller) { 88e41f4b71Sopenharmony_ci MenuItemGroup() { 89e41f4b71Sopenharmony_ci MenuItem({ 90e41f4b71Sopenharmony_ci startIcon: this.theme.cutIcon, 91e41f4b71Sopenharmony_ci content: "Cut", 92e41f4b71Sopenharmony_ci labelInfo: "Ctrl+X", 93e41f4b71Sopenharmony_ci }) 94e41f4b71Sopenharmony_ci MenuItem({ 95e41f4b71Sopenharmony_ci startIcon: this.theme.copyIcon, 96e41f4b71Sopenharmony_ci content: "Copy", 97e41f4b71Sopenharmony_ci labelInfo: "Ctrl+C" 98e41f4b71Sopenharmony_ci }) 99e41f4b71Sopenharmony_ci MenuItem({ 100e41f4b71Sopenharmony_ci startIcon: this.theme.pasteIcon, 101e41f4b71Sopenharmony_ci content: "Paste", 102e41f4b71Sopenharmony_ci labelInfo: "Ctrl+V" 103e41f4b71Sopenharmony_ci }) 104e41f4b71Sopenharmony_ci } 105e41f4b71Sopenharmony_ci } 106e41f4b71Sopenharmony_ci } 107e41f4b71Sopenharmony_ci .radius(this.theme.containerBorderRadius) 108e41f4b71Sopenharmony_ci .clip(true) 109e41f4b71Sopenharmony_ci .backgroundColor(Color.White) 110e41f4b71Sopenharmony_ci .width(this.theme.defaultMenuWidth) 111e41f4b71Sopenharmony_ci } 112e41f4b71Sopenharmony_ci .width(this.theme.defaultMenuWidth) 113e41f4b71Sopenharmony_ci} 114e41f4b71Sopenharmony_ci``` 115e41f4b71Sopenharmony_ci 116e41f4b71Sopenharmony_ci 117e41f4b71Sopenharmony_ci 118e41f4b71Sopenharmony_ci### Setting the Color of the Caret and Selection Handle in the Text Box 119e41f4b71Sopenharmony_ci 120e41f4b71Sopenharmony_ciYou can set the color of the caret and selection handle in the text box using the [caretColor](../reference/apis-arkui/arkui-ts/ts-basic-components-richeditor.md#caretcolor12) API. 121e41f4b71Sopenharmony_ci 122e41f4b71Sopenharmony_ci```ts 123e41f4b71Sopenharmony_ciRichEditor(this.options) 124e41f4b71Sopenharmony_ci .onReady(() => { 125e41f4b71Sopenharmony_ci this.controller.addTextSpan('The component has the color set for the caret and selection handle.', { 126e41f4b71Sopenharmony_ci style: { 127e41f4b71Sopenharmony_ci fontColor: Color.Black, 128e41f4b71Sopenharmony_ci fontSize: 15 129e41f4b71Sopenharmony_ci } 130e41f4b71Sopenharmony_ci }) 131e41f4b71Sopenharmony_ci }) 132e41f4b71Sopenharmony_ci .caretColor(Color.Orange) 133e41f4b71Sopenharmony_ci .width(300) 134e41f4b71Sopenharmony_ci .height(300) 135e41f4b71Sopenharmony_ci``` 136e41f4b71Sopenharmony_ci 137e41f4b71Sopenharmony_ci 138e41f4b71Sopenharmony_ci 139e41f4b71Sopenharmony_ci### Setting Placeholder Text 140e41f4b71Sopenharmony_ci 141e41f4b71Sopenharmony_ciYou 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. 142e41f4b71Sopenharmony_ci 143e41f4b71Sopenharmony_ciIn the API, **value** indicates the placeholder text displayed when there is no input; **style** indicates the font style for the placeholder text. If not specified, the style follows the theme settings. 144e41f4b71Sopenharmony_ci 145e41f4b71Sopenharmony_ci```ts 146e41f4b71Sopenharmony_ciRichEditor(this.options) 147e41f4b71Sopenharmony_ci .placeholder("Enter your content here", { 148e41f4b71Sopenharmony_ci fontColor: Color.Gray, 149e41f4b71Sopenharmony_ci font: { 150e41f4b71Sopenharmony_ci size: 15, 151e41f4b71Sopenharmony_ci weight: FontWeight.Normal, 152e41f4b71Sopenharmony_ci family: "HarmonyOS Sans", 153e41f4b71Sopenharmony_ci style: FontStyle.Normal 154e41f4b71Sopenharmony_ci } 155e41f4b71Sopenharmony_ci }) 156e41f4b71Sopenharmony_ci .width(300) 157e41f4b71Sopenharmony_ci .height(300) 158e41f4b71Sopenharmony_ci``` 159e41f4b71Sopenharmony_ci 160e41f4b71Sopenharmony_ci 161e41f4b71Sopenharmony_ci 162e41f4b71Sopenharmony_ciFor details about all available attributes, see [RichEditor Attributes](../reference/apis-arkui/arkui-ts/ts-basic-components-richeditor.md#attributes). 163e41f4b71Sopenharmony_ci 164e41f4b71Sopenharmony_ci## Adding Events 165e41f4b71Sopenharmony_ci### Adding a Callback for Component Initialization 166e41f4b71Sopenharmony_ci 167e41f4b71Sopenharmony_ciUse 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. 168e41f4b71Sopenharmony_ci 169e41f4b71Sopenharmony_ciIn the API, **callback** indicates the callback invoked when the **RichEditor** component has completed initialization. 170e41f4b71Sopenharmony_ci 171e41f4b71Sopenharmony_ci```ts 172e41f4b71Sopenharmony_ciRichEditor(this.options) 173e41f4b71Sopenharmony_ci .onReady(() => { 174e41f4b71Sopenharmony_ci this.controller.addTextSpan('The onReady callback content is preset text within the component.', { 175e41f4b71Sopenharmony_ci style: { 176e41f4b71Sopenharmony_ci fontColor: Color.Black, 177e41f4b71Sopenharmony_ci fontSize: 15 178e41f4b71Sopenharmony_ci } 179e41f4b71Sopenharmony_ci }) 180e41f4b71Sopenharmony_ci }) 181e41f4b71Sopenharmony_ci``` 182e41f4b71Sopenharmony_ci 183e41f4b71Sopenharmony_ci 184e41f4b71Sopenharmony_ci 185e41f4b71Sopenharmony_ci### Adding a Callback for Content Selection 186e41f4b71Sopenharmony_ci 187e41f4b71Sopenharmony_ciUse 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. 188e41f4b71Sopenharmony_ci 189e41f4b71Sopenharmony_ciIn **callback**, [RichEditorSelection](../reference/apis-arkui/arkui-ts/ts-basic-components-richeditor.md#richeditorselection) provides information about all the selected content. 190e41f4b71Sopenharmony_ci 191e41f4b71Sopenharmony_ciThe callback can be invoked in either of the following ways: (1) If content is selected with the mouse left button, the callback is invoked after the left button is released. (2) If content is selected with a finger, the callback is invoked after the finger is lifted. 192e41f4b71Sopenharmony_ci 193e41f4b71Sopenharmony_ci```ts 194e41f4b71Sopenharmony_ciRichEditor(this.options) 195e41f4b71Sopenharmony_ci .onReady(() => { 196e41f4b71Sopenharmony_ci this.controller.addTextSpan('Select this text to invoke the onSelect callback.', { 197e41f4b71Sopenharmony_ci style: { 198e41f4b71Sopenharmony_ci fontColor: Color.Black, 199e41f4b71Sopenharmony_ci fontSize: 15 200e41f4b71Sopenharmony_ci } 201e41f4b71Sopenharmony_ci }) 202e41f4b71Sopenharmony_ci }) 203e41f4b71Sopenharmony_ci .onSelect((value: RichEditorSelection) => { 204e41f4b71Sopenharmony_ci this.controller1.addTextSpan(JSON.stringify(value), { 205e41f4b71Sopenharmony_ci style: { 206e41f4b71Sopenharmony_ci fontColor: Color.Gray, 207e41f4b71Sopenharmony_ci fontSize: 10 208e41f4b71Sopenharmony_ci } 209e41f4b71Sopenharmony_ci }) 210e41f4b71Sopenharmony_ci }) 211e41f4b71Sopenharmony_ci .width(300) 212e41f4b71Sopenharmony_ci .height(50) 213e41f4b71Sopenharmony_ciText('View callback content:').fontSize(10).fontColor(Color.Gray).width(300) 214e41f4b71Sopenharmony_ciRichEditor(this.options1) 215e41f4b71Sopenharmony_ci .width(300) 216e41f4b71Sopenharmony_ci .height(70) 217e41f4b71Sopenharmony_ci ``` 218e41f4b71Sopenharmony_ci 219e41f4b71Sopenharmony_ci 220e41f4b71Sopenharmony_ci 221e41f4b71Sopenharmony_ci### Adding Callbacks for Before and After Text and Image Changes 222e41f4b71Sopenharmony_ci 223e41f4b71Sopenharmony_ciUse the [onWillChange](../reference/apis-arkui/arkui-ts/ts-basic-components-richeditor.md#onwillchange12) API to add a callback invoked before text or image changes. Use the [onDidChange](../reference/apis-arkui/arkui-ts/ts-basic-components-richeditor.md#ondidchange12) API to add a callback invoked after text or image changes. 224e41f4b71Sopenharmony_ci 225e41f4b71Sopenharmony_ciIn the **onWillChange** callback, [RichEditorChangeValue](../reference/apis-arkui/arkui-ts/ts-basic-components-richeditor.md#richeditorchangevalue12) provides information about the text and image change; a boolean value indicates whether the change is allowed (**true**) or not allowed (**false**). 226e41f4b71Sopenharmony_ci 227e41f4b71Sopenharmony_ciIn the **onDidChange** callback, [OnDidChangeCallback](../reference/apis-arkui/arkui-ts/ts-basic-components-richeditor.md#ondidchangecallback12) provides the content range before and after the text or image change. 228e41f4b71Sopenharmony_ci 229e41f4b71Sopenharmony_ciThe **RichEditor** component constructed with [RichEditorStyledStringOptions](../reference/apis-arkui/arkui-ts/ts-basic-components-richeditor.md#richeditorstyledstringoptions12) does not support these two types of callbacks. 230e41f4b71Sopenharmony_ci 231e41f4b71Sopenharmony_ci```ts 232e41f4b71Sopenharmony_ciRichEditor(this.options) 233e41f4b71Sopenharmony_ci .onReady(() => { 234e41f4b71Sopenharmony_ci this.controller.addTextSpan('The callback is invoked before the text or image change.\nThe callback is invoked after the text or image change.', { 235e41f4b71Sopenharmony_ci style: { 236e41f4b71Sopenharmony_ci fontColor: Color.Black, 237e41f4b71Sopenharmony_ci fontSize: 15 238e41f4b71Sopenharmony_ci } 239e41f4b71Sopenharmony_ci }) 240e41f4b71Sopenharmony_ci }) 241e41f4b71Sopenharmony_ci .onWillChange((value: RichEditorChangeValue) => { 242e41f4b71Sopenharmony_ci this.controller1.addTextSpan('The callback is invoked before the text or image change: \n' + JSON.stringify(value), { 243e41f4b71Sopenharmony_ci style: { 244e41f4b71Sopenharmony_ci fontColor: Color.Gray, 245e41f4b71Sopenharmony_ci fontSize: 10 246e41f4b71Sopenharmony_ci } 247e41f4b71Sopenharmony_ci }) 248e41f4b71Sopenharmony_ci return true; 249e41f4b71Sopenharmony_ci }) 250e41f4b71Sopenharmony_ci .onDidChange((rangeBefore: TextRange, rangeAfter: TextRange) => { 251e41f4b71Sopenharmony_ci this.controller1.addTextSpan('\nThe callback is invoked after the text or image change: \nrangeBefore: ' + JSON.stringify(rangeBefore) + '\nrangeAfter: ' + JSON.stringify(rangeBefore), { 252e41f4b71Sopenharmony_ci style: { 253e41f4b71Sopenharmony_ci fontColor: Color.Gray, 254e41f4b71Sopenharmony_ci fontSize: 10 255e41f4b71Sopenharmony_ci } 256e41f4b71Sopenharmony_ci }) 257e41f4b71Sopenharmony_ci return true; 258e41f4b71Sopenharmony_ci }) 259e41f4b71Sopenharmony_ci .width(300) 260e41f4b71Sopenharmony_ci .height(50) 261e41f4b71Sopenharmony_ciText('View callback content:').fontSize(10).fontColor(Color.Gray).width(300) 262e41f4b71Sopenharmony_ciRichEditor(this.options1) 263e41f4b71Sopenharmony_ci .width(300) 264e41f4b71Sopenharmony_ci .height(70) 265e41f4b71Sopenharmony_ci``` 266e41f4b71Sopenharmony_ci 267e41f4b71Sopenharmony_ci 268e41f4b71Sopenharmony_ci 269e41f4b71Sopenharmony_ci### Adding Callbacks for Before and After Content Input in the Input Method 270e41f4b71Sopenharmony_ci 271e41f4b71Sopenharmony_ciUse the [aboutToIMEInput](../reference/apis-arkui/arkui-ts/ts-basic-components-richeditor.md#abouttoimeinput) API to add a callback invoked when content is about to be entered in the input method. Use the [onIMEInputComplete](../reference/apis-arkui/arkui-ts/ts-basic-components-richeditor.md#onimeinputcomplete) method to add a callback invoked when text input in the input method is complete. 272e41f4b71Sopenharmony_ci 273e41f4b71Sopenharmony_ciIn the **aboutToIMEInput** callback, [RichEditorInsertValue](../reference/apis-arkui/arkui-ts/ts-basic-components-richeditor.md#richeditorinsertvalue) provides information about the content to be entered in the input method; a boolean value indicates whether the content is inserted (**true**) or not (**false**). 274e41f4b71Sopenharmony_ci 275e41f4b71Sopenharmony_ciIn the **onIMEInputComplete** callback, [RichEditorTextSpanResult](../reference/apis-arkui/arkui-ts/ts-basic-components-richeditor.md#richeditortextspanresult) provides information about the text span after the text input is completed. 276e41f4b71Sopenharmony_ci 277e41f4b71Sopenharmony_ciThe **RichEditor** component constructed with [RichEditorStyledStringOptions](../reference/apis-arkui/arkui-ts/ts-basic-components-richeditor.md#richeditorstyledstringoptions12) does not support these two types of callbacks. 278e41f4b71Sopenharmony_ci 279e41f4b71Sopenharmony_ci```ts 280e41f4b71Sopenharmony_ciRichEditor(this.options) 281e41f4b71Sopenharmony_ci .onReady(() => { 282e41f4b71Sopenharmony_ci 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.' , { 283e41f4b71Sopenharmony_ci style: { 284e41f4b71Sopenharmony_ci fontColor: Color.Black, 285e41f4b71Sopenharmony_ci fontSize: 15 286e41f4b71Sopenharmony_ci } 287e41f4b71Sopenharmony_ci }) 288e41f4b71Sopenharmony_ci }) 289e41f4b71Sopenharmony_ci .aboutToIMEInput((value: RichEditorInsertValue) => { 290e41f4b71Sopenharmony_ci this.controller1.addTextSpan('The callback is invoked before content input in the input method: \n'+JSON.stringify(value), { 291e41f4b71Sopenharmony_ci style: { 292e41f4b71Sopenharmony_ci fontColor: Color.Gray, 293e41f4b71Sopenharmony_ci fontSize: 10 294e41f4b71Sopenharmony_ci } 295e41f4b71Sopenharmony_ci }) 296e41f4b71Sopenharmony_ci return true; 297e41f4b71Sopenharmony_ci }) 298e41f4b71Sopenharmony_ci .onIMEInputComplete((value: RichEditorTextSpanResult) => { 299e41f4b71Sopenharmony_ci this.controller1.addTextSpan('The callback is invoked when text input in the input method is complete: \n'+ JSON.stringify(value), { 300e41f4b71Sopenharmony_ci style: { 301e41f4b71Sopenharmony_ci fontColor: Color.Gray, 302e41f4b71Sopenharmony_ci fontSize: 10 303e41f4b71Sopenharmony_ci } 304e41f4b71Sopenharmony_ci }) 305e41f4b71Sopenharmony_ci return true; 306e41f4b71Sopenharmony_ci }) 307e41f4b71Sopenharmony_ci .width(300) 308e41f4b71Sopenharmony_ci .height(50) 309e41f4b71Sopenharmony_ciText('View callback content:').fontSize(10).fontColor(Color.Gray).width(300) 310e41f4b71Sopenharmony_ciRichEditor(this.options1) 311e41f4b71Sopenharmony_ci .width(300) 312e41f4b71Sopenharmony_ci .height(70) 313e41f4b71Sopenharmony_ci``` 314e41f4b71Sopenharmony_ci 315e41f4b71Sopenharmony_ci 316e41f4b71Sopenharmony_ci 317e41f4b71Sopenharmony_ci### Adding a Callback for Before Paste Completion 318e41f4b71Sopenharmony_ci 319e41f4b71Sopenharmony_ciUse 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. 320e41f4b71Sopenharmony_ci 321e41f4b71Sopenharmony_ciIn the API, **callback** is used to define the user paste event. 322e41f4b71Sopenharmony_ci 323e41f4b71Sopenharmony_ciBy default, only plain text can be pasted. You can use this API to override the default system behavior so that both images and text can be pasted. 324e41f4b71Sopenharmony_ci 325e41f4b71Sopenharmony_ci```ts 326e41f4b71Sopenharmony_ciRichEditor(this.options) 327e41f4b71Sopenharmony_ci .onReady(() => { 328e41f4b71Sopenharmony_ci this.controller.addTextSpan('Copy and paste operations on this text trigger the corresponding callbacks.', { 329e41f4b71Sopenharmony_ci style: { 330e41f4b71Sopenharmony_ci fontColor: Color.Black, 331e41f4b71Sopenharmony_ci fontSize: 15 332e41f4b71Sopenharmony_ci } 333e41f4b71Sopenharmony_ci }) 334e41f4b71Sopenharmony_ci }) 335e41f4b71Sopenharmony_ci .onPaste(() => { 336e41f4b71Sopenharmony_ci this.controller1.addTextSpan ('The onPaste callback is invoked.\n', { 337e41f4b71Sopenharmony_ci style: { 338e41f4b71Sopenharmony_ci fontColor: Color.Gray, 339e41f4b71Sopenharmony_ci fontSize: 10 340e41f4b71Sopenharmony_ci } 341e41f4b71Sopenharmony_ci }) 342e41f4b71Sopenharmony_ci }) 343e41f4b71Sopenharmony_ci .width(300) 344e41f4b71Sopenharmony_ci .height(70) 345e41f4b71Sopenharmony_ci``` 346e41f4b71Sopenharmony_ci 347e41f4b71Sopenharmony_ci### Adding a Callback for Before Cut Completion 348e41f4b71Sopenharmony_ci 349e41f4b71Sopenharmony_ciUse 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. 350e41f4b71Sopenharmony_ci 351e41f4b71Sopenharmony_ciIn the API, **callback** is used to define the user cut event. 352e41f4b71Sopenharmony_ci 353e41f4b71Sopenharmony_ciBy default, only plain text can be cut. You can use this API to override the default system behavior so that both images and text can be cut. 354e41f4b71Sopenharmony_ci 355e41f4b71Sopenharmony_ci```ts 356e41f4b71Sopenharmony_ciRichEditor(this.options) 357e41f4b71Sopenharmony_ci .onReady(() => { 358e41f4b71Sopenharmony_ci this.controller.addTextSpan('Copy and paste operations on this text trigger the corresponding callbacks.', { 359e41f4b71Sopenharmony_ci style: { 360e41f4b71Sopenharmony_ci fontColor: Color.Black, 361e41f4b71Sopenharmony_ci fontSize: 15 362e41f4b71Sopenharmony_ci } 363e41f4b71Sopenharmony_ci }) 364e41f4b71Sopenharmony_ci }) 365e41f4b71Sopenharmony_ci .onCut(() => { 366e41f4b71Sopenharmony_ci this.controller1.addTextSpan ('The onCut callback is invoked.\n', { 367e41f4b71Sopenharmony_ci style: { 368e41f4b71Sopenharmony_ci fontColor: Color.Gray, 369e41f4b71Sopenharmony_ci fontSize: 10 370e41f4b71Sopenharmony_ci } 371e41f4b71Sopenharmony_ci }) 372e41f4b71Sopenharmony_ci }) 373e41f4b71Sopenharmony_ci .width(300) 374e41f4b71Sopenharmony_ci .height(70) 375e41f4b71Sopenharmony_ci``` 376e41f4b71Sopenharmony_ci 377e41f4b71Sopenharmony_ci### Adding a Callback for Before Copy Completion 378e41f4b71Sopenharmony_ci 379e41f4b71Sopenharmony_ciUse 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. 380e41f4b71Sopenharmony_ci 381e41f4b71Sopenharmony_ciIn the API, **callback** is used to define the user copy event. 382e41f4b71Sopenharmony_ci 383e41f4b71Sopenharmony_ciBy default, only plain text can be copied. You can use this API to override the default system behavior so that both images and text can be copied. 384e41f4b71Sopenharmony_ci 385e41f4b71Sopenharmony_ci```ts 386e41f4b71Sopenharmony_ciRichEditor(this.options) 387e41f4b71Sopenharmony_ci .onReady(() => { 388e41f4b71Sopenharmony_ci this.controller.addTextSpan('Copy and paste operations on this text trigger the corresponding callbacks.', { 389e41f4b71Sopenharmony_ci style: { 390e41f4b71Sopenharmony_ci fontColor: Color.Black, 391e41f4b71Sopenharmony_ci fontSize: 15 392e41f4b71Sopenharmony_ci } 393e41f4b71Sopenharmony_ci }) 394e41f4b71Sopenharmony_ci }) 395e41f4b71Sopenharmony_ci .onCopy(() => { 396e41f4b71Sopenharmony_ci this.controller1.addTextSpan ('The onCopy callback is invoked.\n', { 397e41f4b71Sopenharmony_ci style: { 398e41f4b71Sopenharmony_ci fontColor: Color.Gray, 399e41f4b71Sopenharmony_ci fontSize: 10 400e41f4b71Sopenharmony_ci } 401e41f4b71Sopenharmony_ci }) 402e41f4b71Sopenharmony_ci }) 403e41f4b71Sopenharmony_ci .width(300) 404e41f4b71Sopenharmony_ci .height(70) 405e41f4b71Sopenharmony_ci``` 406e41f4b71Sopenharmony_ci 407e41f4b71Sopenharmony_ci 408e41f4b71Sopenharmony_ci 409e41f4b71Sopenharmony_ci 410e41f4b71Sopenharmony_ciFor details about all available events, see [RichEditor Events](../reference/apis-arkui/arkui-ts/ts-basic-components-richeditor.md#events). 411e41f4b71Sopenharmony_ci 412e41f4b71Sopenharmony_ci## Setting the Typing Style 413e41f4b71Sopenharmony_ci 414e41f4b71Sopenharmony_ciUse the [setTypingStyle](../reference/apis-arkui/arkui-ts/ts-basic-components-richeditor.md#settypingstyle12) API to set the typing style. 415e41f4b71Sopenharmony_ci 416e41f4b71Sopenharmony_ciIn the API, **value** indicates the preset typing style. 417e41f4b71Sopenharmony_ci 418e41f4b71Sopenharmony_ci```ts 419e41f4b71Sopenharmony_ciRichEditor(this.options) 420e41f4b71Sopenharmony_ci .onReady(() => { 421e41f4b71Sopenharmony_ci this.controller.addTextSpan('Click the button to change the preset typing style.', { 422e41f4b71Sopenharmony_ci style: { 423e41f4b71Sopenharmony_ci fontColor: Color.Black, 424e41f4b71Sopenharmony_ci fontSize: 15 425e41f4b71Sopenharmony_ci } 426e41f4b71Sopenharmony_ci }) 427e41f4b71Sopenharmony_ci }) 428e41f4b71Sopenharmony_ci .width(300) 429e41f4b71Sopenharmony_ci .height(60) 430e41f4b71Sopenharmony_ciButton('setTypingStyle', { 431e41f4b71Sopenharmony_ci buttonStyle: ButtonStyleMode.NORMAL 432e41f4b71Sopenharmony_ci }) 433e41f4b71Sopenharmony_ci .height(30) 434e41f4b71Sopenharmony_ci .fontSize(13) 435e41f4b71Sopenharmony_ci .onClick(() => { 436e41f4b71Sopenharmony_ci this.controller.setTypingStyle({ 437e41f4b71Sopenharmony_ci fontWeight: 'medium', 438e41f4b71Sopenharmony_ci fontColor: Color.Pink, 439e41f4b71Sopenharmony_ci fontSize: 15, 440e41f4b71Sopenharmony_ci fontStyle: FontStyle.Italic, 441e41f4b71Sopenharmony_ci decoration: { 442e41f4b71Sopenharmony_ci type: TextDecorationType.Underline, 443e41f4b71Sopenharmony_ci color: Color.Gray 444e41f4b71Sopenharmony_ci } 445e41f4b71Sopenharmony_ci }) 446e41f4b71Sopenharmony_ci }) 447e41f4b71Sopenharmony_ci``` 448e41f4b71Sopenharmony_ci 449e41f4b71Sopenharmony_ci 450e41f4b71Sopenharmony_ci 451e41f4b71Sopenharmony_ci## Setting Highlight for Selected Content 452e41f4b71Sopenharmony_ci 453e41f4b71Sopenharmony_ciUse the [setSelection](../reference/apis-arkui/arkui-ts/ts-basic-components-richeditor.md#setselection12) API to configure the component to highlight the background of the selected portion. 454e41f4b71Sopenharmony_ci 455e41f4b71Sopenharmony_ciIn the API:<br>**selectionStart**: start position of the selection.<br>selectionEnd: end position of the selection. If both **selectionStart** and **selectionEnd** are set to **-1**, the entire content is selected. 456e41f4b71Sopenharmony_ci 457e41f4b71Sopenharmony_ciIf this API is called when the text box is not focused, the selected effect is not displayed. 458e41f4b71Sopenharmony_ci 459e41f4b71Sopenharmony_ci```ts 460e41f4b71Sopenharmony_ciRichEditor(this.options) 461e41f4b71Sopenharmony_ci .onReady(() => { 462e41f4b71Sopenharmony_ci this.controller.addTextSpan('Click the button to select the text at positions 0 to 2 here.', { 463e41f4b71Sopenharmony_ci style: { 464e41f4b71Sopenharmony_ci fontColor: Color.Black, 465e41f4b71Sopenharmony_ci fontSize: 15 466e41f4b71Sopenharmony_ci } 467e41f4b71Sopenharmony_ci }) 468e41f4b71Sopenharmony_ci }) 469e41f4b71Sopenharmony_ci .width(300) 470e41f4b71Sopenharmony_ci .height(60) 471e41f4b71Sopenharmony_ciButton('setSelection(0,2)', { 472e41f4b71Sopenharmony_ci buttonStyle: ButtonStyleMode.NORMAL 473e41f4b71Sopenharmony_ci }) 474e41f4b71Sopenharmony_ci .height(30) 475e41f4b71Sopenharmony_ci .fontSize(13) 476e41f4b71Sopenharmony_ci .onClick(() => { 477e41f4b71Sopenharmony_ci this.controller.setSelection(0, 2) 478e41f4b71Sopenharmony_ci }) 479e41f4b71Sopenharmony_ci``` 480e41f4b71Sopenharmony_ci 481e41f4b71Sopenharmony_ci 482e41f4b71Sopenharmony_ci 483e41f4b71Sopenharmony_ci## Adding a Text Span 484e41f4b71Sopenharmony_ci 485e41f4b71Sopenharmony_ciIn addition to directly entering content into the component, you can also add a text span using the [addTextSpan](../reference/apis-arkui/arkui-ts/ts-basic-components-richeditor.md#addtextspan) API. 486e41f4b71Sopenharmony_ci 487e41f4b71Sopenharmony_ciIn the API, **value** indicates the text content to add, and **options** indicates the text options, which include the offset position where the text is added and the text style information ([RichEditorParagraphStyle](../reference/apis-arkui/arkui-ts/ts-basic-components-richeditor.md#richeditortextspanoptions)). 488e41f4b71Sopenharmony_ci 489e41f4b71Sopenharmony_ciIf the cursor in the component is blinking, the cursor position is updated to be after the inserted text span. 490e41f4b71Sopenharmony_ci 491e41f4b71Sopenharmony_ci```ts 492e41f4b71Sopenharmony_ciRichEditor(this.options) 493e41f4b71Sopenharmony_ci .onReady(() => { 494e41f4b71Sopenharmony_ci this.controller.addTextSpan('Click the button to add text here.', { 495e41f4b71Sopenharmony_ci style: { 496e41f4b71Sopenharmony_ci fontColor: Color.Black, 497e41f4b71Sopenharmony_ci fontSize: 15 498e41f4b71Sopenharmony_ci } 499e41f4b71Sopenharmony_ci }) 500e41f4b71Sopenharmony_ci }) 501e41f4b71Sopenharmony_ci .width(300) 502e41f4b71Sopenharmony_ci .height(100) 503e41f4b71Sopenharmony_ciButton('addTextSpan', { 504e41f4b71Sopenharmony_ci buttonStyle: ButtonStyleMode.NORMAL 505e41f4b71Sopenharmony_ci }) 506e41f4b71Sopenharmony_ci .height(30) 507e41f4b71Sopenharmony_ci .fontSize(13) 508e41f4b71Sopenharmony_ci .onClick(() => { 509e41f4b71Sopenharmony_ci this.controller.addTextSpan('Add text.') 510e41f4b71Sopenharmony_ci }) 511e41f4b71Sopenharmony_ci``` 512e41f4b71Sopenharmony_ci 513e41f4b71Sopenharmony_ci 514e41f4b71Sopenharmony_ci 515e41f4b71Sopenharmony_ci## Adding an Image Span 516e41f4b71Sopenharmony_ci 517e41f4b71Sopenharmony_ciUse the [addImageSpan](../reference/apis-arkui/arkui-ts/ts-basic-components-richeditor.md#addimagespan) API to add an image span. 518e41f4b71Sopenharmony_ci 519e41f4b71Sopenharmony_ciIn the API, **value** indicates the image content to be added, and **options** indicates the image options, which include the offset position where the image is added and the image style information ([RichEditorImageSpanOptions](../reference/apis-arkui/arkui-ts/ts-basic-components-richeditor.md#richeditorimagespanoptions)). 520e41f4b71Sopenharmony_ci 521e41f4b71Sopenharmony_ciAdds an image span. If the cursor in the component is blinking, the cursor position is updated to be after the inserted image span. 522e41f4b71Sopenharmony_ci 523e41f4b71Sopenharmony_ci```ts 524e41f4b71Sopenharmony_ciRichEditor(this.options) 525e41f4b71Sopenharmony_ci .onReady(() => { 526e41f4b71Sopenharmony_ci this.controller.addTextSpan('Click the button to add an image here.', { 527e41f4b71Sopenharmony_ci style: { 528e41f4b71Sopenharmony_ci fontColor: Color.Black, 529e41f4b71Sopenharmony_ci fontSize: 15 530e41f4b71Sopenharmony_ci } 531e41f4b71Sopenharmony_ci }) 532e41f4b71Sopenharmony_ci }) 533e41f4b71Sopenharmony_ci .width(300) 534e41f4b71Sopenharmony_ci .height(100) 535e41f4b71Sopenharmony_ciButton('addImageSpan', { 536e41f4b71Sopenharmony_ci buttonStyle: ButtonStyleMode.NORMAL 537e41f4b71Sopenharmony_ci }) 538e41f4b71Sopenharmony_ci .height(30) 539e41f4b71Sopenharmony_ci .fontSize(13) 540e41f4b71Sopenharmony_ci .onClick(() => { 541e41f4b71Sopenharmony_ci this.controller.addImageSpan($r("app.media.startIcon"), { 542e41f4b71Sopenharmony_ci imageStyle: { 543e41f4b71Sopenharmony_ci size: ["57px", "57px"] 544e41f4b71Sopenharmony_ci } 545e41f4b71Sopenharmony_ci }) 546e41f4b71Sopenharmony_ci }) 547e41f4b71Sopenharmony_ci``` 548e41f4b71Sopenharmony_ci 549e41f4b71Sopenharmony_ci 550e41f4b71Sopenharmony_ci 551e41f4b71Sopenharmony_ci## Adding a Builder Span 552e41f4b71Sopenharmony_ciUse the [addBuilderSpan](../reference/apis-arkui/arkui-ts/ts-basic-components-richeditor.md#addbuilderspan11) API to add a builder span. 553e41f4b71Sopenharmony_ci 554e41f4b71Sopenharmony_ciIn the API, **value** indicates the content created by the builder, and **options** indicates the builder options, which allow you to set the index of this builder in the **RichEditor** component through [RichEditorBuilderSpanOptions](../reference/apis-arkui/arkui-ts/ts-basic-components-richeditor.md#richeditorbuilderspanoptions11). Each text character is considered a single unit. 555e41f4b71Sopenharmony_ci 556e41f4b71Sopenharmony_ci```ts 557e41f4b71Sopenharmony_ci@Builder 558e41f4b71Sopenharmony_ciTextBuilder() { 559e41f4b71Sopenharmony_ci Row() { 560e41f4b71Sopenharmony_ci Image($r('app.media.startIcon')).width(50).height(50).margin(16) 561e41f4b71Sopenharmony_ci Column() { 562e41f4b71Sopenharmony_ci Text("Text.txt").fontWeight (FontWeight.Bold).fontSize(16) 563e41f4b71Sopenharmony_ci Text("123.45KB").fontColor('#8a8a8a').fontSize(12) 564e41f4b71Sopenharmony_ci }.alignItems(HorizontalAlign.Start) 565e41f4b71Sopenharmony_ci }.backgroundColor('#f4f4f4') 566e41f4b71Sopenharmony_ci .borderRadius("20") 567e41f4b71Sopenharmony_ci .width(220) 568e41f4b71Sopenharmony_ci} 569e41f4b71Sopenharmony_ci 570e41f4b71Sopenharmony_ciButton('addBuilderSpan', { 571e41f4b71Sopenharmony_ci buttonStyle: ButtonStyleMode.NORMAL 572e41f4b71Sopenharmony_ci }) 573e41f4b71Sopenharmony_ci .height(30) 574e41f4b71Sopenharmony_ci .fontSize(13) 575e41f4b71Sopenharmony_ci .onClick(() => { 576e41f4b71Sopenharmony_ci this.my_builder = () => { 577e41f4b71Sopenharmony_ci this.TextBuilder() 578e41f4b71Sopenharmony_ci } 579e41f4b71Sopenharmony_ci this.controller.addBuilderSpan(this.my_builder) 580e41f4b71Sopenharmony_ci }) 581e41f4b71Sopenharmony_ci``` 582e41f4b71Sopenharmony_ci 583e41f4b71Sopenharmony_ci 584e41f4b71Sopenharmony_ci## Adding a Symbol Span 585e41f4b71Sopenharmony_ci 586e41f4b71Sopenharmony_ciUse the [addSymbolSpan](../reference/apis-arkui/arkui-ts/ts-basic-components-richeditor.md#addsymbolspan11) AOU to add a symbol span. 587e41f4b71Sopenharmony_ci 588e41f4b71Sopenharmony_ciIn the API, **value** indicates the content of the symbol span, and **options** indicates options of the symbol span, which are used to set the offset position and style information of the symbol span ([RichEditorSymbolSpanOptions](../reference/apis-arkui/arkui-ts/ts-basic-components-richeditor.md#richeditorsymbolspanoptions11)). 589e41f4b71Sopenharmony_ci 590e41f4b71Sopenharmony_ciIf the cursor in the component is blinking, the cursor position is updated to be after the inserted symbol span. 591e41f4b71Sopenharmony_ci 592e41f4b71Sopenharmony_ciCurrently, gestures, copying, and dragging are not supported for the symbol span. 593e41f4b71Sopenharmony_ci 594e41f4b71Sopenharmony_ci```ts 595e41f4b71Sopenharmony_ciRichEditor(this.options) 596e41f4b71Sopenharmony_ci .onReady(() => { 597e41f4b71Sopenharmony_ci this.controller.addTextSpan('Click the button to add a symbol here.', { 598e41f4b71Sopenharmony_ci style: { 599e41f4b71Sopenharmony_ci fontColor: Color.Black, 600e41f4b71Sopenharmony_ci fontSize: 15 601e41f4b71Sopenharmony_ci } 602e41f4b71Sopenharmony_ci }) 603e41f4b71Sopenharmony_ci }) 604e41f4b71Sopenharmony_ci .width(300) 605e41f4b71Sopenharmony_ci .height(100) 606e41f4b71Sopenharmony_ciButton('addSymbolSpan', { 607e41f4b71Sopenharmony_ci buttonStyle: ButtonStyleMode.NORMAL 608e41f4b71Sopenharmony_ci }) 609e41f4b71Sopenharmony_ci .height(30) 610e41f4b71Sopenharmony_ci .fontSize(13) 611e41f4b71Sopenharmony_ci .onClick(() => { 612e41f4b71Sopenharmony_ci this.controller.addSymbolSpan($r("sys.symbol.basketball_fill"), { 613e41f4b71Sopenharmony_ci style: { 614e41f4b71Sopenharmony_ci fontSize: 30 615e41f4b71Sopenharmony_ci } 616e41f4b71Sopenharmony_ci }) 617e41f4b71Sopenharmony_ci }) 618e41f4b71Sopenharmony_ci``` 619e41f4b71Sopenharmony_ci 620e41f4b71Sopenharmony_ci 621e41f4b71Sopenharmony_ci## Obtaining Span Information 622e41f4b71Sopenharmony_ci 623e41f4b71Sopenharmony_ciUse the [getSpans](../reference/apis-arkui/arkui-ts/ts-basic-components-richeditor.md#getspans) API to obtain the span information of the component. 624e41f4b71Sopenharmony_ci 625e41f4b71Sopenharmony_ciIn the API, **value** indicates the range of the target span. The return value is an array of either [RichEditorTextSpanResult](../reference/apis-arkui/arkui-ts/ts-basic-components-richeditor.md#richeditortextspanresult) or [RichEditorImageSpanResult](../reference/apis-arkui/arkui-ts/ts-basic-components-richeditor.md#richeditorimagespanresult), which includes information about text and image spans. 626e41f4b71Sopenharmony_ci 627e41f4b71Sopenharmony_ci```ts 628e41f4b71Sopenharmony_ciRichEditor(this.options) 629e41f4b71Sopenharmony_ci .onReady(() => { 630e41f4b71Sopenharmony_ci this.controller.addTextSpan('Click the button to obtain the span information.', { 631e41f4b71Sopenharmony_ci style: { 632e41f4b71Sopenharmony_ci fontColor: Color.Black, 633e41f4b71Sopenharmony_ci fontSize: 15 634e41f4b71Sopenharmony_ci } 635e41f4b71Sopenharmony_ci }) 636e41f4b71Sopenharmony_ci }) 637e41f4b71Sopenharmony_ci .width(300) 638e41f4b71Sopenharmony_ci .height(50) 639e41f4b71Sopenharmony_ciText('View the return value of getSpans: ').fontSize (10).fontColor(Color.Gray).width(300) 640e41f4b71Sopenharmony_ciRichEditor(this.options1) 641e41f4b71Sopenharmony_ci .width(300) 642e41f4b71Sopenharmony_ci .height(50) 643e41f4b71Sopenharmony_ciButton('getSpans', { 644e41f4b71Sopenharmony_ci buttonStyle: ButtonStyleMode.NORMAL 645e41f4b71Sopenharmony_ci }) 646e41f4b71Sopenharmony_ci .height(30) 647e41f4b71Sopenharmony_ci .fontSize(13) 648e41f4b71Sopenharmony_ci .onClick(() => { 649e41f4b71Sopenharmony_ci this.controller1.addTextSpan(JSON.stringify(this.controller.getSpans()), { 650e41f4b71Sopenharmony_ci style: { 651e41f4b71Sopenharmony_ci fontColor: Color.Gray, 652e41f4b71Sopenharmony_ci fontSize: 10 653e41f4b71Sopenharmony_ci } 654e41f4b71Sopenharmony_ci }) 655e41f4b71Sopenharmony_ci 656e41f4b71Sopenharmony_ci }) 657e41f4b71Sopenharmony_ci``` 658e41f4b71Sopenharmony_ci 659