【发布时间】:2013-03-29 13:27:48
【问题描述】:
我想在 WinRT 中找出baseline of a font。
我还通过creating a dummy TextBlock 计算了特定字体的文本大小,但我不确定如何计算基线。这在 WinRT 中是否可行?
【问题讨论】:
标签: fonts windows-runtime baseline
我想在 WinRT 中找出baseline of a font。
我还通过creating a dummy TextBlock 计算了特定字体的文本大小,但我不确定如何计算基线。这在 WinRT 中是否可行?
【问题讨论】:
标签: fonts windows-runtime baseline
不幸的是,您正在寻找的是 FormattedText [MSDN: 1 2 ],它存在于 WPF 中并且不存在于 WinRT 中(我什至不认为它甚至存在于 silverlight 中)。
它可能会包含在未来的版本中,因为它似乎是一个非常受欢迎的功能,非常想念,并且团队意识到它遗漏了。见这里:http://social.msdn.microsoft.com。
如果您有兴趣或真的非常需要一种方法来测量字体的细节,您可以尝试为 DirectWrite 编写一个包装器,据我所知,它在 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
更新
我想了一会儿,并记得TextBlocks 有一个经常被遗忘的属性BaselineOffset,它为您提供了所选字体从框顶部的基线下降!因此,您可以使用每个人都用来替换 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;
}
我用它来做到这一点:
完美!正确的?希望这会有所帮助-ck
【讨论】: