gpt4 book ai didi

c# - 系统字体为 125% 时奇怪的文本缩放

转载 作者:太空狗 更新时间:2023-10-29 19:44:04 26 4
gpt4 key购买 nike

这是完整的应用程序 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,它们具有不同的高度和行位置。

使用 等宽 字体可能会有更好的默认布局体验,例如 ConsolasHELLO WORLD! 几乎打印到中间边界。

关于c# - 系统字体为 125% 时奇怪的文本缩放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22013324/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com