zoukankan      html  css  js  c++  java
  • WPF中的文字修饰

    我们知道,文字的修饰包括:空心字、立体字、划线字、阴影字、加粗、倾斜等。这里只说划线字的修饰方式,按划线的位置,我们可将之分为:上划线、中划线、基线与下划线。如图:


    从上至下,分别为上划线(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;
    }

     

  • 相关阅读:
    Android 2.2 r1 API 中文文档系列(11) —— RadioButton
    Android API 中文 (15) —— GridView
    Android 中文 API (16) —— AnalogClock
    Android2.2 API 中文文档系列(7) —— ImageButton
    Android2.2 API 中文文档系列(6) —— ImageView
    Android 2.2 r1 API 中文文档系列(12) —— Button
    Android2.2 API 中文文档系列(8) —— QuickContactBadge
    [Android1.5]TextView跑马灯效果
    [Android1.5]ActivityManager: [1] Killed am start n
    Android API 中文(14) —— ViewStub
  • 原文地址:https://www.cnblogs.com/dinotang/p/3272309.html
Copyright © 2011-2022 走看看