WPF教程(二十二)文本呈现

在本章节,我们讨论为什么有时候在WPF中文本呈现得很模糊,之后是如何修复的,你自己又如何控制文本的呈现。

前面的教程我们有提到,其他UI框架如WinForms通过使用Windows API来实现各种功能,相对于这些框架,WPF总是通过其自身实现。在文本显示这里两者表现的非常明显——WinForms使用系统提供的GDI API来显示,而WPF拥有自己的文本呈现方式,更好的支持动画或者设备。

不幸的是,这一点导致了在WPF中,文字显示很模糊,尤其是字体很小的时候。这对于WPF开发者来说是个大问题,还好,微软在.NET框架4.0版本中做了很大的改进。如果使用4.0或者更高版本,文字几乎能达到像素级完美。

控制文本显示

微软在.NET 4.0中决定给开发者更多的文本显示控制,通过引进TextOptions类中的TextFormattingMode属性和TextRenderingMode属性来实现。这样你就可以使用控件级别来决定文本的格式和显示。这个还得使用一个例子来看看这些属性是如何影响文本的显示的。

TextFormattingMode

使用TextFormattingMode属性,你可以决定在格式化文本时使用哪种算法——Ideal(默认)还是Display。正常情况下这个属性无需设置,因为大多数情况下默认的Ideal效果更好。但是如果你要显示很小的文本,那么设置成Display会得到更好的效果。来看下面的例子(差别很微小):

<span style="font-size:14px;"><Window x:Class="WpfTutorialSamples.Control_concepts.TextFormattingModeSample"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="TextFormattingModeSample" Height="200" Width="400">
    <StackPanel Margin="10">
        <Label TextOptions.TextFormattingMode="Ideal" FontSize="9">TextFormattingMode.Ideal, small text</Label>
        <Label TextOptions.TextFormattingMode="Display" FontSize="9">TextFormattingMode.Display, small text</Label>
        <Label TextOptions.TextFormattingMode="Ideal" FontSize="20">TextFormattingMode.Ideal, large text</Label>
        <Label TextOptions.TextFormattingMode="Display" FontSize="20">TextFormattingMode.Display, large text</Label>
    </StackPanel>
</Window></span>
Using the TextFormattingMode property


TextRenderingMode

TextRenderingMode属性用于设置哪种反走样算法,当它和Display联合使用的时候,将产生显著的效果。看来下面的例子:

<span style="font-size:14px;"><Window x:Class="WpfTutorialSamples.Control_concepts.TextRenderingModeSample"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="TextRenderingModeSample" Height="300" Width="400">
    <StackPanel Margin="10" TextOptions.TextFormattingMode="Display">
        <Label TextOptions.TextRenderingMode="Auto" FontSize="9">TextRenderingMode.Auto, small text</Label>
        <Label TextOptions.TextRenderingMode="Aliased" FontSize="9">TextRenderingMode.Aliased, small text</Label>
        <Label TextOptions.TextRenderingMode="ClearType" FontSize="9">TextRenderingMode.ClearType, small text</Label>
        <Label TextOptions.TextRenderingMode="Grayscale" FontSize="9">TextRenderingMode.Grayscale, small text</Label>
        <Label TextOptions.TextRenderingMode="Auto" FontSize="18">TextRenderingMode.Auto, large text</Label>
        <Label TextOptions.TextRenderingMode="Aliased" FontSize="18">TextRenderingMode.Aliased, large text</Label>
        <Label TextOptions.TextRenderingMode="ClearType" FontSize="18">TextRenderingMode.ClearType, large text</Label>
        <Label TextOptions.TextRenderingMode="Grayscale" FontSize="18">TextRenderingMode.Grayscale, large text</Label>
    </StackPanel>
</Window></span>
Using the TextRenderingMode property

从上图可以看到,几行文本的差异很大,这特殊情况下得按需使用。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值