FormattedText 類別

定義

在 Windows Presentation Foundation (WPF) 應用程式中提供對繪製文字的低階控制。Provides low-level control for drawing text in Windows Presentation Foundation (WPF) applications.

public ref class FormattedText
public class FormattedText
type FormattedText = class
Public Class FormattedText
繼承
FormattedText

範例

下列範例會建立 FormattedText 物件,然後將數個格式樣式套用至文字。The following example creates a FormattedText object and then applies several formatting styles to the text.

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

備註

FormattedText物件可讓您繪製多行文字,其中文字中的每個字元都可以個別格式化。The FormattedText object allows you to draw multi-line text, in which each character in the text can be individually formatted. 下例顯示已套用多種格式的文字。The following example shows text that has several formats applied to it.

使用 FormattedText 物件顯示的文字Text displayed using FormattedText object
格式化文字字串的範例Example of a formatted text string

就中的文字功能而言 WPFWPFFormattedText 會被視為「低層級」,因為它會將文字當做圖形元素來處理。In terms of text features in WPFWPF, FormattedText is considered "low-level" because it processes text as graphical elements. WPFWPF 專門用於文字的控制項內容中處理文字的其他層面 (TextBlockTextBox) 、執行非固定格式檔模型 (請參閱非固定Flow Document Overview格式檔案模式) ,或支援 XPSXPS 檔模型 (請參閱WPF 中的檔) 。Other aspects of text in WPFWPF handle text in the context of controls that are dedicated to text (TextBlock, TextBox), implement the flow document model (see Flow Document Overview), or support the XPSXPS document model (see Documents in WPF).

中的許多 setter 方法 FormattedText 與支援的附加屬性相似 TextElement ,但 TextElement 附加屬性則適用于 flow 或的較高層級文字支援 XPSXPSMany of the setter methods in FormattedText have similarities to attached properties that are supported by TextElement, but the TextElement attached properties apply to the higher-level text support either for flow or XPSXPS.

建構函式

FormattedText(String, CultureInfo, FlowDirection, Typeface, Double, Brush)
已過時。

使用指定的文字、文化特性、流向、字樣、字型大小和筆刷,初始化 FormattedText 類別的新執行個體。Initializes a new instance of the FormattedText class with the specified text, culture, flow direction, typeface, font size, and brush.

FormattedText(String, CultureInfo, FlowDirection, Typeface, Double, Brush, Double)

使用指定的文字、文化特性、流向、字樣、字型大小、前景筆刷和 pixelsPerDip 值,初始化 FormattedText 類別的新執行個體。Initializes a new instance of the FormattedText class with the specified text, culture, flow direction, typeface, font size, foreground brush, and pixelsPerDip value.

FormattedText(String, CultureInfo, FlowDirection, Typeface, Double, Brush, NumberSubstitution)
已過時。

初始化 FormattedText 類別的執行個體,這個執行個體具有指定的文字、文化特性、流程方向、字樣、字型大小、筆刷和數字取代行為。Initializes a new instance of the FormattedText class with the specified text, culture, flow direction, typeface, font size, brush, and number substitution behavior.

FormattedText(String, CultureInfo, FlowDirection, Typeface, Double, Brush, NumberSubstitution, Double)

使用指定的文字、文化特性、流向、字樣、字型大小、前景筆刷、數字取代行為和 pixelsPerDip 值,初始化 FormattedText 類別的新執行個體。Initializes a new instance of the FormattedText class with the specified text, culture, flow direction, typeface, font size, foreground brush, number substitution behavior, and pixelsPerDip value.

FormattedText(String, CultureInfo, FlowDirection, Typeface, Double, Brush, NumberSubstitution, TextFormattingMode)
已過時。

使用指定的文字、文化特性、流程方向、字樣、字型大小、筆刷和數字替代行為和文字格式化模式,初始化 FormattedText 類別的新執行個體。Initializes a new instance of the FormattedText class with the specified text, culture, flow direction, typeface, font size, brush, number substitution behavior, and text formatting mode.

FormattedText(String, CultureInfo, FlowDirection, Typeface, Double, Brush, NumberSubstitution, TextFormattingMode, Double)

使用指定的文字、文化特性、流向、字樣、字型大小、前景筆刷、數字取代行為和 pixelsPerDip 值,初始化 FormattedText 類別的新執行個體。Initializes a new instance of the FormattedText class with the specified text, culture, flow direction, typeface, font size, foreground brush, number substitution behavior, and pixelsPerDip value.

屬性

Baseline

取得從第一行頂端到 FormattedText 物件第一行基準線的距離。Gets the distance from the top of the first line to the baseline of the first line of a FormattedText object.

Extent

取得從第一行的最頂端繪製像素到最後一行的最底端繪製像素的距離。Gets the distance from the topmost drawn pixel of the first line to the bottommost drawn pixel of the last line.

FlowDirection

取得或設定 FlowDirection 物件的 FormattedTextGets or sets the FlowDirection of a FormattedText object.

Height

取得從 FormattedText 物件第一行頂端到最後一行底端的距離。Gets the distance from the top of the first line to the bottom of the last line of the FormattedText object.

LineHeight

取得文字行之間的行高或行距。Gets the line height, or line spacing, between lines of text.

MaxLineCount

取得或設定可以顯示的最大行數。Gets or sets the maximum number of lines to display. 超過 MaxLineCount 的文字不會顯示。Text exceeding the MaxLineCount will not be displayed.

MaxTextHeight

取得或設定文字欄的最大高度。Gets or sets the maximum height of a text column.

MaxTextWidth

取得或設定文字行的最大文字寬度 (長度)。Gets or sets the maximum text width (length) for a line of text.

MinWidth

取得可以完整容納所指定文字內容的最小文字寬度。Gets the smallest possible text width that can fully contain the specified text content.

OverhangAfter

取得從最後一行文字底端到繪製在最底端的像素的距離。Gets the distance from the bottom of the last line of text to the bottommost drawn pixel.

OverhangLeading

取得從行之前置對齊點到前置繪製的像素的最大距離。Gets the maximum distance from the leading alignment point to the leading drawn pixel of a line.

OverhangTrailing

取得從行之結尾有著色的像素到結尾對齊點的最大距離。Gets the maximum distance from the trailing inked pixel to the trailing alignment point of a line.

PixelsPerDip

取得或設定應轉譯文字的 PixelsPerDip。Get or sets the PixelsPerDip at which the text should be rendered.

Text

取得要顯示的文字字串。Gets the string of text to be displayed.

TextAlignment

取得或設定 FormattedText 物件內的文字對齊。Gets or sets the alignment of text within a FormattedText object.

Trimming

取得或設定用來指出文字省略的方法。Gets or sets the means by which the omission of text is indicated.

Width

取得行的前置與結尾對齊點之間的寬度,但不含任何結尾空白字元。Gets the width between the leading and trailing alignment points of a line, excluding any trailing white-space characters.

WidthIncludingTrailingWhitespace

取得行的前置與結尾對齊點之間的寬度,而且包含任何結尾空白字元。Gets the width between the leading and trailing alignment points of a line, including any trailing white-space characters.

方法

BuildGeometry(Point)

傳回 Geometry 物件,這個物件表示格式化文字 (包括所有圖像和文字裝飾)。Returns a Geometry object that represents the formatted text, including all glyphs and text decorations.

BuildHighlightGeometry(Point)

傳回 Geometry 物件,表示格式化文字的週框方塊。Returns a Geometry object that represents the highlight bounding box of the formatted text.

BuildHighlightGeometry(Point, Int32, Int32)

傳回 Geometry 物件,表示格式化文字之所指定子字串的反白顯示週框方塊。Returns a Geometry object that represents the highlight bounding box for a specified substring of the formatted text.

Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(繼承來源 Object)
GetMaxTextWidths()

擷取文字寬度的陣列。Retrieves an array of text widths. 陣列中的每個元素都表示連續文字行的最大文字寬度。Each element in the array represents the maximum text width of sequential lines of text.

GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
SetCulture(CultureInfo)

