如何计算 Metro 中字符串的宽度(不显示它)?

发布于 2025-01-01 14:55:33 字数 272 浏览 1 评论 0原文

我未能找到与以下 WPF 代码等效的 Windows 运行时来测量字符串的宽度:

FormattedText formattedText = new FormattedText(in_string,in_culture,in_flowdir,in_font,in_sz,in_color);
string_width = formattedText.WidthIncludingTrailingWhitespace);

有人知道是否可以在 Metro 中完成吗?

I have failed to find a Windows Runtime equivalent to the following WPF code to measure the width of a string:

FormattedText formattedText = new FormattedText(in_string,in_culture,in_flowdir,in_font,in_sz,in_color);
string_width = formattedText.WidthIncludingTrailingWhitespace);

Does anybody know if it can be done in Metro?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你与清晨阳光 2025-01-08 14:55:33

有可能,我找到了一种提供有用测量的方法,但我不确定这是最好的方法:

// Setup the TextBlock with *everything* that affects how it 
// will be drawn (this is not a complete example)
TextBlock^ tb = ref new TextBlock; 
tb->VerticalAlignment = Windows::UI::Xaml::VerticalAlignment::Top; 
tb->HorizontalAlignment = Windows::UI::Xaml::HorizontalAlignment::Left; 
tb->Height = in_height; 
tb->Text = text;

// Be sure to tell Measure() the correct dimensions that the TextBox 
// have to draw in!
tb->Measure(SizeHelper::FromDimensions(Parent->Width,Parent->Height)); 
text_width = tb->DesiredSize.Width;

我的直觉是,在某些情况下,此代码会给出意想不到的结果。

It is possible, I've found one method that gives useful measurements, but I am not sure it is the best way of doing it:

// Setup the TextBlock with *everything* that affects how it 
// will be drawn (this is not a complete example)
TextBlock^ tb = ref new TextBlock; 
tb->VerticalAlignment = Windows::UI::Xaml::VerticalAlignment::Top; 
tb->HorizontalAlignment = Windows::UI::Xaml::HorizontalAlignment::Left; 
tb->Height = in_height; 
tb->Text = text;

// Be sure to tell Measure() the correct dimensions that the TextBox 
// have to draw in!
tb->Measure(SizeHelper::FromDimensions(Parent->Width,Parent->Height)); 
text_width = tb->DesiredSize.Width;

My gut feeling is that there are situations in which this code will give an unexpected result.

戴着白色围巾的女孩 2025-01-08 14:55:33

试试这个:

private double stringWidth(string s, double fontSize)
    {
        if(s==" ")
            s = "\u00A0";  //this line wasn't required in silverlight but is now

        TextBlock t = new TextBlock()
        {
            FontSize = fontSize,
            Text = s
        };
        t.Measure(new Size(double.MaxValue, double.MaxValue));  //this line wasn't required in silverlight but is now
        return t.ActualWidth;
    }

Try this:

private double stringWidth(string s, double fontSize)
    {
        if(s==" ")
            s = "\u00A0";  //this line wasn't required in silverlight but is now

        TextBlock t = new TextBlock()
        {
            FontSize = fontSize,
            Text = s
        };
        t.Measure(new Size(double.MaxValue, double.MaxValue));  //this line wasn't required in silverlight but is now
        return t.ActualWidth;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文