locked
Adding different colors and Sizes to a RichEditBox

    Question

  • I have been trying to add different colors on text of a Rich Edit Box on a Windows store app Windows runtime. My app Edits Rich Text Documents. I was able to Changing font settings by selecting Text but if I change the Foreground color of the Edit Rich box All Text color changes to that color. Note it's  not a RichTextBox it's a RichEditBox.  Thank you!!

    Rogelio Rios

    Monday, December 01, 2014 5:55 PM

Answers

  • You'll use the text object model to change the properties on different text ranges. You can get the ITextDocument from the RichEditBox.Document property and then get text ranges and set the CharacterFormat for the range.

    Take a look at the XAML text editing sample 

    Monday, December 01, 2014 7:09 PM
    Owner
  • By using the Selection.StartPosition and Selection.EndPosition properties:

    public MainPage()
            {
                this.InitializeComponent();
    
        
                rtb.Document.SetText(Windows.UI.Text.TextSetOptions.None, "Here is a text");
                ITextDocument doc = rtb.Document;
                ITextSelection selection = doc.Selection;
                //set some selection for testing purposes only...
                selection.StartPosition = 5;
                selection.EndPosition = 6;
    
    
                
            }
    
            void MainPage_Loaded(object sender, RoutedEventArgs e)
            {
                ITextDocument doc = rtb.Document;
                ITextSelection selection = doc.Selection;
                ITextRange range = doc.GetRange(selection.StartPosition, selection.EndPosition);
                range.CharacterFormat.ForegroundColor = Windows.UI.Colors.Red;
                range.CharacterFormat.FontStyle = Windows.UI.Text.FontStyle.Italic;
                rtb.Document.ApplyDisplayUpdates();
    
    
            }
    
    <RichEditBox x:Name="rtb">
            </RichEditBox>
    

    Please remember to mark helpful posts as answer and/or helpful.

    Monday, December 01, 2014 10:27 PM

All replies

  • You'll use the text object model to change the properties on different text ranges. You can get the ITextDocument from the RichEditBox.Document property and then get text ranges and set the CharacterFormat for the range.

    Take a look at the XAML text editing sample 

    Monday, December 01, 2014 7:09 PM
    Owner
  • How do I get the text range of where the curser is at?

    Rogelio Rios

    Monday, December 01, 2014 9:51 PM
  • By using the Selection.StartPosition and Selection.EndPosition properties:

    public MainPage()
            {
                this.InitializeComponent();
    
        
                rtb.Document.SetText(Windows.UI.Text.TextSetOptions.None, "Here is a text");
                ITextDocument doc = rtb.Document;
                ITextSelection selection = doc.Selection;
                //set some selection for testing purposes only...
                selection.StartPosition = 5;
                selection.EndPosition = 6;
    
    
                
            }
    
            void MainPage_Loaded(object sender, RoutedEventArgs e)
            {
                ITextDocument doc = rtb.Document;
                ITextSelection selection = doc.Selection;
                ITextRange range = doc.GetRange(selection.StartPosition, selection.EndPosition);
                range.CharacterFormat.ForegroundColor = Windows.UI.Colors.Red;
                range.CharacterFormat.FontStyle = Windows.UI.Text.FontStyle.Italic;
                rtb.Document.ApplyDisplayUpdates();
    
    
            }
    
    <RichEditBox x:Name="rtb">
            </RichEditBox>
    

    Please remember to mark helpful posts as answer and/or helpful.

    Monday, December 01, 2014 10:27 PM