WPF RichTextBox: how to change the selected text font?

how to change font for current selected text area inside WPF RichTextBox?

+3
source share
5 answers

solvable ...

if (this.TextEditor.Selection.IsEmpty)
    this.TextEditor.CurrentFontFamily = SelectedFont;
else
    this.TextEditor.Selection.ApplyPropertyValue(TextElement.FontFamilyProperty, SelectedFont);
+4
source

I have implemented a toolbar that can change the font size, family, color, etc. I found that details can be complex with wpf richtextbox. Setting the selection font makes sense, but there are also default font properties for the text box and current carriage properties that you can deal with. Here is what I wrote to make it work in most cases with font size. The process should be the same for fontfamily and fontcolor. Hope this helps.

    public static void SetFontSize(RichTextBox target, double value)
    {
        // Make sure we have a richtextbox.
        if (target == null)
            return;

        // Make sure we have a selection. Should have one even if there is no text selected.
        if (target.Selection != null)
        {
            // Check whether there is text selected or just sitting at cursor
            if (target.Selection.IsEmpty)
            {
                // Check to see if we are at the start of the textbox and nothing has been added yet
                if (target.Selection.Start.Paragraph == null)
                {
                    // Add a new paragraph object to the richtextbox with the fontsize
                    Paragraph p = new Paragraph();
                    p.FontSize = value;
                    target.Document.Blocks.Add(p);
                }
                else
                {
                    // Get current position of cursor
                    TextPointer curCaret = target.CaretPosition;
                    // Get the current block object that the cursor is in
                    Block curBlock = target.Document.Blocks.Where
                        (x => x.ContentStart.CompareTo(curCaret) == -1 && x.ContentEnd.CompareTo(curCaret) == 1).FirstOrDefault();
                    if (curBlock != null)
                    {
                        Paragraph curParagraph = curBlock as Paragraph;
                        // Create a new run object with the fontsize, and add it to the current block
                        Run newRun = new Run();
                        newRun.FontSize = value;
                        curParagraph.Inlines.Add(newRun);
                        // Reset the cursor into the new block. 
                        // If we don't do this, the font size will default again when you start typing.
                        target.CaretPosition = newRun.ElementStart;
                    }
                }
            }
            else // There is selected text, so change the fontsize of the selection
            {
                TextRange selectionTextRange = new TextRange(target.Selection.Start, target.Selection.End);
                selectionTextRange.ApplyPropertyValue(TextElement.FontSizeProperty, value);
            }
        }
        // Reset the focus onto the richtextbox after selecting the font in a toolbar etc
        target.Focus();
    }
+9

- :

 TextSelection text = richTextBox.Selection; 
 if (!text.IsEmpty) 
 { 
     text.ApplyPropertyValue(RichTextBox.FontSizeProperty, value); 
 }
+6

:

Dim rng As TextRange = New TextRange (YourRtfBox.Selection.Start, YourRtfBox.Selection.End)

:

rng.ApplyPropertyValue(Inline.FontSizeProperty, YourFontSizeValue) rng.ApplyPropertyValue(Inline.FontFamilyProperty, YourFontFamilyValue)

+3

RichTextBox, :

text.ApplyPropertyValue(Run.FontFamilyProperty, value);

RichTextBox Run, Run Dependency. , Silverlight, WPF .

+2

Source: https://habr.com/ru/post/1759951/