我们知道,文字的修饰包括:空心字、立体字、划线字、阴影字、加粗、倾斜等。这里只说划线字的修饰方式,按划线的位置,我们可将之分为:上划线、中划线、基线与下划线。如图:
从上至下,分别为上划线(Overline),中划线(StrikeThrough),基线(Baseline)和下划线(Underline)。
如何实现?
(1)XAML代码:
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" > <TextBlock TextDecorations="Strikethrough" FontSize="72" FontFamily="Arial">A</TextBlock> </Page>
这里TextDecorations属性可以设置为:OverLine, Strikethrough, Baseline, Underline或None,如果没有设置TextDecorations属性,则默认为None,即不带划线修饰。
(2)使用C#代码:
private void SetDefaultStrikethrough() { textBlock1.TextDecorations = TextDecorations.Strikethrough; }
(为了简洁,这里只列出相关的关键代码,其他代码未用C#列出。textBlock1为TextBlock的名称,在XAML中使用 x:Name="textBlock1"形式标记)
如果要更复杂点的效果,比如需要设置划线的颜色、线粗等,如下图:
如何制作类似效果呢?
方法是:设置TextBlock的TextDecorations属性,再对TextDecoration的Pen属性进行设置。
如下XAML代码:
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" > <Canvas> <TextBlock FontSize="84" FontFamily="Arial Black" Margin="0,0"> <TextBlock.TextDecorations> <TextDecoration PenOffset="10" PenOffsetUnit="Pixel" PenThicknessUnit="Pixel"> <TextDecoration.Pen> <Pen Thickness="5"> <Pen.Brush> <LinearGradientBrush Opacity="0.8" StartPoint="0,0.5" EndPoint="1,0.5"> <LinearGradientBrush.GradientStops> <GradientStop Color="Yellow" Offset="0" /> <GradientStop Color="Red" Offset="1" /> </LinearGradientBrush.GradientStops> </LinearGradientBrush> </Pen.Brush> <Pen.DashStyle> <DashStyle Dashes="1,2,3"/> </Pen.DashStyle> </Pen> </TextDecoration.Pen> </TextDecoration> </TextBlock.TextDecorations> GOOD </TextBlock> </Canvas> </Page>
C#关键代码:
private void SetLinearGradientUnderline() { TextDecoration myUnderline = new TextDecoration(); Pen myPen = new Pen(); myPen.Brush = new LinearGradientBrush(Colors.Yellow, Colors.Red, new Point(0, 0.5), new Point(1, 0.5)); myPen.Brush.Opacity = 0.8; myPen.Thickness = 5; myPen.DashStyle = DashStyles.Dash; myUnderline.Pen = myPen; myUnderline.PenThicknessUnit = TextDecorationUnit.FontRecommended; TextDecorationCollection myCollection = new TextDecorationCollection(); myCollection.Add(myUnderline); textBlockGood.TextDecorations = myCollection; }