|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: Basic Features in .NET MAUI Rich Text Editor | Syncfusion® |
| 4 | +description: Learn about the basic features of the .NET MAUI Rich Text Editor (SfRichTextEditor) such as handling content, events, and other core functionalities. |
| 5 | +platform: maui |
| 6 | +control: Rich Text Editor |
| 7 | +documentation: ug |
| 8 | +--- |
| 9 | + |
| 10 | +# Basic Features in .NET MAUI Rich Text Editor (SfRichTextEditor) |
| 11 | + |
| 12 | +This section covers the essential properties, methods, and events of the .NET MAUI `SfRichTextEditor` for handling content and user interactions. |
| 13 | + |
| 14 | +## Handling Content |
| 15 | + |
| 16 | +### Setting and Getting HTML Content |
| 17 | + |
| 18 | +The rich content of the editor, including all formatting, is accessible through the `Text` property. You can use this property to both load and retrieve the content as an HTML string. |
| 19 | + |
| 20 | +{% tabs %} |
| 21 | + |
| 22 | +{% highlight xaml %} |
| 23 | + |
| 24 | +<rte:SfRichTextEditor Text="The Syncfusion .NET MAUI Rich Text Editor is a WYSIWYG editor for creating and editing rich text content." /> |
| 25 | + |
| 26 | +{% endhighlight %} |
| 27 | + |
| 28 | +{% highlight c# %} |
| 29 | + |
| 30 | +SfRichTextEditor richTextEditor = new SfRichTextEditor(); |
| 31 | +richTextEditor.Text = "The Syncfusion .NET MAUI Rich Text Editor is a WYSIWYG editor for creating and editing rich text content."; |
| 32 | +this.Content = richTextEditor; |
| 33 | + |
| 34 | +{% endhighlight %} |
| 35 | + |
| 36 | +{% endtabs %} |
| 37 | + |
| 38 | +### Getting HTML Content Asynchronously |
| 39 | + |
| 40 | +To ensure you get the most up-to-date content, especially after recent edits, you can retrieve the HTML string asynchronously using the `GetHtmlString` method. |
| 41 | + |
| 42 | +{% tabs %} |
| 43 | + |
| 44 | +{% highlight c# %} |
| 45 | + |
| 46 | +string htmlContent = await rte.GetHtmlText(); |
| 47 | + |
| 48 | +{% endhighlight %} |
| 49 | + |
| 50 | +{% endtabs %} |
| 51 | + |
| 52 | + |
| 53 | +### Getting Selected HTML |
| 54 | + |
| 55 | +To retrieve the HTML representation of the currently selected content, use the `GetSelectedText` method. |
| 56 | + |
| 57 | +{% tabs %} |
| 58 | + |
| 59 | +{% highlight c# %} |
| 60 | + |
| 61 | +string selectedText = await rte.GetSelectedText(); |
| 62 | + |
| 63 | +{% endhighlight %} |
| 64 | + |
| 65 | +{% endtabs %} |
| 66 | + |
| 67 | +## Placeholder |
| 68 | + |
| 69 | +The editor can display a placeholder text when the content is empty. This is useful for prompting the user. The placeholder is cleared as soon as the user starts typing. |
| 70 | + |
| 71 | +{% tabs %} |
| 72 | + |
| 73 | +{% highlight xaml %} |
| 74 | + |
| 75 | +<rte:SfRichTextEditor Placeholder="Enter your content here..." /> |
| 76 | + |
| 77 | +{% endhighlight %} |
| 78 | + |
| 79 | +{% highlight c# %} |
| 80 | + |
| 81 | +SfRichTextEditor richTextEditor = new SfRichTextEditor(); |
| 82 | +richTextEditor.Placeholder = "Enter your content here..."; |
| 83 | + |
| 84 | +{% endhighlight %} |
| 85 | + |
| 86 | +{% endtabs %} |
| 87 | + |
| 88 | + |
| 89 | + |
| 90 | + |
| 91 | +## Events |
| 92 | + |
| 93 | +### TextChanged Event |
| 94 | + |
| 95 | +The `TextChanged` event is fired whenever the content in the editor is changed. The event arguments provide the old and new HTML content. |
| 96 | + |
| 97 | +{% tabs %} |
| 98 | + |
| 99 | +{% highlight xaml %} |
| 100 | + |
| 101 | +<rte:SfRichTextEditor TextChanged="OnTextChanged" /> |
| 102 | + |
| 103 | +{% endhighlight %} |
| 104 | + |
| 105 | +{% highlight c# %} |
| 106 | + |
| 107 | +private void OnTextChanged(object sender, RichTextEditorTextChangedEventArgs e) |
| 108 | +{ |
| 109 | + string oldHtml = e.OldValue; |
| 110 | + string newHtml = e.NewValue; |
| 111 | + // Logic to execute when the Text changes. |
| 112 | +} |
| 113 | + |
| 114 | +{% endhighlight %} |
| 115 | + |
| 116 | +{% endtabs %} |
| 117 | + |
| 118 | +### FormatChanged Event |
| 119 | + |
| 120 | +The `FormatChanged` event is Occurs when the formatting status changes. This is useful for implementing contextual formatting options. |
| 121 | + |
| 122 | +{% tabs %} |
| 123 | + |
| 124 | +{% highlight xaml %} |
| 125 | + |
| 126 | +<rte:SfRichTextEditor FormatChanged="OnFormatChanged" /> |
| 127 | + |
| 128 | +{% endhighlight %} |
| 129 | + |
| 130 | +{% highlight c# %} |
| 131 | + |
| 132 | +private void OnFormatChanged(object sender, RichTextEditorFormatChangedEventArgs e) |
| 133 | +{ |
| 134 | + // Logic to execute when the Format changes. |
| 135 | +} |
| 136 | + |
| 137 | +{% endhighlight %} |
| 138 | + |
| 139 | +{% endtabs %} |
| 140 | + |
| 141 | +### HyperlinkClicked Event |
| 142 | + |
| 143 | +The `HyperlinkClicked` event is fired when a user taps on a hyperlink within the content. The event arguments contain the URL and the text of the Clicked link. |
| 144 | + |
| 145 | +{% tabs %} |
| 146 | + |
| 147 | +{% highlight xaml %} |
| 148 | + |
| 149 | +<rte:SfRichTextEditor HyperlinkClicked="OnHyperlinkClicked"/> |
| 150 | + |
| 151 | +{% endhighlight %} |
| 152 | + |
| 153 | +{% highlight c# %} |
| 154 | + |
| 155 | +SfRichTextEditor richTextEditor = new SfRichTextEditor(); |
| 156 | +richTextEditor.Text = "<p>Visit the <a href='https://www.syncfusion.com'>Syncfusion</a> website.</p>"; |
| 157 | +richTextEditorHyperlinkClicked += OnHyperlinkClicked |
| 158 | + |
| 159 | + |
| 160 | +private void OnHyperlinkClicked(object sender, RichTextEditorHyperlinkClickedEventArgs e) |
| 161 | +{ |
| 162 | + string url = e.URL; |
| 163 | + string text = e.DisplayText; |
| 164 | + // You can handle the navigation here, for example: |
| 165 | +} |
| 166 | + |
| 167 | +{% endhighlight %} |
| 168 | + |
| 169 | +{% endtabs %} |
| 170 | + |
| 171 | +## Focus Management |
| 172 | + |
| 173 | +You can programmatically set or remove focus from the editor control using the `Focus()` and `Unfocus()` methods. |
| 174 | + |
| 175 | +{% tabs %} |
| 176 | + |
| 177 | +{% highlight C# %} |
| 178 | + |
| 179 | +// To set focus on the Rich Text Editor |
| 180 | +richTextEditor.Focus(); |
| 181 | + |
| 182 | +// To remove focus from the Rich Text Editor |
| 183 | +richTextEditor.Unfocus(); |
| 184 | + |
| 185 | +{% endhighlight %} |
| 186 | +{% endtabs %} |
0 commit comments