Change color and font for some part of text in WPF C#

后端 未结 4 1113
孤城傲影
孤城傲影 2020-11-27 18:25

Is there a way to change color and font for some part of text which I want to put on TextBox or RichTextBox. I am using C# WPF.

For example

 richText         


        
4条回答
  •  猫巷女王i
    2020-11-27 18:58

    I have made my own class to manipulate Texts of TextBlock, TextBox...

    /// 
    /// Class for text manipulation operations
    /// 
    public class TextManipulation
    {
        /// 
        /// Is manipulating a specific string inside of a TextPointer Range (TextBlock, TextBox...)
        /// 
        /// Starting point where to look
        /// Endpoint where to look
        /// This is the string you want to manipulate
        /// The new FontStyle
        /// The new FontWeight
        /// The new foreground
        /// The new background
        /// The new FontSize
        public static void FromTextPointer(TextPointer startPointer, TextPointer endPointer, string keyword, FontStyle fontStyle, FontWeight fontWeight, Brush foreground, Brush background, double fontSize)
        {
            FromTextPointer(startPointer, endPointer, keyword, fontStyle, fontWeight, foreground, background, fontSize, null);
        }
    
        /// 
        /// Is manipulating a specific string inside of a TextPointer Range (TextBlock, TextBox...)
        /// 
        /// Starting point where to look
        /// Endpoint where to look
        /// This is the string you want to manipulate
        /// The new FontStyle
        /// The new FontWeight
        /// The new foreground
        /// The new background
        /// The new FontSize
        /// The New String (if you want to replace, can be null)
        public static void FromTextPointer(TextPointer startPointer, TextPointer endPointer, string keyword, FontStyle fontStyle, FontWeight fontWeight, Brush foreground, Brush background, double fontSize, string newString)
        {
            if(startPointer == null)throw new ArgumentNullException(nameof(startPointer));
            if(endPointer == null)throw new ArgumentNullException(nameof(endPointer));
            if(string.IsNullOrEmpty(keyword))throw new ArgumentNullException(keyword);
    
            TextRange text = new TextRange(startPointer, endPointer);
            TextPointer current = text.Start.GetInsertionPosition(LogicalDirection.Forward);
            while (current != null)
            {
                string textInRun = current.GetTextInRun(LogicalDirection.Forward);
                if (!string.IsNullOrWhiteSpace(textInRun))
                {
                    int index = textInRun.IndexOf(keyword);
                    if (index != -1)
                    {
                        TextPointer selectionStart = current.GetPositionAtOffset(index,LogicalDirection.Forward);
                        TextPointer selectionEnd = selectionStart.GetPositionAtOffset(keyword.Length,LogicalDirection.Forward);
                        TextRange selection = new TextRange(selectionStart, selectionEnd);
    
                        if(!string.IsNullOrEmpty(newString))
                            selection.Text = newString;
    
                        selection.ApplyPropertyValue(TextElement.FontSizeProperty, fontSize);
                        selection.ApplyPropertyValue(TextElement.FontStyleProperty, fontStyle);
                        selection.ApplyPropertyValue(TextElement.FontWeightProperty, fontWeight);
                        selection.ApplyPropertyValue(TextElement.ForegroundProperty, foreground);
                        selection.ApplyPropertyValue(TextElement.BackgroundProperty, background);
                    }
                }
                current = current.GetNextContextPosition(LogicalDirection.Forward);
            }
        }
    }
    

    Usage

    TextManipulation.FromTextPointer(_TextBlock.ContentStart, _TextBlock.ContentEnd, "IWantToBeManipulated", NewFontStyle, NewFontWeight, NewForeground, NewBackground, NewFontSize);
    TextManipulation.FromTextPointer(_TextBlock.ContentStart, _TextBlock.ContentEnd, "IWantToBeManipulated", NewFontStyle, NewFontWeight, NewForeground, NewBackground, NewFontSize, "NewStringIfYouWant");
    

提交回复
热议问题