fonts - 在 WinRT 中计算字体基线

标签 fonts windows-runtime baseline

我想找出baseline of a font在 WinRT 中。

我还通过 creating a dummy TextBlock 计算了特定字体的文本大小,但我不确定如何计算基线。这在 WinRT 中甚至可能吗?

最佳答案

不幸的是,您要找的是FormattedText [MSDN:1 2 ] 存在于 WPF 中,而在 WinRT 中不存在(我什至不认为它甚至在 Silverlight 中)。

它可能会包含在 future 的版本中,因为它似乎是一个非常受追捧的功能,非常想念并且团队意识到它被遗漏了。看这里:http://social.msdn.microsoft.com .

如果您有兴趣或真的,真的需要一种方法来测量字体的细节,您可以尝试为 编写一个包装器。直写 据我所知,它位于 WinRT 可用技术堆栈中,但只能通过 C++ 访问

如果您想尝试,这里有几个起点供您引用:

  • these guys seem to actually be using DirectWrite in a WinRT app
  • this is a wrapper for C++ making DX available, DirectWrite would be much of the same

  • 希望这会有所帮助,祝你好运 -ck

    更新

    我想了想,想起了TextBlock s 拥有经常被遗忘的属性(property) BaselineOffset这为您提供了所选字体从框顶部的基线下降!因此,您可以使用每个人都使用的相同 hack 来替换 MeasureString代替FormattedText的损失.酱汁来了:
        private double GetBaselineOffset(double size, FontFamily family = null, FontWeight? weight = null, FontStyle? style = null, FontStretch? stretch = null)
        {
            var temp = new TextBlock();
            temp.FontSize = size;
            temp.FontFamily = family ?? temp.FontFamily;
            temp.FontStretch = stretch ?? temp.FontStretch;
            temp.FontStyle = style ?? temp.FontStyle;
            temp.FontWeight = weight ?? temp.FontWeight;
    
            var _size = new Size(10000, 10000);
            var location = new Point(0, 0);
    
            temp.Measure(_size);
            temp.Arrange(new Rect(location, _size));
    
            return temp.BaselineOffset;
        }
    

    我用它来做到这一点:
    screenshot

    完美的!对?希望这有帮助 -ck

    关于fonts - 在 WinRT 中计算字体基线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15862600/

    相关文章:

    c# - 如何允许在 ListView/GridView 项控件内进行操作,同时允许在 ListView/GridView 上进行滚动和横向滑动操作?

    c# - 当应用程序在 WP 8.1 商店应用程序中恢复时,应用程序恢复事件不会触发

    Azure ARM - 具有 ARM 模板的基线资源

    javascript - 如何使用 Javascript 更改 @font-face 源?

    android - Android 中字体的物理大小(以磅为单位)

    c# - 是否可以防止 Windows 10 uwp 中的锁屏?

    flutter - TextBaseline 的字母和表意枚举在 Flutter 中不起作用

    html - Firefox 中不显示字体

    html - 如何在 CSS 中添加新字体以进行本地测试

    css - H 标签不会位于基线网格上