WPF RichTextBox: How to change selected text font?

前端 未结 5 1657
暗喜
暗喜 2020-12-18 09:46

how can I change the font for a currently selected text area inside a WPF RichTextBox?

相关标签:
5条回答
  • 2020-12-18 09:55

    To get the current selection use:

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

    And then set the fontstyle:

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

    0 讨论(0)
  • 2020-12-18 09:57

    How about something like:

     TextSelection text = richTextBox.Selection; 
     if (!text.IsEmpty) 
     { 
         text.ApplyPropertyValue(RichTextBox.FontSizeProperty, value); 
     }
    
    0 讨论(0)
  • 2020-12-18 09:59

    Solved...

    if (this.TextEditor.Selection.IsEmpty)
        this.TextEditor.CurrentFontFamily = SelectedFont;
    else
        this.TextEditor.Selection.ApplyPropertyValue(TextElement.FontFamilyProperty, SelectedFont);
    
    0 讨论(0)
  • 2020-12-18 10:00

    To change the font family for a selection in the RichTextBox you should use this:

    text.ApplyPropertyValue(Run.FontFamilyProperty, value);
    

    The selected text in a RichTextBox is a Run object, so one must use the Run Dependency Properties. This seems to work in Silverlight at least, so should be the same thing in WPF.

    0 讨论(0)
  • 2020-12-18 10:15

    I've implemented a toolbar that can change the font size, family, color, etc. What i found is the details can be tricky with the wpf richtextbox. Setting the selection font makes some sense, but, there are also the default font properties of the text box, and the current caret properties to contend with. Here is what i've written to get it to work for most cases with the font size. The process should be the same for fontfamily and fontcolor. Hope it 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();
        }
    
    0 讨论(0)
提交回复
热议问题