|
| 1 | +--- |
| 2 | +index: texttrimming |
| 3 | +title: TextTrimming |
| 4 | +description: About the TextTrimming property |
| 5 | +--- |
| 6 | + |
| 7 | +import CharacterEllipsis from '/img/reference/properties/texttrimming/texttrimming-characterellipsis.png'; |
| 8 | +import LeadingCharacterEllipsis from '/img/reference/properties/texttrimming/texttrimming-leadingcharacterellipsis.png'; |
| 9 | +import NoTrimming from '/img/reference/properties/texttrimming/texttrimming-none.png'; |
| 10 | +import PrefixCharacterEllipsis from '/img/reference/properties/texttrimming/texttrimming-prefixcharacterellipsis.png'; |
| 11 | +import WordEllipsis from '/img/reference/properties/texttrimming/texttrimming-wordellipsis.png'; |
| 12 | +import TextWrappingWithTextTrimming from '/img/reference/properties/texttrimming/textwrapping-with-texttrimming.png'; |
| 13 | + |
| 14 | +# TextTrimming |
| 15 | + |
| 16 | +## Overview |
| 17 | + |
| 18 | +The `TextTrimming` property allows you to control how text is displayed when it exceeds the maximum available space in a control. This property is accessible by text-displaying controls, such as [`TextBlock`](https://github.com/AvaloniaUI/Avalonia/blob/master/src/Avalonia.Controls/TextBlock.cs), [`SelectableTextBlock`](https://github.com/AvaloniaUI/Avalonia/blob/master/src/Avalonia.Controls/SelectableTextBlock.cs) or [`ContentPresenter`](https://github.com/AvaloniaUI/Avalonia/blob/master/src/Avalonia.Controls/Presenters/ContentPresenter.cs). |
| 19 | + |
| 20 | +Text trimming adds an ellipsis (…) to indicate truncated text, instead of abruptly cutting off the text. |
| 21 | + |
| 22 | +:::note |
| 23 | +Avalonia uses the Unicode ellipsis character `U+2026` by default, not three periods. |
| 24 | +::: |
| 25 | + |
| 26 | +## Trimming Modes |
| 27 | + |
| 28 | +Avalonia provides five text trimming options: |
| 29 | + |
| 30 | +1. None |
| 31 | +2. CharacterEllipsis |
| 32 | +3. WordEllipsis |
| 33 | +4. PrefixCharacterEllipsis |
| 34 | +5. LeadingCharacterEllipsis |
| 35 | + |
| 36 | +### None |
| 37 | + |
| 38 | +No trimming is applied. Text is cut off when it reaches the boundary of the control. |
| 39 | + |
| 40 | +```xml |
| 41 | +<TextBlock Text="This is a very long line of text that will get cut off." |
| 42 | + TextTrimming="None" |
| 43 | + Width="200" /> |
| 44 | +``` |
| 45 | + |
| 46 | +<div className="center" style={{maxWidth:400}}> |
| 47 | +<img className="center" src={NoTrimming} alt="A screenshot of an IDE, displaying a long line of text in a box that is abruptly cut off." /> |
| 48 | +</div> |
| 49 | + |
| 50 | +### CharacterEllipsis |
| 51 | + |
| 52 | +Trims text after a character ends. An ellipsis is added where the text is truncated. |
| 53 | + |
| 54 | +Intended for general-purposes trimming, when your UI design requires precise space usage. |
| 55 | + |
| 56 | +```xml |
| 57 | +<TextBlock Text="This is a very long line of text that will get cut off." |
| 58 | + TextTrimming="CharacterEllipsis" |
| 59 | + Width="200" /> |
| 60 | +``` |
| 61 | + |
| 62 | +<div className="center" style={{maxWidth:400}}> |
| 63 | +<img className="center" src={CharacterEllipsis} alt="A screenshot of an IDE, displaying a long line of text in a box that is cut off after a character, with an ellipsis added." /> |
| 64 | +</div> |
| 65 | + |
| 66 | +### WordEllipsis |
| 67 | + |
| 68 | +Trims text after a word ends. Whole words are preserved, and an ellipsis is added when this is no longer possible. |
| 69 | + |
| 70 | +Intended to maximize readability by preventing incomplete words from appearing. |
| 71 | + |
| 72 | +```xml |
| 73 | +<TextBlock Text="This is a very long line of text that will get cut off." |
| 74 | + TextTrimming="WordEllipsis" |
| 75 | + Width="200" /> |
| 76 | +``` |
| 77 | + |
| 78 | +<div className="center" style={{maxWidth:400}}> |
| 79 | +<img className="center" src={WordEllipsis} alt="A screenshot of an IDE, displaying a long line of text in a box that is cut off after a complete word, with an ellipsis added." /> |
| 80 | +</div> |
| 81 | + |
| 82 | +### PrefixCharacterEllipsis |
| 83 | + |
| 84 | +Trims text in the middle. The beginning and end of the text string are displayed, with an ellipsis separating them. |
| 85 | + |
| 86 | +Default is to show the first eight characters, then an ellipsis, then however many characters are required to fill the available space. |
| 87 | + |
| 88 | +Intended for file paths, URLs, or any other text where both the beginning and end should be displayed. |
| 89 | + |
| 90 | +```xml |
| 91 | +<TextBlock Text="C:\Users\Documents\Projects\MyProject\source.cs" |
| 92 | + TextTrimming="PrefixCharacterEllipsis" |
| 93 | + Width="200" /> |
| 94 | +``` |
| 95 | + |
| 96 | +<div className="center" style={{maxWidth:400}}> |
| 97 | +<img className="center" src={PrefixCharacterEllipsis} alt="A screenshot of an IDE, displaying a long line of text in a box that is cut off in the middle, with an ellipsis placed between the starting and ending characters." /> |
| 98 | +</div> |
| 99 | + |
| 100 | +### LeadingCharacterEllipsis |
| 101 | + |
| 102 | +Trims text from the beginning. An ellipsis starts the displayed text, followed by the characters at the end of the text. |
| 103 | + |
| 104 | +Intended for file paths or any other text where only the end is important. |
| 105 | + |
| 106 | +```xml |
| 107 | +<TextBlock Text="C:\Users\Documents\Projects\MyProject\source.cs" |
| 108 | + TextTrimming="LeadingCharacterEllipsis" |
| 109 | + Width="200" /> |
| 110 | +``` |
| 111 | + |
| 112 | +<div className="center" style={{maxWidth:400}}> |
| 113 | +<img className="center" src={LeadingCharacterEllipsis} alt="A screenshot of an IDE, displaying a long line of text in a box that is cut off at the start, with an ellipsis replacing the starting characters and the ending characters visible." /> |
| 114 | +</div> |
| 115 | + |
| 116 | +## Example uses |
| 117 | + |
| 118 | +### Combining with MaxWidth |
| 119 | + |
| 120 | +Combine `TextTrimming` with `MaxWidth` to create responsive text displays that maintain a consistent area on your UI. |
| 121 | + |
| 122 | +```xml |
| 123 | +<TextBlock Text="{Binding UserName}" |
| 124 | + MaxWidth="300" |
| 125 | + TextTrimming="CharacterEllipsis" /> |
| 126 | +``` |
| 127 | + |
| 128 | +### Combining with TextWrapping |
| 129 | + |
| 130 | +Combine `TextTrimming` and `TextWrapping` to apply trimming to the last visible line when wrapping is enabled. |
| 131 | + |
| 132 | +```xml |
| 133 | +<TextBlock Text="{Binding Content}" |
| 134 | + Width="300" |
| 135 | + MaxLines="3" |
| 136 | + TextWrapping="Wrap" |
| 137 | + TextTrimming="WordEllipsis" /> |
| 138 | +``` |
| 139 | + |
| 140 | +<div className="center" style={{maxWidth:400}}> |
| 141 | +<img className="center" src={TextWrappingWithTextTrimming} alt="A screenshot of an IDE, displaying a long line of text in a box that wraps within the box for three lines, before being cut off with an ellipsis added." /> |
| 142 | +</div> |
| 143 | + |
| 144 | +## See also |
| 145 | + |
| 146 | +- [TextBlock control](https://docs.avaloniaui.net/docs/reference/controls/textblock) |
| 147 | +- [SelectableTextBlock control](https://docs.avaloniaui.net/docs/reference/controls/selectable-textblock) |
| 148 | +- [TextTrimming API reference](https://reference.avaloniaui.net/api/Avalonia.Media/TextTrimming/) |
0 commit comments