FormattedText 类(MSDN)

FormattedText 类

.NET Framework 4.5       

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

System.Object
  System.Windows.Media.FormattedText

命名空间: System.Windows.Media
程序集: PresentationCore(在 PresentationCore.dll 中)
C#
C++
F#
VB
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 对象,然后向文本应用多种格式设置样式。

C#
VB
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) 成员都是线程安全的。但不保证所有实例成员都是线程安全的。
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值