【问题标题】:WinRT How to Get Pixel Width of given StringWinRT如何获取给定字符串的像素宽度
【发布时间】:2013-04-08 05:17:29
【问题描述】:

这里有一个解决办法:

FormattedText

但是可惜,WinRT 不支持此功能。 WinRT 中是否有替代/替代方法?

【问题讨论】:

    标签: c# windows-runtime microsoft-metro winrt-xaml pixels


    【解决方案1】:

    以下解决方案取自此答案: Calculate Font baseline in WinRT

    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;
    }
    

    【讨论】:

      【解决方案2】:

      我设法通过 HACK 类型的解决方案解决了这个问题。我的主要要求是为一些文本保留空间。我使用了每个“空白空间”所需的固定字体和计算空间。一旦我知道了这一点,剩下的就变得微不足道了。

      【讨论】:

      • 我有同样的问题,但不知道如何通过阅读您的回答来解决它。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-17
      • 2015-12-30
      • 2011-09-24
      • 2010-11-28
      相关资源
      最近更新 更多