系统字体为125%时出现奇怪的文本缩放

时间:2014-02-25 11:49:20

标签: c# .net wpf

这是完整的应用程序xaml:

< Window x:Class="WpfApplication30.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow"
        Width="525"
        Height="350">
  < Border VerticalAlignment="Center"
          BorderBrush="Green"
          BorderThickness="1">
    < TextBlock Text="HELLO WORLD!"
               VerticalAlignment="Center"
               FontSize="16" />
  </Border>
</Window>

当系统字体设置为125%时,文本不在边框的中心。顶部边框和文本之间有7个像素,底部边框和文本之间只有4个像素。我该如何解决?

UPD: 加入

UseLayoutRounding="True"
SnapsToDevicePixels="True"

但是diffrenece仍在这里:8px和6px

1 个答案:

答案 0 :(得分:3)

字体垂直居中很复杂,因为TextBlock将与配置空间的中心对齐,以便所选字体中的任何字符都适合此区域。可以在维基百科Baseline (typography)文章

中找到基本概述

您可以尝试使用

来补偿特定字体的指标
  • GetEmHeight(FontStyle)
  • GetCellAscent(FontStyle)
  • GetCellDescent(FontStyle)
  • GetLineSpacing(FontStyle)
<{1>}上的

。并调整FontFamily

的展示位置

来源:How to: Obtain Font Metrics

但我真的建议不这样做,因为有些字符可能会使用TextBlockÁ这样的空格,它们具有不同的高度和线条位置。

对于j monospaced的字体,您可能有更好的默认布局体验,Consolas HELLO WORLD!的字体几乎可以打印到边框的中间位置。