FormattedText 類別

定義

在 Windows Presentation Foundation (WPF) 應用程式中提供對繪製文字的低階控制。

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

範例

下列範例會建立 物件,然後將數個 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

備註

物件 FormattedText 可讓您繪製多行文字,其中文字中的每個字元都可以個別格式化。 下例顯示已套用多種格式的文字。

使用 FormattedText 物件顯示的文字 格式化文字字串的範例

就 WPF 中的文字功能而言, FormattedText 會被視為「低階」,因為它會將文字當做圖形化元素來處理。 WPF 中文字的其他層面會處理控制項內容中專用於文字 (TextBlockTextBox) 、實作流程檔模型 (請參閱Flow檔概觀) ,或支援 XPS 檔模型, (請參閱WPF) 中的檔

中的 FormattedText 許多 setter 方法與 支援的附加屬性 TextElement 相似,但 TextElement 附加屬性會套用至流程或 XPS 的高階文字支援。

建構函式

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

使用指定的文字、文化特性、流向、字樣、字型大小和筆刷,初始化 FormattedText 類別的新執行個體。

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

使用指定的文字、文化特性、流向、字樣、字型大小、前景筆刷和 pixelsPerDip 值,初始化 FormattedText 類別的新執行個體。

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

初始化 FormattedText 類別的執行個體,這個執行個體具有指定的文字、文化特性、流程方向、字樣、字型大小、筆刷和數字取代行為。

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

使用指定的文字、文化特性、流向、字樣、字型大小、前景筆刷、數字取代行為和 pixelsPerDip 值,初始化 FormattedText 類別的新執行個體。

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

使用指定的文字、文化特性、流程方向、字樣、字型大小、筆刷和數字替代行為和文字格式化模式,初始化 FormattedText 類別的新執行個體。

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

使用指定的文字、文化特性、流向、字樣、字型大小、前景筆刷、數字取代行為和 pixelsPerDip 值,初始化 FormattedText 類別的新執行個體。

屬性

Baseline

取得從第一行頂端到 FormattedText 物件第一行基準線的距離。

Extent

取得從第一行的最頂端繪製像素到最後一行的最底端繪製像素的距離。

FlowDirection

取得或設定 FlowDirection 物件的 FormattedText

Height

取得從 FormattedText 物件第一行頂端到最後一行底端的距離。

LineHeight

取得文字行之間的行高或行距。

MaxLineCount

取得或設定可以顯示的最大行數。 超過 MaxLineCount 的文字不會顯示。

MaxTextHeight

取得或設定文字欄的最大高度。

MaxTextWidth

取得或設定文字行的最大文字寬度 (長度)。

MinWidth

取得可以完整容納所指定文字內容的最小文字寬度。

OverhangAfter

取得從最後一行文字底端到繪製在最底端的像素的距離。

OverhangLeading

取得從行之前置對齊點到前置繪製的像素的最大距離。

OverhangTrailing

取得從行之結尾有著色的像素到結尾對齊點的最大距離。

PixelsPerDip

取得或設定應轉譯文字的 PixelsPerDip。

Text

取得要顯示的文字字串。

TextAlignment

取得或設定 FormattedText 物件內的文字對齊。

Trimming

取得或設定用來指出文字省略的方法。

Width

取得行的前置與結尾對齊點之間的寬度,但不含任何結尾空白字元。

WidthIncludingTrailingWhitespace

取得行的前置與結尾對齊點之間的寬度,而且包含任何結尾空白字元。

方法

BuildGeometry(Point)

傳回 Geometry 物件,這個物件表示格式化文字 (包括所有圖像和文字裝飾)。

BuildHighlightGeometry(Point)

傳回 Geometry 物件,表示格式化文字的週框方塊。

BuildHighlightGeometry(Point, Int32, Int32)

傳回 Geometry 物件,表示格式化文字之所指定子字串的反白顯示週框方塊。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetMaxTextWidths()

擷取文字寬度的陣列。 陣列中的每個元素都表示連續文字行的最大文字寬度。

GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
SetCulture(CultureInfo)

設定 CultureInfo 物件中整個字元集的 FormattedText

SetCulture(CultureInfo, Int32, Int32)

設定 CultureInfo 物件中所指定字元子集的 FormattedText

SetFontFamily(FontFamily)

設定 FormattedText 物件的字型系列。

SetFontFamily(FontFamily, Int32, Int32)

設定 FormattedText 物件中所指定字元子集的字型系列。

SetFontFamily(String)

設定 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)

變更 FontWeight 物件內所指定文字的 FormattedText

SetForegroundBrush(Brush)

變更整個 Brush 物件的前景 FormattedText

SetForegroundBrush(Brush, Int32, Int32)

變更 Brush 物件內所指定文字的前景 FormattedText

SetMaxTextWidths(Double[])

設定 FormattedText 內的每行最大文字寬度陣列。 陣列中的每個元素都表示連續文字行的最大文字寬度。

SetNumberSubstitution(NumberSubstitution)

設定 FormattedText 物件中整個字元集的數字取代行為。

SetNumberSubstitution(NumberSubstitution, Int32, Int32)

設定 FormattedText 物件內所指定文字的數字取代行為。

SetTextDecorations(TextDecorationCollection)

設定 TextDecorationCollection 物件中整個字元集的 FormattedText

SetTextDecorations(TextDecorationCollection, Int32, Int32)

設定 TextDecorationCollection 物件內所指定文字的 FormattedText

ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

適用於

另請參閱