Skip to content

Commit 7d978fe

Browse files
authored
fix formatting to clarify fast path note, other items (#5153)
1 parent ab46d06 commit 7d978fe

File tree

1 file changed

+34
-38
lines changed

1 file changed

+34
-38
lines changed

hub/apps/design/controls/text-block.md

Lines changed: 34 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ description: Text block is the primary control for displaying read-only text in
44
title: Text block
55
label: Text block
66
template: detail.hbs
7-
ms.date: 05/19/2017
7+
ms.date: 02/04/2025
88
ms.topic: article
99
doc-status: Published
1010
ms.localizationpriority: medium
11+
no-loc: [TextBlock, Text, Inline, Inlines, Bold, Italic, Run, Span, LineBreak]
1112
---
1213

1314
# Text block
@@ -22,19 +23,6 @@ Although you can put line breaks in the text, text block is designed to display
2223

2324
For more info about choosing the right text control, see the [Text controls](text-controls.md) article.
2425

25-
## UWP and WinUI 2
26-
27-
[!INCLUDE [uwp-winui2-note](../../../includes/uwp-winui-2-note.md)]
28-
29-
APIs for this control exist in the [Windows.UI.Xaml.Controls](/uwp/api/Windows.UI.Xaml.Controls) namespace.
30-
31-
> [!div class="checklist"]
32-
>
33-
> - **UWP APIs:** [TextBlock class](/uwp/api/Windows.UI.Xaml.Controls.TextBlock), [Text property](/uwp/api/windows.ui.xaml.controls.textblock.text), [Inlines property](/uwp/api/windows.ui.xaml.controls.textblock.inlines)
34-
> - [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-
3826
## Create a text block
3927

4028
> [!div class="checklist"]
@@ -46,7 +34,7 @@ We recommend using the latest [WinUI 2](../../winui/winui2/index.md) to get the
4634
4735
[!INCLUDE [winui-3-gallery](../../../includes/winui-3-gallery.md)]
4836

49-
Here's how to define a simple TextBlock control and set its Text property to a string.
37+
Here's how to define a simple TextBlock control and set its `Text` property to a string.
5038

5139
```xaml
5240
<TextBlock Text="Hello, world!" />
@@ -61,37 +49,38 @@ textBlock1.Text = "Hello, world!";
6149

6250
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).
6351

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.
6553

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.)
6755

6856
```xaml
6957
<TextBlock>Text can be <Bold>bold</Bold>, <Underline>underlined</Underline>,
7058
<Italic>italic</Italic>, or a <Bold><Italic>combination</Italic></Bold>.</TextBlock>
7159
```
7260

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.
7462

7563
## Performance considerations
7664

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).
7866

7967
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.
8068

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.
8270
- [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**&nbsp;&nbsp;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.
8972

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.
9180
92-
>**Tip**&nbsp;&nbsp;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.
9382

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.
9584

9685
```csharp
9786
protected override void OnLaunched(LaunchActivatedEventArgs e)
@@ -117,7 +106,7 @@ In this example, the first TextBlock is rendered using the fast path, while the
117106
<StackPanel/>
118107
```
119108

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.
121110

122111
![Text rendered in debug mode](images/text-block-rendering-performance.png)
123112

@@ -126,18 +115,18 @@ When you run this XAML in debug mode with IsTextPerformanceVisualizationEnabled
126115
127116
## Formatting text
128117

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.
130119

131120
> [!NOTE]
132121
> Remember, as noted in the previous section, inline text elements and non-default typography values are not rendered on the fast path.
133122
134123
### Inline elements
135124

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).
137126

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.
139128

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`.
141130

142131
```xaml
143132
<TextBlock FontFamily="Segoe UI" Width="400" Text="Sample text formatting runs">
@@ -181,15 +170,22 @@ Typography.SetStylisticSet4(textBlock1, true);
181170
<TextBlock>12 x <Run Typography.Fraction="Slashed">1/3</Run> = 4.</TextBlock>
182171
```
183172

184-
## Get the sample code
173+
## UWP and WinUI 2
174+
175+
[!INCLUDE [uwp-winui2-note](../../../includes/uwp-winui-2-note.md)]
176+
177+
APIs for this control exist in the [Windows.UI.Xaml.Controls](/uwp/api/Windows.UI.Xaml.Controls) namespace.
185178

186-
- [WinUI Gallery sample](https://github.com/Microsoft/WinUI-Gallery) - See all the XAML controls in an interactive format.
179+
> [!div class="checklist"]
180+
>
181+
> - **UWP APIs:** [TextBlock class](/uwp/api/Windows.UI.Xaml.Controls.TextBlock), [Text property](/uwp/api/windows.ui.xaml.controls.textblock.text), [Inlines property](/uwp/api/windows.ui.xaml.controls.textblock.inlines)
182+
> - [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.
187185

188186
## Related articles
189187

190188
- [Text controls](text-controls.md)
191-
- [Guidelines for spell checking](text-controls.md)
192-
- [Guidelines for text input](text-controls.md)
193189
- [TextBox class](/windows/windows-app-sdk/api/winrt/microsoft.UI.Xaml.Controls.TextBox)
194190
- [PasswordBox class](/windows/windows-app-sdk/api/winrt/microsoft.UI.Xaml.Controls.PasswordBox)
195191
- [String.Length property](/dotnet/api/system.string.length)

0 commit comments

Comments
 (0)