如何:向文本应用变换

应用变换可以改变应用程序中文本的显示。 下面的示例使用不同类型的呈现变换来影响 TextBlock 控件中文本的显示。

示例:

下面的示例展示了在二维 x-y 平面中围绕指定点旋转的文本。

使用 RotateTransform 旋转的文本 旋转的文本

下面的代码示例使用 RotateTransform 来旋转文本。 如果 Angle 值为 90,则元素会按顺时针方向旋转 90 度。

<!-- Rotate the text 90 degrees using a RotateTransform. -->
<TextBlock FontFamily="Arial Black" FontSize="64" Foreground="Moccasin" Margin ="80, 10, 0, 0">
  Text Transforms
  <TextBlock.RenderTransform>
    <RotateTransform Angle="90" />
  </TextBlock.RenderTransform>
</TextBlock>

以下示例显示了沿 x 轴缩放 150% 的第二行文本,第三行文本沿 y 轴缩放 150%。

使用 ScaleTransform 缩放的文本 进行缩放

下面的代码示例使用 ScaleTransform 从其原始大小缩放文本。

<!-- Scale the text using a ScaleTransform. -->
<TextBlock
  Name="textblockScaleMaster" 
  FontSize="32"
  Foreground="SteelBlue"
  Text="Scaled Text"
  Margin="100, 0, 0, 0"
  Grid.Column="0" Grid.Row="0">
</TextBlock>
<TextBlock
  FontSize="32"
  FontWeight="Bold" 
  Foreground="SteelBlue"
  Text="{Binding Path=Text, ElementName=textblockScaleMaster}"
  Margin="100, 0, 0, 0"
  Grid.Column="0" Grid.Row="1">
  <TextBlock.RenderTransform>
    <ScaleTransform ScaleX="1.5" ScaleY="1.0" />
  </TextBlock.RenderTransform>
</TextBlock>
<TextBlock
  FontSize="32"
  FontWeight="Bold" 
  Foreground="SteelBlue"
  Text="{Binding Path=Text, ElementName=textblockScaleMaster}"
  Margin="100, 0, 0, 0"
  Grid.Column="0" Grid.Row="2">
  <TextBlock.RenderTransform>
    <ScaleTransform ScaleX="1.0" ScaleY="1.5" />
  </TextBlock.RenderTransform>
</TextBlock>

注释

缩放文本与增加文本的字号不同。 字体大小是相互独立计算的,以便以不同的大小提供最佳分辨率。 而缩放后的文本将按比例保持原始的文本大小。

以下示例显示沿 x 轴倾斜的文本。

使用 SkewTransform 扭曲的文本

下面的代码示例使用 SkewTransform 来倾斜文本。 倾斜(也称为剪切)是一种以非统一方式拉伸坐标空间的转换。 在此示例中,两个文本字符串沿 x 坐标倾斜 -30° 和 30° 。

<!-- Skew the text using a SkewTransform. -->
<TextBlock
  Name="textblockSkewMaster" 
  FontSize="32"
  FontWeight="Bold" 
  Foreground="Maroon"
  Text="Skewed Text"
  Margin="125, 0, 0, 0"
  Grid.Column="0" Grid.Row="0">
  <TextBlock.RenderTransform>
    <SkewTransform AngleX="-30" AngleY="0" />
  </TextBlock.RenderTransform>
</TextBlock>
<TextBlock
  FontSize="32"
  FontWeight="Bold" 
  Foreground="Maroon"
  Text="{Binding Path=Text, ElementName=textblockSkewMaster}"
  Margin="100, 0, 0, 0"
  Grid.Column="0" Grid.Row="1">
  <TextBlock.RenderTransform>
    <SkewTransform AngleX="30" AngleY="0" />
  </TextBlock.RenderTransform>
</TextBlock>

以下示例显示沿 x 轴和 y 轴翻译或移动的文本。

使用 TranslateTransform 偏移的文本

下面的代码示例使用 TranslateTransform 来偏移文本。 在本示例中,主要文本下方略微偏移的文本副本营造出了阴影效果。

<!-- Skew the text using a TranslateTransform. -->
<TextBlock
  FontSize="32"
  FontWeight="Bold" 
  Foreground="Black"
  Text="{Binding Path=Text, ElementName=textblockTranslateMaster}"
  Margin="100, 0, 0, 0"
  Grid.Column="0" Grid.Row="0">
  <TextBlock.RenderTransform>
    <TranslateTransform X="2" Y="2" />
  </TextBlock.RenderTransform>
</TextBlock>
<TextBlock
  Name="textblockTranslateMaster" 
  FontSize="32"
  FontWeight="Bold" 
  Foreground="Coral"
  Text="Translated Text"
  Margin="100, 0, 0, 0"
  Grid.Column="0" Grid.Row="0"/>

注释

DropShadowBitmapEffect 提供了一组丰富的功能,用于提供阴影效果。 有关详细信息,请参阅 创建带阴影的文本

另请参阅