設定 CultureInfo 物件中整個字元集的 FormattedTextSets the CultureInfo for the entire set of characters in the FormattedText object.

SetCulture(CultureInfo, Int32, Int32)

設定 CultureInfo 物件中所指定字元子集的 FormattedTextSets the CultureInfo for a specified subset of characters in the FormattedText object.

SetFontFamily(FontFamily)

設定 FormattedText 物件的字型系列。Sets the font family for a FormattedText object.

SetFontFamily(FontFamily, Int32, Int32)

設定 FormattedText 物件中所指定字元子集的字型系列。Sets the font family for a specified subset of characters in the FormattedText object.

SetFontFamily(String)

設定 FormattedText 物件中整個字元集的字型系列。Sets the font family for the entire set of characters in the FormattedText object.

SetFontFamily(String, Int32, Int32)

設定 FormattedText 物件中所指定字元子集的字型系列。Sets the font family for a specified subset of characters in the FormattedText object.

SetFontSize(Double)

設定 FormattedText 物件中整個字元集的字型大小。Sets the font size for the entire set of characters in the FormattedText object.

SetFontSize(Double, Int32, Int32)

設定 FormattedText 物件中所指定字元子集的字型大小。Sets the font size for a specified subset of characters in the FormattedText object.

SetFontStretch(FontStretch)

設定 FormattedText 物件中整個字元集的字型自動縮放值。Sets the font stretch value for the entire set of characters in the FormattedText object.

SetFontStretch(FontStretch, Int32, Int32)

設定 FormattedText 物件中所指定字元子集的字型自動縮放值。Sets the font stretch value for a specified subset of characters in the FormattedText object.

SetFontStyle(FontStyle)

設定 FormattedText 物件中整個字元集的字型樣式。Sets the font style for the entire set of characters in the FormattedText object.

SetFontStyle(FontStyle, Int32, Int32)

設定 FormattedText 物件中所指定字元子集的字型樣式。Sets the font style for a specified subset of characters in the FormattedText object.

SetFontTypeface(Typeface)

設定 FormattedText 物件中整個字元集的字型字樣。Sets the font typeface for the entire set of characters in the FormattedText object.

SetFontTypeface(Typeface, Int32, Int32)

設定 FormattedText 物件中所指定字元子集的字型字樣名稱。Sets the font typeface for a specified subset of characters in the FormattedText object.

SetFontWeight(FontWeight)

設定 FormattedText 物件中整個字元集的字型粗細。Sets the font weight for the entire set of characters in the FormattedText object.

SetFontWeight(FontWeight, Int32, Int32)

變更 FontWeight 物件內所指定文字的 FormattedTextChanges the FontWeight for specified text within a FormattedText object.

SetForegroundBrush(Brush)

變更整個 Brush 物件的前景 FormattedTextChanges the foreground Brush for an entire FormattedText object.

SetForegroundBrush(Brush, Int32, Int32)

變更 Brush 物件內所指定文字的前景 FormattedTextChanges the foreground Brush for specified text within a FormattedText object.

SetMaxTextWidths(Double[])

設定 FormattedText 內的每行最大文字寬度陣列。Sets an array of maximum text widths within the FormattedText, on a per-line basis. 陣列中的每個元素都表示連續文字行的最大文字寬度。Each element in the array represents the maximum text width of sequential lines of text.

SetNumberSubstitution(NumberSubstitution)

設定 FormattedText 物件中整個字元集的數字取代行為。Sets the number substitution behavior for the entire set of characters in the FormattedText object.

SetNumberSubstitution(NumberSubstitution, Int32, Int32)

設定 FormattedText 物件內所指定文字的數字取代行為。Sets the number substitution behavior for specified text within a FormattedText object.

SetTextDecorations(TextDecorationCollection)

設定 TextDecorationCollection 物件中整個字元集的 FormattedTextSets the TextDecorationCollection for the entire set of characters in the FormattedText object.

SetTextDecorations(TextDecorationCollection, Int32, Int32)

設定 TextDecorationCollection 物件內所指定文字的 FormattedTextSets the TextDecorationCollection for specified text within a FormattedText object.

ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)

適用於

另請參閱