TextBlock 클래스
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
적은 양의 텍스트를 표시하기 위한 간단한 컨트롤을 제공합니다.
public ref class TextBlock sealed : FrameworkElement
/// [Windows.Foundation.Metadata.Activatable(65536, Windows.Foundation.UniversalApiContract)]
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
/// [Windows.UI.Xaml.Markup.ContentProperty(Name="Inlines")]
class TextBlock final : FrameworkElement
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
/// [Windows.UI.Xaml.Markup.ContentProperty(Name="Inlines")]
/// [Windows.Foundation.Metadata.Activatable(65536, "Windows.Foundation.UniversalApiContract")]
class TextBlock final : FrameworkElement
[Windows.Foundation.Metadata.Activatable(65536, typeof(Windows.Foundation.UniversalApiContract))]
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
[Windows.UI.Xaml.Markup.ContentProperty(Name="Inlines")]
public sealed class TextBlock : FrameworkElement
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
[Windows.UI.Xaml.Markup.ContentProperty(Name="Inlines")]
[Windows.Foundation.Metadata.Activatable(65536, "Windows.Foundation.UniversalApiContract")]
public sealed class TextBlock : FrameworkElement
Public NotInheritable Class TextBlock
Inherits FrameworkElement
<TextBlock ...>text</TextBlock>
-or-
<TextBlock>
oneOrMoreInlineElements
</TextBlock>
-or-
<TextBlock .../>
- 상속
- 특성
Windows 요구 사항
| 디바이스 패밀리 |
Windows 10 (10.0.10240.0에서 도입되었습니다.)
|
| API contract |
Windows.Foundation.UniversalApiContract (v1.0에서 도입되었습니다.)
|
예제
팁
자세한 정보, 디자인 지침 및 코드 예제는 텍스트 블록을 참조하세요.
WinUI 2 갤러리 앱 이 설치된 경우 여기를 클릭하여 앱을 열고 작동 중인 TextBlock을 확인합니다.
이 예제에서는 텍스트 선택을 사용하도록 설정하고 텍스트 줄바꿈을 사용하도록 설정한 TextBlock을 보여 줍니다.
중요
TextBlock 내에서 텍스트 선택에 키보드를 사용하는 경우 사용자는 먼저 Caret 브라우징을 활성화해야 합니다(포그라운드의 앱에서 F7 키를 누릅니다).
렌더링된 텍스트는 다음과 같습니다.
<TextBlock Text="This text demonstrates the wrapping behavior of a TextBlock." Width="240"
IsTextSelectionEnabled="True" TextWrapping="Wrap"/>
TextBlock textBlock = new TextBlock();
textBlock.Text = "This text demonstrates the wrapping behavior of a TextBlock.";
textBlock.Width = 240;
textBlock.IsTextSelectionEnabled = true;
textBlock.TextWrapping = TextWrapping.Wrap;
// Add TextBlock to the visual tree.
rootPanel.Children.Add(textBlock);
이 예제에서는 텍스트 한 Run 자를 사용하여 TextBlock의 모양을 사용자 지정하는 방법을 보여줍니다. FontWeight, FontFamily, FontStyleForeground 색 및 SelectionHighlightColor 속성이 사용자 지정됩니다.
렌더링된 텍스트는 다음과 같습니다.
<TextBlock Text="This text demonstrates some TextBlock properties."
IsTextSelectionEnabled="True"
SelectionHighlightColor="Green"
Foreground="Blue"
FontWeight="Light"
FontFamily="Arial"
FontStyle="Italic"/>
TextBlock textBlock = new TextBlock();
textBlock.Text = "This text demonstrates some TextBlock properties.";
textBlock.IsTextSelectionEnabled = true;
textBlock.SelectionHighlightColor = new SolidColorBrush(Windows.UI.Colors.Green);
textBlock.Foreground = new SolidColorBrush(Windows.UI.Colors.Blue);
textBlock.FontWeight = Windows.UI.Text.FontWeights.Light;
textBlock.FontFamily = new FontFamily("Arial");
textBlock.FontStyle = Windows.UI.Text.FontStyle.Italic;
// Add TextBlock to the visual tree.
rootPanel.Children.Add(textBlock);
이 예제에서는 TextBlock 내에서 다른 인라인 요소를 사용자 지정하는 방법을 보여 줍니다.
렌더링된 텍스트는 다음과 같습니다.
<TextBlock IsTextSelectionEnabled="True" SelectionHighlightColor="Green" FontFamily="Arial">
<Run Foreground="Blue" FontWeight="Light" Text="This text demonstrates "></Run>
<Span FontWeight="SemiBold">
<Run FontStyle="Italic">the use of inlines </Run>
<Run Foreground="Red">with formatting.</Run>
</Span>
</TextBlock>
TextBlock textBlock = new TextBlock();
textBlock.IsTextSelectionEnabled = true;
textBlock.SelectionHighlightColor = new SolidColorBrush(Windows.UI.Colors.Green);
textBlock.FontFamily = new FontFamily("Arial");
// For Run and Span, add 'using Windows.UI.Xaml.Documents;'
Windows.UI.Xaml.Documents.Run run = new Run();
run.Foreground = new SolidColorBrush(Windows.UI.Colors.Blue);
run.FontWeight = Windows.UI.Text.FontWeights.Light;
run.Text = "This text demonstrates ";
Windows.UI.Xaml.Documents.Span span = new Span();
span.FontWeight = Windows.UI.Text.FontWeights.SemiBold;
Run run1 = new Run();
run1.FontStyle = Windows.UI.Text.FontStyle.Italic;
run1.Text = "the use of inlines ";
Run run2 = new Run();
run2.Foreground = new SolidColorBrush(Windows.UI.Colors.Red);
run2.Text = "with formatting.";
span.Inlines.Add(run1);
span.Inlines.Add(run2);
textBlock.Inlines.Add(run);
textBlock.Inlines.Add(span);
// Add TextBlock to the visual tree.
rootPanel.Children.Add(textBlock);
이 예제에서는 인라인 하이퍼링크를 사용하는 방법을 보여줍니다. 자세한 내용은 Hyperlink를 참조하세요.
<TextBlock><Hyperlink xml:space="preserve" NavigateUri="http://www.bing.com"> Hyperlink to Bing </Hyperlink></TextBlock>
// Create a TextBlock this is needed to put the hyperlink inside
TextBlock textBlock = new TextBlock();
// Create a Hyperlink and a Run.
// The Run is used as the visible content of the hyperlink.
Hyperlink hyperlink = new Hyperlink();
Run run = new Run();
// Set the Text property on the run.
// This is the visible text of the hyperlink.
run.Text = " Hyperlink to Bing ";
// Add the Run to the Hyperlink.
hyperlink.Inlines.Add(run);
// Set the URI for the Hyperlink.
hyperlink.NavigateUri = new Uri("http://www.bing.com");
// Add the Hyperlink to the TextBlock.
textBlock.Inlines.Add(hyperlink);
// Add TextBlock to the visual tree.
rootPanel.Children.Add(textBlock);
다음 예제에서는 TextBlock의 LineStackingStrategy 텍스트 줄에 대해 줄 상자를 만드는 방법을 확인 하려면 속성을 사용 하는 방법을 보여 줍니다. 첫 번째 TextBlock의 값은 LineStackingStrategy MaxHeight 이고, 두 번째 TextBlock은 BlockLineHeight 값을, 세 번째 TextBlock에는 BaselineToBaseline 값이 있습니다.
렌더링된 텍스트는 다음과 같습니다.
<StackPanel>
<!-- This TextBlock has a LineStackingStrategy set to "MaxHeight". -->
<TextBlock FontFamily="Verdana"
LineStackingStrategy="MaxHeight"
LineHeight="10"
Width="500"
TextWrapping="Wrap" >
Use the <Run FontSize="30">LineStackingStrategy</Run> property to determine how a line box is
created for each line. A value of <Run FontSize="20">MaxHeight</Run> specifies that the stack
height is the smallest value that contains all the inline elements on that line when those
elements are properly aligned. A value of <Run FontSize="20">BlockLineHeight</Run> specifies
that the stack height is determined by the block element LineHeight property value. A value of
<Run FontSize="20">BaselineToBaseline</Run> specifies that the stack height is determined by adding
LineHeight to the baseline of the previous line.
</TextBlock>
<!-- With a margin pushing down 20 pixels, draw a line just above the second textblock. -->
<!-- The fonts will reach above the LineHeight size and over the line. -->
<StackPanel Margin="0,20,0,0" HorizontalAlignment="Center">
<Line Stroke="Green" X2="500" />
</StackPanel>
<!-- Here is the same TextBlock but the LineStackingStrategy is set to "BlockLineHeight". -->
<TextBlock FontFamily="Verdana"
LineStackingStrategy="BlockLineHeight"
LineHeight="10"
Width="500"
TextWrapping="Wrap">
Use the <Run FontSize="30">LineStackingStrategy</Run> property to determine how a line box is
created for each line. A value of <Run FontSize="20">MaxHeight</Run> specifies that the stack
height is the smallest value that contains all the inline elements on that line when those
elements are properly aligned. A value of <Run FontSize="20">BlockLineHeight</Run> specifies
that the stack height is determined by the block element LineHeight property value. A value of
<Run FontSize="20">BaselineToBaseline</Run> specifies that the stack height is determined by adding
LineHeight to the baseline of the previous line.
</TextBlock>
<!-- With a margin pushing down 20 pixels, draw a line just above the third textblock. -->
<StackPanel Margin="0,20,0,0" HorizontalAlignment="Center">
<Line Stroke="Green" X2="500" />
</StackPanel>
<!-- Here is the same TextBlock but the LineStackingStrategy is set to "BaselineToBaseline". -->
<TextBlock FontFamily="Verdana"
LineStackingStrategy="BaselineToBaseline"
LineHeight="10"
Width="500"
TextWrapping="Wrap">
Use the <Run FontSize="30">LineStackingStrategy</Run> property to determine how a line box is
created for each line. A value of <Run FontSize="20">MaxHeight</Run> specifies that the stack
height is the smallest value that contains all the inline elements on that line when those
elements are properly aligned. A value of <Run FontSize="20">BlockLineHeight</Run> specifies
that the stack height is determined by the block element LineHeight property value. A value of
<Run FontSize="20">BaselineToBaseline</Run> specifies that the stack height is determined by adding
LineHeight to the baseline of the previous line.
</TextBlock>
</StackPanel>
설명
팁
자세한 정보, 디자인 지침 및 코드 예제는 텍스트 블록을 참조하세요.
TextBlock은 앱에서 읽기 전용 텍스트를 표시하기 위한 기본 컨트롤입니다. 이 컨트롤을 사용하여 한 줄 또는 여러 줄 텍스트, 인라인 하이퍼링크 및 굵게, 기울임꼴 또는 밑줄 서식이 적용된 텍스트를 표시할 수 있습니다.
TextBlock은 일반적으로 사용하기 쉽고 보다 나은 텍스트 렌더링 성능을 RichTextBlock제공하므로 대부분의 앱 UI 텍스트에 선호됩니다. 또한 텍스트가 렌더링되는 방식을 사용자 지정할 수 있도록 동일한 서식 옵션을 여러 개 제공합니다. 텍스트에 줄 바꿈을 넣을 수 있지만 TextBlock은 단일 단락을 표시하도록 설계되었으며 텍스트 들여쓰기를 지원하지 않습니다. RichTextBlock 여러 단락, 여러 열 텍스트 또는 이미지와 같은 인라인 UI 요소에 대한 지원이 필요한 경우를 고려합니다.
텍스트 성능
Windows 10부터 전체 메모리 사용을 줄이고 텍스트 측정 및 정렬을 수행하는 CPU 시간을 크게 줄이는 TextBlock의 성능이 향상되었습니다. 이러한 성능 향상에 대해 자세히 알아보려면 TextBlock 컨트롤 가이드의 성능 고려 사항 섹션을 참조하세요.
기본 제공 텍스트 스타일
플랫폼과 함께 제공되는 Windows 10가지 텍스트 스타일을 사용하여 텍스트 스타일을 시스템에서 사용되는 텍스트와 정렬할 수 있습니다. 기본 제공 스타일을 사용하여 Windows 10 유형 램프에 맞추는 방법은 다음과 같습니다. 자세한 내용은 XAML 테마 리소스를 참조하세요.
<TextBlock Text="Header" Style="{StaticResource HeaderTextBlockStyle}"/>
<TextBlock Text="SubHeader" Style="{StaticResource SubheaderTextBlockStyle}"/>
<TextBlock Text="Title" Style="{StaticResource TitleTextBlockStyle}"/>
<TextBlock Text="SubTitle" Style="{StaticResource SubtitleTextBlockStyle}"/>
<TextBlock Text="Base" Style="{StaticResource BaseTextBlockStyle}"/>
<TextBlock Text="Body" Style="{StaticResource BodyTextBlockStyle}"/>
<TextBlock Text="Caption" Style="{StaticResource CaptionTextBlockStyle}"/>
렌더링된 텍스트는 다음과 같습니다.
컬러 글꼴
기본적으로 TextBlock은 표시 색 글꼴을 지원합니다. 시스템의 기본 색 글꼴은 Segoe UI 이모지이고 TextBlock은 이 글꼴로 대체되어 문자 모양을 색으로 표시합니다. 자세한 내용은 속성을 참조하세요 IsColorFontEnabled .
<TextBlock FontSize="30">Hello ☺⛄☂♨⛅</TextBlock>
렌더링된 텍스트는 다음과 같습니다.
버전 기록
| Windows 버전 | SDK 버전 | 추가된 값 |
|---|---|---|
| 1607 | 14393 | GetAlphaMask |
| 1703 | 15063 | TextDecorations |
| 1709 | 16299 | HorizontalTextAlignment |
| 1709 | 16299 | IsTextTrimmed |
| 1709 | 16299 | IsTextTrimmedChanged |
| 1709 | 16299 | TextHighlighters |
| 1809 | 17763 | CopySelectionToClipboard |
| 1809 | 17763 | SelectionFlyout |
생성자
| TextBlock() |
TextBlock 클래스의 새 인스턴스를 초기화합니다. |
속성
메서드
이벤트
적용 대상
추가 정보
피드백
다음에 대한 사용자 의견 제출 및 보기
