You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
> -[Open the WinUI 2 Gallery app and see the TextBlock in action](winui2gallery:/item/TextBlock). [!INCLUDE [winui-2-gallery](../../../includes/winui-2-gallery.md)]
35
-
36
-
We recommend using the latest [WinUI 2](../../winui/winui2/index.md) to get the most current styles, templates, and features for all controls.
37
-
38
26
## Create a text block
39
27
40
28
> [!div class="checklist"]
@@ -46,7 +34,7 @@ We recommend using the latest [WinUI 2](../../winui/winui2/index.md) to get the
There are two properties you can use to add content to a TextBlock: [Text](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.controls.textblock.text) and [Inlines](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.controls.textblock.inlines).
63
51
64
-
The most common way to display text is to set the Text property to a string value, as shown in the previous example.
52
+
The most common way to display text is to set the `Text` property to a string value, as shown in the previous example.
65
53
66
-
You can also add content by placing inline flow content elements in the TextBox.Inlines property, like this.
54
+
You can also add content by placing inline flow content elements in the `Inlines` property, like this. (`Inlines` is the default content property of a TextBlock, so you don't need to explicitly add it in XAML.)
67
55
68
56
```xaml
69
57
<TextBlock>Text can be <Bold>bold</Bold>, <Underline>underlined</Underline>,
70
58
<Italic>italic</Italic>, or a <Bold><Italic>combination</Italic></Bold>.</TextBlock>
71
59
```
72
60
73
-
Elements derived from the Inline class, such as Bold, Italic, Run, Span, and LineBreak, enable different formatting for different parts of the text. For more info, see the [Formatting text](#formatting-text) section. The inline Hyperlink element lets you add a hyperlink to your text. However, using Inlines also disables fast path text rendering, which is discussed in the next section.
61
+
Elements derived from the [Inline](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.documents.inline) class, such as [Bold](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.documents.bold), [Italic](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.documents.italic), [Run](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.documents.run), [Span](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.documents.span), and [LineBreak](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.documents.linebreak), enable different formatting for different parts of the text. For more info, see the [Formatting text](#formatting-text) section. The inline [Hyperlink](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.documents.hyperlink) element lets you add a hyperlink to your text. However, using `Inlines` also disables fast path text rendering, which is discussed in the next section.
74
62
75
63
## Performance considerations
76
64
77
-
Whenever possible, XAML uses a more efficient code path to layout text. This fast path both decreases overall memory use and greatly reduces the CPU time to do text measuring and arranging. This fast path applies only to TextBlock, so it should be preferred when possible over RichTextBlock.
65
+
Whenever possible, XAML uses a more efficient code path to layout text. This fast path both decreases overall memory use and greatly reduces the CPU time to do text measuring and arranging. This fast path applies only to TextBlock, so it should be preferred when possible over [RichTextBlock](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.controls.richtextblock).
78
66
79
67
Certain conditions require TextBlock to fall back to a more feature-rich and CPU intensive code path for text rendering. To keep text rendering on the fast path, be sure to follow these guidelines when setting the properties listed here.
80
68
81
-
-[Text](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.controls.textblock.text): The most important condition is that the fast path is used only when you set text by explicitly setting the Text property, either in XAML or in code (as shown in the previous examples). Setting the text via TextBlock's Inlines collection (such as `<TextBlock>Inline text</TextBlock>`) will disable the fast path, due to the potential complexity of multiple formats.
69
+
-[Text](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.controls.textblock.text): The most important condition is that the fast path is used only when you set text by explicitly setting the `Text` property, either in XAML or in code (as shown in the previous examples). Setting the text via TextBlock's `Inlines` collection (such as `<TextBlock>Inline text</TextBlock>`) will disable the fast path, due to the potential complexity of multiple formats.
82
70
-[CharacterSpacing](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.controls.textblock.characterspacing): Only the default value of 0 is fast path.
83
-
-[TextTrimming](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.controls.textblock.texttrimming): Only the **None**, **CharacterEllipsis**, and **WordEllipsis** values are fast path. The **Clip** value disables the fast path.
84
-
85
-
> **Note** Prior to Windows 10, version 1607, additional properties also affect the fast path. If your app is run on an earlier version of Windows, these conditions will also cause your text to render on the slow path. For more info about versions, see [Version adaptive code](/windows/uwp/debug-test-perf/version-adaptive-code).
86
-
-[Typography](/uwp/api/Windows.UI.Xaml.Documents.Typography): Only the default values for the various Typography properties are fast path.
87
-
-[LineStackingStrategy](/uwp/api/windows.ui.xaml.controls.textblock.linestackingstrategy): If [LineHeight](/uwp/api/windows.ui.xaml.controls.textblock.lineheight) is not 0, the **BaselineToBaseline** and **MaxHeight** values disable the fast path.
88
-
-[IsTextSelectionEnabled](/uwp/api/windows.ui.xaml.controls.textblock.istextselectionenabled): Only **false** is fast path. Setting this property to **true** disables the fast path.
71
+
-[TextTrimming](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.controls.textblock.texttrimming): Only the `None`, `CharacterEllipsis`, and `WordEllipsis` values are fast path. The `Clip` value disables the fast path.
89
72
90
-
You can set the [DebugSettings.IsTextPerformanceVisualizationEnabled](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.debugsettings.istextperformancevisualizationenabled) property to **true** during debugging to determine whether text is using fast path rendering. When this property is set to true, the text that is on the fast path displays in a bright green color.
73
+
> [!NOTE]
74
+
> **UWP Only:**
75
+
> Prior to Windows 10, version 1607, additional properties also affect the fast path. If your app is run on an earlier version of Windows, these conditions will cause your text to render on the slow path. For more info about versions, see [Version adaptive code](/windows/uwp/debug-test-perf/version-adaptive-code).
76
+
>
77
+
> -[Typography](/uwp/api/Windows.UI.Xaml.Documents.Typography): Only the default values for the various `Typography` properties are fast path.
78
+
> -[LineStackingStrategy](/uwp/api/windows.ui.xaml.controls.textblock.linestackingstrategy): If [LineHeight](/uwp/api/windows.ui.xaml.controls.textblock.lineheight) is not 0, the `BaselineToBaseline` and `MaxHeight` values disable the fast path.
79
+
> -[IsTextSelectionEnabled](/uwp/api/windows.ui.xaml.controls.textblock.istextselectionenabled): Only `false` is fast path. Setting this property to `true` disables the fast path.
91
80
92
-
>**Tip** This feature is explained in depth in this session from Build 2015- XAML Performance: Techniques for Maximizing Universal Windows App Experiences Built with XAML.
81
+
You can set the [DebugSettings.IsTextPerformanceVisualizationEnabled](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.debugsettings.istextperformancevisualizationenabled) property to `true` during debugging to determine whether text is using fast path rendering. When this property is set to `true`, the text that is on the fast path displays in a bright green color.
93
82
94
-
You typically set debug settings in the [OnLaunched](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.application.onlaunched) method override in the code-behind page for App.xaml, like this.
83
+
You typically set debug settings in the [OnLaunched](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.application.onlaunched) method override in the code-behind page for `App.xaml`, like this.
@@ -117,7 +106,7 @@ In this example, the first TextBlock is rendered using the fast path, while the
117
106
<StackPanel/>
118
107
```
119
108
120
-
When you run this XAML in debug mode with IsTextPerformanceVisualizationEnabled set to true, the result looks like this.
109
+
When you run this XAML in debug mode with [IsTextPerformanceVisualizationEnabled](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.debugsettings.istextperformancevisualizationenabled) set to `true`, the result looks like this.
121
110
122
111

123
112
@@ -126,18 +115,18 @@ When you run this XAML in debug mode with IsTextPerformanceVisualizationEnabled
126
115
127
116
## Formatting text
128
117
129
-
Although the Text property stores plain text, you can apply various formatting options to the TextBlock control to customize how the text is rendered in your app. You can set standard control properties like FontFamily, FontSize, FontStyle, Foreground, and CharacterSpacing to change the look of the text. You can also use inline text elements and Typography attached properties to format your text. These options affect only how the TextBlock displays the text locally, so if you copy and paste the text into a rich text control, for example, no formatting is applied.
118
+
Although the [Text](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.controls.textblock.text) property stores plain text, you can apply various formatting options to the TextBlock control to customize how the text is rendered in your app. You can set standard control properties like [FontFamily](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.controls.textblock.fontfamily), [FontSize](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.controls.textblock.fontsize), [FontStyle](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.controls.textblock.fontstyle), [Foreground](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.controls.textblock.foreground), and [CharacterSpacing](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.controls.textblock.characterspacing) to change the look of the text. You can also use inline text elements and [Typography](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.documents.typography) attached properties to format your text. These options affect only how the TextBlock displays the text locally, so if you copy and paste the text into a rich text control, for example, no formatting is applied.
130
119
131
120
> [!NOTE]
132
121
> Remember, as noted in the previous section, inline text elements and non-default typography values are not rendered on the fast path.
133
122
134
123
### Inline elements
135
124
136
-
The [Microsoft.UI.Xaml.Documents](/windows/windows-app-sdk/api/winrt/microsoft.UI.Xaml.Documents) namespace provides a variety of inline text elements that you can use to format your text, such as Bold, Italic, Run, Span, and LineBreak.
125
+
The [Microsoft.UI.Xaml.Documents](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.documents) namespace provides a variety of inline text elements that you can use to format your text, such as [Bold](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.documents.bold), [Italic](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.documents.italic), [Run](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.documents.run), [Span](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.documents.span), and [LineBreak](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.documents.linebreak).
137
126
138
-
You can display a series of strings in a TextBlock, where each string has different formatting. You can do this by using a Run element to display each string with its formatting and by separating each Run element with a LineBreak element.
127
+
You can display a series of strings in a TextBlock, where each string has different formatting. You can do this by using a `Run` element to display each string with its formatting and by separating each `Run` element with a `LineBreak` element.
139
128
140
-
Here's how to define several differently formatted text strings in a TextBlock by using Run objects separated with a LineBreak.
129
+
Here's how to define several differently formatted text strings in a TextBlock by using `Run` objects separated with a `LineBreak`.
141
130
142
131
```xaml
143
132
<TextBlockFontFamily="Segoe UI"Width="400"Text="Sample text formatting runs">
> -[Open the WinUI 2 Gallery app and see the TextBlock in action](winui2gallery:/item/TextBlock). [!INCLUDE [winui-2-gallery](../../../includes/winui-2-gallery.md)]
183
+
184
+
We recommend using the latest [WinUI 2](../../winui/winui2/index.md) to get the most current styles, templates, and features for all controls.
187
185
188
186
## Related articles
189
187
190
188
-[Text controls](text-controls.md)
191
-
-[Guidelines for spell checking](text-controls.md)
0 commit comments