Wednesday, May 18, 2011

Text Improvements in WPF 4.0

Microsoft introduced new Text Rendering Stack for better Text Display in WPF 4.0. You can change Selection color and Cursor color for Text Input controls. This features are simple but very useful.

SelectionBrush and CaretBrush

You can apply custom color for Selected Text using SelectionBrush property and Cursor color using CaretBrush property for Text Input controls like TextBox and RichTextBox.

<TextBox Name="MyText" Text="This is simple selection."
            CaretBrush="Red" CaretIndex="5"
            SelectionStart="5" SelectionLength="9"
            FontSize="25" >
            <GradientStop Color="Blue" Offset="0" />
            <GradientStop Color="Cyan" Offset="1" />


In first output you can see selected text in TextBox filled with LinearGardient brush and in second output you can see Cursor with Red color. 

Text Rendering

Microsoft completely replace old Text Rendering Stack and come up with new and improved one in WPF 4. It supports four types of text rendering modes. You can specify rendering mode using TextRenderingMode property.

    <StackPanel TextOptions.TextFormattingMode="Display">
        <TextBlock Margin="3">This is Clear Text</TextBlock>
        <TextBlock TextOptions.TextRenderingMode="Aliased"
                   Margin="3">This is Aliased Mode.</TextBlock>
        <TextBlock TextOptions.TextRenderingMode="Grayscale"
                   Margin="3">This is Grayscale Mode.</TextBlock>
        <TextBlock TextOptions.TextRenderingMode="ClearType"
                   Margin="3">This is ClearType Mode.</TextBlock>
        <TextBlock TextOptions.TextRenderingMode="Auto"
                   Margin="3">This is Auto Mode.</TextBlock>


No comments:

Post a Comment