FormattedText 类
.NET Framework 4.5
为在 Windows Presentation Foundation (WPF) 应用程序中绘制文本提供低级别控制。
程序集: PresentationCore(在 PresentationCore.dll 中)
FormattedText 类型公开以下成员。
显示:
名称 | 说明 | |
---|---|---|
FormattedText(String, CultureInfo, FlowDirection, Typeface, Double, Brush) | 使用指定的文本、区域性、流方向、字体、字号和画笔初始化 FormattedText 类的一个新实例。 | |
FormattedText(String, CultureInfo, FlowDirection, Typeface, Double, Brush, NumberSubstitution) | 使用指定的文本、区域性、流方向、字体、字号、画笔和数字替换行为初始化 FormattedText 类的一个新实例。 | |
FormattedText(String, CultureInfo, FlowDirection, Typeface, Double, Brush, NumberSubstitution, TextFormattingMode) | 使用指定的文本、区域性、流方向、字体、字号、画笔、数字替换行为以及文本格式设置模式初始化 FormattedText 类的新实例。 |
显示:
名称 | 说明 | |
---|---|---|
Baseline | 获取 FormattedText 对象中从第一行顶部到第一行基线的距离。 | |
Extent | 获取从第一行最顶部绘制的像素到最后一行最底部绘制的像素之间的距离。 | |
FlowDirection | 获取或设置 FormattedText 对象的 FlowDirection。 | |
Height | 获取 FormattedText 对象中从第一行顶部到最后一行底部的距离。 | |
LineHeight | 获取文本行之间的行高或行距。 | |
MaxLineCount | 获取或设置要显示的最大行数。超出 MaxLineCount 的文本将不会显示。 | |
MaxTextHeight | 获取或设置文本列的最大高度。 | |
MaxTextWidth | 获取或设置文本行的最大文本宽度(长度)。 | |
MinWidth | 获取可完全容纳指定文本内容的可能的最小文本宽度。 | |
OverhangAfter | 获取从文本最后一行的底部到最底部绘制的像素之间的距离。 | |
OverhangLeading | 获取一行中从前导对齐点到前导绘制像素之间的最大距离。 | |
OverhangTrailing | 获取一行中从尾随绘制像素到尾随对齐点之间的最大距离。 | |
Text | 获取要显示的文本字符串。 | |
TextAlignment | 获取或设置 FormattedText 对象中文本的对齐方式。 | |
Trimming | 获取或设置省略文本的指示方式。 | |
Width | 获取一行中前导对齐点和尾随对齐点之间的宽度(不包括任何尾随空格字符)。 | |
WidthIncludingTrailingWhitespace | 获取一行中前导对齐点和尾随对齐点之间的宽度(包括所有尾随空格字符)。 |
显示:
使用 FormattedText 对象可以绘制多行文本,且可以单独对该文本中的每个字符设置格式。下面的示例演示应用了多种格式的文本。
就 WPF 中的文本功能而言,FormattedText 被认为是“低级别”的,这是因为它以图形元素的形式处理文本。WPF 的其他文本功能包括:在专门用于文本的控件的上下文中处理文本(TextBlock、TextBox)、实现流文档模型(请参见流文档概述)或支持 XPS 文档模型(请参见 WPF 中的文档)。
FormattedText 中的很多 setter 方法与 TextElement 支持的附加属性类似,但 TextElement 附加属性只适用于为流或 XPS 提供较高级别的文本支持。
下面的示例创建一个 FormattedText 对象,然后向文本应用多种格式设置样式。
C#
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)); }
Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008(不支持服务器核心角色), Windows Server 2008 R2(支持带 SP1 或更高版本的服务器核心角色;不支持 Itanium)
.NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。