本主题概述 FormattedText 对象的功能。 此对象提供用于在 Windows Presentation Foundation (WPF) 应用程序中绘制文本的低级别控件。
技术概述
使用 FormattedText 对象可以绘制多行文本,其中文本中的每个字符都可以单独设置格式。 以下示例显示应用了多种格式的文本。
注释
对于从 Win32 API 迁移的开发人员,Win32 迁移一节中的表列出了 Win32 DrawText 标志和 Windows Presentation Foundation (WPF) 中的近似等效项。
使用格式化文本的原因
WPF 包含多个控件,用于将文本绘制到屏幕。 每个控件都针对不同的方案,并有自己的功能和限制列表。 通常,当需要有限的文本支持时,应使用 TextBlock 元素,例如用户界面(UI)中的简短句子。 当需要最少的文本支持时,可以使用 Label。 有关详细信息,请参阅 WPF中的
FormattedText 对象提供的文本格式设置功能比 Windows Presentation Foundation (WPF) 文本控件更大的功能,并且当你想要将文本用作装饰元素时,它非常有用。 有关详细信息,请参阅以下部分 将格式化文本转换为几何图形。
此外,FormattedText 对象可用于创建面向文本的 DrawingVisual派生对象。 DrawingVisual 是用于呈现形状、图像或文本的轻型绘图类。 有关详细信息,请参阅使用 DrawingVisuals 执行测试示例。
使用 FormattedText 对象
若要创建格式化文本,请调用 FormattedText 构造函数来创建 FormattedText 对象。 创建初始格式化文本字符串后,可以应用一系列格式样式。
使用 MaxTextWidth 属性将文本限制为特定宽度。 文本将自动换行以避免超过指定的宽度。 使用 MaxTextHeight 属性将文本限制为特定高度。 文本将显示省略号“...”对于超过指定高度的文本。
可以将多个格式样式应用于一个或多个字符。 例如,可以同时调用 SetFontSize 和 SetForegroundBrush 方法来更改文本中前五个字符的格式。
下面的代码示例创建一个 FormattedText 对象,然后将多个格式样式应用于文本。
protected override void OnRender(DrawingContext drawingContext)
{
string testString = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor";
// Create the initial formatted text string.
FormattedText formattedText = new FormattedText(
testString,
CultureInfo.GetCultureInfo("en-us"),
FlowDirection.LeftToRight,
new Typeface("Verdana"),
32,
Brushes.Black);
// Set a maximum width and height. If the text overflows these values, an ellipsis "..." appears.
formattedText.MaxTextWidth = 300;
formattedText.MaxTextHeight = 240;
// Use a larger font size beginning at the first (zero-based) character and continuing for 5 characters.
// The font size is calculated in terms of points -- not as device-independent pixels.
formattedText.SetFontSize(36 * (96.0 / 72.0), 0, 5);
// Use a Bold font weight beginning at the 6th character and continuing for 11 characters.
formattedText.SetFontWeight(FontWeights.Bold, 6, 11);
// Use a linear gradient brush beginning at the 6th character and continuing for 11 characters.
formattedText.SetForegroundBrush(
new LinearGradientBrush(
Colors.Orange,
Colors.Teal,
90.0),
6, 11);
// Use an Italic font style beginning at the 28th character and continuing for 28 characters.
formattedText.SetFontStyle(FontStyles.Italic, 28, 28);
// Draw the formatted text string to the DrawingContext of the control.
drawingContext.DrawText(formattedText, new Point(10, 0));
}
Protected Overrides Sub OnRender(ByVal drawingContext As DrawingContext)
Dim testString As String = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor"
' Create the initial formatted text string.
Dim formattedText As New FormattedText(testString, CultureInfo.GetCultureInfo("en-us"), FlowDirection.LeftToRight, New Typeface("Verdana"), 32, Brushes.Black)
' Set a maximum width and height. If the text overflows these values, an ellipsis "..." appears.
formattedText.MaxTextWidth = 300
formattedText.MaxTextHeight = 240
' Use a larger font size beginning at the first (zero-based) character and continuing for 5 characters.
' The font size is calculated in terms of points -- not as device-independent pixels.
formattedText.SetFontSize(36 * (96.0 / 72.0), 0, 5)
' Use a Bold font weight beginning at the 6th character and continuing for 11 characters.
formattedText.SetFontWeight(FontWeights.Bold, 6, 11)
' Use a linear gradient brush beginning at the 6th character and continuing for 11 characters.
formattedText.SetForegroundBrush(New LinearGradientBrush(Colors.Orange, Colors.Teal, 90.0), 6, 11)
' Use an Italic font style beginning at the 28th character and continuing for 28 characters.
formattedText.SetFontStyle(FontStyles.Italic, 28, 28)
' Draw the formatted text string to the DrawingContext of the control.
drawingContext.DrawText(formattedText, New Point(10, 0))
End Sub
字号度量单位
与 Windows Presentation Foundation(WPF) 应用程序中的其他文本对象一样,FormattedText 对象使用独立于设备的像素作为度量单位。 但是,大多数 Win32 应用程序使用点作为度量单位。 如果要在 Windows Presentation Foundation (WPF) 应用程序中以点(points)为单位显示文本,则需要将设备无关的单位(每单位 1/96 英寸)转换为点(points)。 下面的代码示例演示如何执行此转换。
// The font size is calculated in terms of points -- not as device-independent pixels.
formattedText.SetFontSize(36 * (96.0 / 72.0), 0, 5);
' The font size is calculated in terms of points -- not as device-independent pixels.
formattedText.SetFontSize(36 * (96.0 / 72.0), 0, 5)
将格式化文本转换为几何图形
可以将格式化文本转换为 Geometry 对象,从而创建其他类型的视觉有趣的文本。 例如,可以根据文本字符串的轮廓创建 Geometry 对象。
以下示例演示了通过修改转换后文本的笔划、填充和突出显示来创建有趣的视觉效果的几种方法。
当文本转换为 Geometry 对象时,它不再是字符集合,不能修改文本字符串中的字符。 但是,可以通过修改转换后的文本的笔划和填充属性来影响转换文本的外观。 笔划是指已转换文本的轮廓;填充是指转换后文本轮廓内的区域。 有关详细信息,请参阅 创建大纲文本。
还可以将格式化文本转换为 PathGeometry 对象,并使用该对象突出显示文本。 例如,可以将动画应用于 PathGeometry 对象,以便动画遵循格式化文本的轮廓。
以下示例显示了已转换为 PathGeometry 对象的格式化文本。 一个动画椭圆沿着文本笔划的路径移动。
的路径几何图形
沿着文本路径几何图形运动的球
有关详细信息,请参阅如何:为文本创建 PathGeometry 动画。
转换为 PathGeometry 对象后,可以创建格式化文本的其他有趣用途。 例如,可剪辑视频,以便在格式化文本中显示。
的路径几何图形中显示的视频
Win32 迁移
绘制文本 FormattedText 的功能类似于 Win32 DrawText 函数的功能。 对于从 Win32 API 迁移的开发人员,下表列出了 Win32 DrawText 标志以及 Windows Presentation Foundation (WPF)中的大致等效项。
DrawText 标志 | WPF 等效项 | 注释 |
---|---|---|
DT_BOTTOM | Height | 使用 Height 属性计算适当的 Win32 DrawText “y” 位置。 |
DT_CALCRECT | Height、Width | 使用 Height 和 Width 属性计算输出矩形。 |
DT_CENTER | TextAlignment | 将 TextAlignment 属性的值设置为 Center。 |
DT_EDITCONTROL | 没有 | 不需要。 空格宽度和最后一行的呈现与框架编辑控件相同。 |
DT_结束省略号 | Trimming | 将 Trimming 属性与值 CharacterEllipsis一起使用。 使用 WordEllipsis 来获取带有 DT_WORD_ELIPSIS 尾部省略号的 Win32 DT_END_ELLIPSIS;在这种情况下,省略号字符仅出现在一行容不下的字词中。 |
DT_EXPAND_TABS | 没有 | 不需要。 制表符自动扩展为在每 4 个 em 后停止,这大约为 8 个与语言无关的字符的宽度。 |
DT_EXTERNALLEADING | 没有 | 不需要。 行距中始终包括外部间隙。 使用 LineHeight 属性创建用户定义的行距。 |
DT_HIDEPREFIX (隐藏前缀) | 没有 | 不支持。 在构造 FormattedText 对象之前,请从字符串中删除“&”。 |
DT_LEFT | TextAlignment | 这是默认文本对齐方式。 将 TextAlignment 属性的值设置为 Left。 (仅限 WPF) |
DT_MODIFYSTRING | 没有 | 不支持。 |
DT_NOCLIP | VisualClip | 剪辑不会自动发生。 如果要剪辑文本,请使用 VisualClip 属性。 |
DT_NOFULLWIDTHCHARBREAK | 没有 | 不支持。 |
DT_NOPREFIX | 没有 | 不需要。 字符串中的“&”字符始终被视为普通字符。 |
DT_PATHELLIPSIS | 没有 | 将 Trimming 属性与值 WordEllipsis一起使用。 |
DT_PREFIX | 没有 | 不支持。 如果要对文本(如快捷键或链接)使用下划线,请使用 SetTextDecorations 方法。 |
DT_PREFIXONLY | 没有 | 不支持。 |
DT_RIGHT | TextAlignment | 将 TextAlignment 属性的值设置为 Right。 (仅限 WPF) |
DT_RTLREADING | FlowDirection | 将 FlowDirection 属性设置为 RightToLeft。 |
DT_单行文本 | 没有 | 不需要。 FormattedText 对象表现为单行控件,除非设置了 MaxTextWidth 属性或文本包含回车/换行 (CR/LF) 字符。 |
DT_TABSTOP | 没有 | 不支持用户定义的制表位位置。 |
DT_TOP | Height | 不需要。 顶部理由是默认值。 可以使用 Height 属性来定义其他垂直定位值,以计算适当的 Win32 DrawText “y” 位置。 |
DT_VCENTER | Height | 使用 Height 属性计算适当的 Win32 DrawText “y” 位置。 |
DT_WORDBREAK | 没有 | 不需要。 使用 FormattedText 对象会自动发生断字。 不能禁用它。 |
省略号 | Trimming | 将 Trimming 属性与值 WordEllipsis一起使用。 |