This is a migrated thread and some comments may be shown as answers.

Insert text with specified font

8 Answers 62 Views
RichTextEditor
This is a migrated thread and some comments may be shown as answers.
Mihajlo
Top achievements
Rank 1
Mihajlo asked on 13 Sep 2018, 04:06 PM

What is the proper way to insert text with specified font in any place in measured document? I have tried with the following code but it does not set the font if just right of the caret is space, and it changes the font of the whole word if the caret is in the middle of the word.

var editor = new RadDocumentEditor(radRichTextEditor1.Document);
editor.ChangeFontFamily(new Telerik.WinControls.RichTextEditor.UI.FontFamily("Times New Roman"));
editor.Insert("ABC");

 

 

8 Answers, 1 is accepted

Sort by
0
Mihajlo
Top achievements
Rank 1
answered on 14 Sep 2018, 03:02 PM

To answer my own question, selection here is king, as always. The follwing works if selection does not exist. If one ore more selections exist one must decide what the code should do about it. The easiest way is to clear the selection before this operation.

 

var editor = new RadDocumentEditor(radRichTextEditor1.Document);
using (DocumentPosition originalCaretPosition = new DocumentPosition(editor.Document.CaretPosition, true))
{
    editor.Insert("ABC");
 
    editor.Document.Selection.SetSelectionStart(originalCaretPosition);
    editor.Document.Selection.AddSelectionEnd(editor.Document.CaretPosition);
 
    editor.ChangeFontFamily(new Telerik.WinControls.RichTextEditor.UI.FontFamily("Times New Roman"));
}

 

0
Accepted
Tanya
Telerik team
answered on 17 Sep 2018, 06:18 PM
Hi Mihajlo,

When inserting content directly with RadDocumentEditor, it uses the current style of the span the caret is on. To ensure that the styling you have applied is used, I would suggest you to construct a Span object and apply the desired styling to it and add it to the document using the InsertInline() method of the editor. Here is how this would look like in code:

Span span = new Span("test");
span.FontFamily = new FontFamily("Times New Roman");
this.radRichTextEditor.InsertInline(span);

Hope this is helpful.

Regards,
Tanya
Progress Telerik
Get quickly and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Mihajlo
Top achievements
Rank 1
answered on 31 May 2019, 03:52 PM

To insert text with various font styles set we can do the following:

editor.InsertInline(new Span("regular"));
editor.InsertInline(new Span("bold") { FontWeight = FontWeights.Bold });
editor.InsertInline(new Span("italic") { FontStyle = System.Drawing.FontStyle.Italic });
editor.InsertInline(new Span("underline") { UnderlineDecoration = UnderlineTypes.Line });
editor.InsertInline(new Span("strikeout") { Strikethrough = true });

 

But how to insert an empty paragraph with desired font size (paragraph height), for instance between words "bold" and "italic" above, where there are no spaces, so calling ChangeFontSize method before InsertParagraph would not work properly?

0
Tanya
Telerik team
answered on 04 Jun 2019, 12:20 PM
Hi Mihajlo,

The methods of the editor work on the selection or if it is missing, on the element under the caret position. Here is how I modified the code so you can achieve the requirement:
editor.InsertInline(new Span("regular"));
editor.InsertInline(new Span("bold") { FontWeight = FontWeights.Bold });
editor.InsertInline(new Span("italic") { FontStyle = System.Drawing.FontStyle.Italic });
// Add the new paragraph
editor.InsertParagraph();
// Change its properties
editor.ChangeParagraphLineSpacing(Unit.PointToDip(25), LineSpacingType.Exact);
// Add another paragraph for the following content
editor.InsertParagraph();
// Change its properties
editor.ChangeParagraphLineSpacing(1.15, LineSpacingType.Auto);
editor.InsertInline(new Span("underline") { UnderlineDecoration = UnderlineTypes.Line });
editor.InsertInline(new Span("strikeout") { Strikethrough = true });

Hope this will help you achieve the desired result.

Regards,
Tanya
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Mihajlo
Top achievements
Rank 1
answered on 04 Jun 2019, 02:30 PM

I know only the font and its size. I don't know how to convert from those to the values that are 25 and 1.15 in your example. Paragraph height should naturally fit one character of my font and size.

Maybe I could use a trick where I insert one character, move back one place, then insert all the text and paragraphs I need, and then delete that character after the caret. Was only wondering if there is a way without using trickery.

0
Tanya
Telerik team
answered on 05 Jun 2019, 01:35 PM
Hello Mihajlo,

You can maintain the line height as big as the font size requires it to be using the Single line spacing. You can set it as follows:
editor.ChangeParagraphLineSpacing(1, LineSpacingType.Auto);

The 25 and 1.15 values set different types of line spacing as it can be seen from the second parameter passed to the ChangeParagraphLineSpacing() method. The Auto line spacing type determines different values which are dependent of the font settings. If you would like to set a specific line height values and keep it always the same no matter of the content and its settings, you can use the Exact line spacing type whose measurement unit is in points.

Hope this makes things clear.

Regards,
Tanya
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Mihajlo
Top achievements
Rank 1
answered on 07 Jun 2019, 03:28 PM

I think there is an error in the way the editor handles empty paragraphs. Let's take for instance demo application with its default document, with text "RadRichTextEditor for WinForms" at the beginning in font Calibri of size 28. Suppose the user wants to add A, then empty line, then C in single line, followed by the rest of the document, and to have these three new lines in Times New Roman in size 10.

If there is no empty line, but letter B in middle line, then user can set caret to document start, set the font and size, and type A<Return>B<Return>C<Return>. And it would look fine (and even then, if we set caret just after B and press Return the new paragraph will have spacing after set to 7.5 for no apparent reason).

If we want an empty line instead of B in the line, then there is no simple way to do it. Typing A<Return><Return>C<Return> would produce a huge gap between A and C (we still first set the caret, and font and size). One way to accomplish what we want is to select all new text, set font size to something other than 10, and then back to 10 (which is also weird to have to do).

Turning this into code is even harder. How would you insert A<Return><Return>C<Return> from code in Times New Roman size 10 at the demo's default document's beginning?

0
Tanya
Telerik team
answered on 12 Jun 2019, 02:02 PM
Hi Mihajlo,

Thank you for providing the additional steps.

Indeed, we managed to reproduce the issue in the UI and logged it in our backlog on your behalf: RadRichTextEditor: font is reset after inserting two empty lines by pressing Enter twice. I added Telerik points in appreciation for this report.

To achieve this behavior in code, you can apply the properties after each paragraph insertion.

Regards,
Tanya
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
RichTextEditor
Asked by
Mihajlo
Top achievements
Rank 1
Answers by
Mihajlo
Top achievements
Rank 1
Tanya
Telerik team
Share this question
or