这是完整的应用程序 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 个像素。我该如何解决?
更新: 添加
UseLayoutRounding="True"
SnapsToDevicePixels="True"
但差异仍然存在:8px 和 6px
最佳答案
字体垂直居中很复杂,因为 TextBlock
将与配置空间的中心对齐,以便您选择的字体中的任何字符都适合该区域。可以在维基百科中找到这是为什么的基本概述 Baseline (typography)文章
您可以尝试使用
来补偿特定字体的指标GetEmHeight(FontStyle)
GetCellAscent(FontStyle)
GetCellDescent(FontStyle)
GetLineSpacing(FontStyle)
在 FontFamily
类上。并调整 TextBlock
的位置。
来源:How to: Obtain Font Metrics
但我真的建议不要这样做,因为有些字符可能会使用空格,例如 Á
和 j
,它们具有不同的高度和行位置。
使用 等宽
字体可能会有更好的默认布局体验,例如 Consolas
,HELLO WORLD!
几乎打印到中间边界。
关于c# - 系统字体为 125% 时奇怪的文本缩放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22013324/