背水一战 Windows 10 (27) - 控件(文本类): TextBlock
2021-05-12 22:27
标签:鼠标 颜色 change ssi 显示不全 dir stroke 字体 触摸 [源码下载] 作者:webabcd 示例 Controls/TextControl/TextBlockDemo1.xaml.cs Controls/TextControl/TextBlockDemo2.xaml.cs Controls/TextControl/Tips.xaml.cs OK 背水一战 Windows 10 (27) - 控件(文本类): TextBlock 标签:鼠标 颜色 change ssi 显示不全 dir stroke 字体 触摸 原文地址:http://www.cnblogs.com/lonelyxmas/p/7567230.html
介绍
背水一战 Windows 10 之 控件(文本类)
1、TextBlock 的示例 1
Controls/TextControl/TextBlockDemo1.xamlPage
x:Class="Windows10.Controls.TextControl.TextBlockDemo1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.Controls.TextControl"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
Grid Background="Transparent">
StackPanel Margin="10 0 10 10">
TextBlock Text="i am a "TextBlock"" Margin="5"
Foreground="Blue"
FontFamily="微软雅黑"
FontSize="24"
FontWeight="Bold"
FontStyle="Italic"
FontStretch="Normal"
Padding="5" />
TextBlock Text="i am a "TextBlock"" Margin="5" TextAlignment="Justify" />
TextBlock Width="100" HorizontalAlignment="Left" Text="abcdefghijklmnopq www" Margin="5" TextWrapping="NoWrap" />
TextBlock Width="100" HorizontalAlignment="Left" Text="abcdefghijklmnopq www" Margin="5" TextWrapping="Wrap" />
TextBlock Width="100" HorizontalAlignment="Left" Text="abcdefghijklmnopq www" Margin="5" TextWrapping="WrapWholeWords" />
TextBlock Width="100" HorizontalAlignment="Left" Text="abcdefghi jklmnopqrstuvwxyz" Margin="5" TextTrimming="None"/>
TextBlock Width="100" HorizontalAlignment="Left" Text="abcdefghi jklmnopqrstuvwxyz" Margin="5" TextTrimming="Clip"/>
TextBlock Width="100" HorizontalAlignment="Left" Text="abcdefghi jklmnopqrstuvwxyz" Margin="5" TextTrimming="WordEllipsis"/>
TextBlock Width="100" HorizontalAlignment="Left" Text="abcdefghi jklmnopqrstuvwxyz" Margin="5" TextTrimming="CharacterEllipsis"/>
TextBlock MaxLines="3" Margin="5">
TextBlock.Inlines>
Run>111111Run>
LineBreak />
Run>222222Run>
LineBreak />
Run>333333Run>
LineBreak />
Run>444444Run>
LineBreak />
Run>555555Run>
TextBlock.Inlines>
TextBlock>
TextBlock Margin="5" CharacterSpacing="100" LineStackingStrategy="MaxHeight" LineHeight="50">
TextBlock.Inlines>
Run Foreground="Red">RunRun>
Span Foreground="Blue">SpanSpan>
LineBreak />
Bold>BoldBold>
Italic>ItalicItalic>
Underline>UnderlineUnderline>
TextBlock.Inlines>
TextBlock>
Grid Background="Red" Margin="5">
TextBlock Text="Aj" FontSize="24" TextLineBounds="Full" />
Grid>
Grid Background="Red" Margin="5">
TextBlock Text="Aj" FontSize="24" TextLineBounds="TrimToCapHeight" />
Grid>
Grid Background="Red" Margin="5">
TextBlock Text="Aj" FontSize="24" TextLineBounds="TrimToBaseline" />
Grid>
Grid Background="Red" Margin="5">
TextBlock Text="Aj" FontSize="24" TextLineBounds="Tight" />
Grid>
TextBlock Text="<>&"" Margin="5" />
TextBlock Text="==&&" Margin="5" />
StackPanel>
Grid>
Page>
/*
* TextBlock - 文本显示框(继承自 FrameworkElement, 请参见 /Controls/BaseControl/FrameworkElementDemo/)
*/
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace Windows10.Controls.TextControl
{
public sealed partial class TextBlockDemo1 : Page
{
public TextBlockDemo1()
{
this.InitializeComponent();
}
}
}
2、TextBlock 的示例 2
Controls/TextControl/TextBlockDemo2.xamlPage
x:Class="Windows10.Controls.TextControl.TextBlockDemo2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.Controls.TextControl"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
Grid Background="Transparent">
StackPanel Margin="10 0 10 10">
TextBlock Name="textBlock1" Margin="5" IsTextSelectionEnabled="True" SelectionHighlightColor="Red">
TextBlock.Inlines>
Run>abcdefgRun>
LineBreak />
Run>hijklmnRun>
LineBreak />
Run>opqrstRun>
TextBlock.Inlines>
TextBlock>
TextBlock Name="lblMsg1" Margin="5" />
TextBlock Name="textBlock2" Margin="5" IsTextSelectionEnabled="True" SelectionHighlightColor="Red">
Run>abcdefghijklmnRun>
LineBreak />
Run>opqrstuvwxyzRun>
TextBlock>
TextBlock Name="lblMsg2" Margin="5" />
Grid Background="Red" Margin="5">
TextBlock Name="textBlock3" Text="abcdefghijklmnopqrstuvwxyz" FontSize="24" />
Line Name="line" Stroke="Blue" StrokeThickness="1" X1="0" X2="300" />
Grid>
TextBlock Name="lblMsg3" Margin="5" />
TextBlock Name="textBlock4" Margin="5" FontSize="24" TextWrapping="Wrap" />
Grid Name="grid" Margin="5">
Grid.ColumnDefinitions>
ColumnDefinition x:Name="gridColumn1" Width="Auto" />
ColumnDefinition x:Name="gridColumn2" Width="*" />
Grid.ColumnDefinitions>
TextBlock Name="textBlock5" FontSize="24">
Run>abcRun>
LineBreak />
Run>xyzRun>
TextBlock>
Grid>
TextBlock Name="lblMsg5" Margin="5" />
StackPanel>
Grid>
Page>
/*
* TextBlock - 文本显示框(继承自 FrameworkElement, 请参见 /Controls/BaseControl/FrameworkElementDemo/)
* SelectedText - 获取选中的文本内容
* SelectionChanged - 选中的文本内容发生变化后触发的事件
* ContentStart - 开头内容的 TextPointer 对象
* ContentEnd - 结尾内容的 TextPointer 对象
* Focus(FocusState value) - 获取焦点
* SelectAll() - 选中全部内容(先要获取焦点后,才能做这个操作)
* Select(TextPointer start, TextPointer end) - 选中指定范围的内容(先要获取焦点后,才能做这个操作)
* SelectionStart - 选中内容的起始位置(TextPointer 对象)
* SelectionEnd - 选中内容的结束位置(TextPointer 对象)
* BaselineOffset - 获取基线的位置(什么是基线:英文字符的基线基本相当于单词本4条线中的第3条线)
* IsColorFontEnabled - 是否以彩色方式显示 Segoe UI Emoji 之类的字符(默认值是 true)
*
*
* TextPointer - 文本框中的指针对象
* Offset - 指针的位置
* LogicalDirection - 指针的逻辑方向
* Backward - 向后,即从右到左(比如,如果插入字符的话,就会在指针位置的左边插入)
* Forward - 向前,即从左到右(比如,如果插入字符的话,就会在指针位置的右边插入)
* Rect GetCharacterRect(LogicalDirection direction) - 返回当前指针的矩形框
* TextPointer GetPositionAtOffset(offset, LogicalDirection direction) - 将指针位置偏移指定的距离(正代表向右偏移,负代表向左偏移)
*/
using System;
using System.Text;
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Documents;
namespace Windows10.Controls.TextControl
{
public sealed partial class TextBlockDemo2 : Page
{
public TextBlockDemo2()
{
this.InitializeComponent();
this.Loaded += TextBlockDemo2_Loaded;
}
private void TextBlockDemo2_Loaded(object sender, RoutedEventArgs e)
{
textBlock1.SelectionChanged += (x, y) =>
{
// 显示用户选中的文本内容
lblMsg1.Text = textBlock1.SelectedText;
};
textBlock1.ContextMenuOpening += (x, y) =>
{
// 触发条件:触摸屏长按或鼠标右键
};
// 获取焦点
textBlock2.Focus(FocusState.Programmatic);
TextPointer start = textBlock2.ContentStart;
TextPointer end = textBlock2.ContentEnd;
// textBlock2 的 Text 的内容一共有 26 个字符,但是这里的指针位置 offset 是从 0 到 34,一共 35 个指针位置
//
3、使用自定义字体, 使用 Unicode 编码
Controls/TextControl/Tips.xamlPage
x:Class="Windows10.Controls.TextControl.Tips"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.Controls.TextControl"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
Grid Background="Transparent">
StackPanel Margin="10 0 10 10">
TextBlock Text="逐浪大雪钢笔体" FontSize="36" Margin="5"
FontFamily="/Controls/TextControl/MyFont.otf#逐浪大雪钢笔体">
TextBlock>
TextBlock Text=""123"
123" FontSize="36" Margin="5" />
StackPanel>
Grid>
Page>
/*
* 演示文本类控件如何使用自定义字体,以及使用 Unicode 编码
*/
using Windows.UI.Xaml.Controls;
namespace Windows10.Controls.TextControl
{
public sealed partial class Tips : Page
{
public Tips()
{
this.InitializeComponent();
}
}
}
[源码下载]
上一篇:背水一战 Windows 10 (35) - 控件(弹出类): FlyoutBase, Flyout, MenuFlyout
下一篇:背水一战 Windows 10 (36) - 控件(弹出类): ToolTip, Popup, PopupMenu
文章标题:背水一战 Windows 10 (27) - 控件(文本类): TextBlock
文章链接:http://soscw.com/essay/84874.html