现在的位置: 首页 > 综合 > 正文

FormattedText 类(MSDN)

2013年01月01日 ⁄ 综合 ⁄ 共 5442字 ⁄ 字号 评论关闭

FormattedText 类

.NET Framework 4.5       

为在 Windows Presentation Foundation (WPF) 应用程序中绘制文本提供低级别控制。

System.Object
  System.Windows.Media.FormattedText

命名空间:
System.Windows.Media

程序集: PresentationCore(在 PresentationCore.dll 中)

public class FormattedText

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 获取一行中前导对齐点和尾随对齐点之间的宽度(包括所有尾随空格字符)。

页首

显示:
  名称 说明
公共方法 BuildGeometry 返回一个 Geometry 对象,该对象表示格式化文本,包括所有标志符号和文本修饰。
公共方法 BuildHighlightGeometry(Point) 返回一个 Geometry 对象,该对象表示格式化文本的高光点边界框。
公共方法 BuildHighlightGeometry(Point, Int32, Int32) 返回一个 Geometry 对象,该对象表示格式化文本的指定子字符串的高光点边界框。
公共方法 Equals(Object) 确定指定的对象是否等于当前对象。 (继承自
Object
。)
受保护的方法 Finalize 允许对象在“垃圾回收”回收之前尝试释放资源并执行其他清理操作。 (继承自

Object
。)
公共方法 GetHashCode 作为默认哈希函数。 (继承自
Object
。)
公共方法 GetMaxTextWidths 检索一个由文本宽度组成的数组。该数组中的每个元素都表示连续文本行的最大文本宽度。
公共方法 GetType 获取当前实例的 Type (继承自

Object
。)
受保护的方法 MemberwiseClone 创建当前 Object 的浅表副本。 (继承自

Object
。)
公共方法 SetCulture(CultureInfo) 设置 FormattedText 对象中整个字符集的
CultureInfo
公共方法 SetCulture(CultureInfo, Int32, Int32) 设置 FormattedText 对象中指定的字符子集的
CultureInfo
公共方法 SetFontFamily(FontFamily) 设置 FormattedText 对象的字体系列。
公共方法 SetFontFamily(String) 设置 FormattedText 对象中整个字符集的字体系列。
公共方法 SetFontFamily(FontFamily, Int32, Int32) 设置 FormattedText 对象中指定的字符子集的字体系列。
公共方法 SetFontFamily(String, Int32, Int32) 设置 FormattedText 对象中指定的字符子集的字体系列。
公共方法 SetFontSize(Double) 设置 FormattedText 对象中整个字符集的字号。
公共方法 SetFontSize(Double, Int32, Int32) 设置 FormattedText 对象中指定的字符子集的字号。
公共方法 SetFontStretch(FontStretch) 设置 FormattedText 对象中整个字符集的字体拉伸值。
公共方法 SetFontStretch(FontStretch, Int32, Int32) 设置 FormattedText 对象中指定的字符子集的字体拉伸值。
公共方法 SetFontStyle(FontStyle) 设置 FormattedText 对象中整个字符集的字体样式。
公共方法 SetFontStyle(FontStyle, Int32, Int32) 设置 FormattedText 对象中指定的字符子集的字体样式。
公共方法 SetFontTypeface(Typeface) 设置 FormattedText 对象中整个字符集的字体。
公共方法 SetFontTypeface(Typeface, Int32, Int32) 设置 FormattedText 对象中指定的字符子集的字体。
公共方法 SetFontWeight(FontWeight) 设置 FormattedText 对象中整个字符集的字体粗细。
公共方法 SetFontWeight(FontWeight, Int32, Int32) 更改 FormattedText 对象中指定文本的
FontWeight
公共方法 SetForegroundBrush(Brush) 更改整个 FormattedText 对象的前景
Brush
公共方法 SetForegroundBrush(Brush, Int32, Int32) 更改 FormattedText 对象中指定文本的前景
Brush
公共方法 SetMaxTextWidths 逐行设置 FormattedText 中最大文本宽度的数组。该数组中的每个元素都表示连续文本行的最大文本宽度。
公共方法 SetNumberSubstitution(NumberSubstitution) 设置 FormattedText 对象中整个字符集的数字替换行为。
公共方法 SetNumberSubstitution(NumberSubstitution, Int32, Int32) 设置 FormattedText 对象中指定文本的数字替换行为。
公共方法 SetTextDecorations(TextDecorationCollection) 设置 FormattedText 对象中整个字符集的
TextDecorationCollection
公共方法 SetTextDecorations(TextDecorationCollection, Int32, Int32) 设置 FormattedText 对象中指定文本的
TextDecorationCollection
公共方法 ToString 返回表示当前对象的字符串。 (继承自
Object
。)

页首

使用 FormattedText 对象可以绘制多行文本,且可以单独对该文本中的每个字符设置格式。下面的示例演示应用了多种格式的文本。

格式化文本字符串的示例

使用 FormattedText 对象显示的文本

就 WPF 中的文本功能而言,FormattedText 被认为是“低级别”的,这是因为它以图形元素的形式处理文本。WPF 的其他文本功能包括:在专门用于文本的控件的上下文中处理文本(TextBlockTextBox)、实现流文档模型(请参见流文档概述)或支持
XPS 文档模型(请参见 WPF 中的文档)。

FormattedText 中的很多 setter 方法与
TextElement 支持的附加属性类似,但
TextElement 附加属性只适用于为流或 XPS 提供较高级别的文本支持。

下面的示例创建一个 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));
}


.NET Framework

受以下版本支持:4.5、4、3.5、3.0

.NET Framework Client Profile

受以下版本支持:4、3.5 SP1

Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008(不支持服务器核心角色), Windows Server 2008 R2(支持带 SP1 或更高版本的服务器核心角色;不支持 Itanium)

.NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。  

此类型的任何公共
static(在 Visual Basic 中为
Shared
) 成员都是线程安全的。但不保证所有实例成员都是线程安全的。
【上篇】
【下篇】

抱歉!评论已关闭.