WPF / XAML - 文本可以自动调整大小吗?

发布于 2024-08-16 18:29:46 字数 208 浏览 2 评论 0原文

对于固定大小的可换行文本区域,有没有办法根据文本量使字体大小尽可能大?

例如,如果您有一个 500x500 的区域,其中包含文本“Hello”,则字体大小将非常大。但是,如果您有一段文本,则字体大小会较小以适合该区域。

我看过 Viewbox,但看不出它可以与可换行文本一起使用。

任何可以执行此操作的 xaml 或代码都会有所帮助(不必是特定的控件)。

For a fixed size wrappable text area, is there any way to make the font size as large as possible based on the amount of text?

For example, if you have a 500x500 area with the text "Hello", the font size would be really big. But if you have a paragraph of text the font size would be smaller to fit into the area.

I have looked at Viewbox but can't see that it could work with wrappable text.

ANY xaml or code that could do this would help (doesn't have to be a specific control).

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

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

发布评论

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

评论(1

原谅我要高飞 2024-08-23 18:29:46

你所问的比听起来更复杂,但我会给你一个想法:

<DockPanel x:Name="LayoutRoot">
    <TextBox x:Name="text" Text="this is some text and some more text I don't see any problems..." DockPanel.Dock="Top" TextChanged="text_TextChanged"/>
    <TextBlock DockPanel.Dock="Top" Text="{Binding ElementName=tb, Path=FontSize}"/>
    <Border Name="bd" BorderBrush="Black" BorderThickness="1">
        <TextBlock Name="tb" Text="{Binding ElementName=text, Path=Text}" TextWrapping="Wrap"/>
    </Border>
</DockPanel>

在后面的代码中:

public MainWindow()
{
    this.InitializeComponent();
    RecalcFontSize();
    tb.SizeChanged += new SizeChangedEventHandler(tb_SizeChanged);
}

void tb_SizeChanged(object sender, SizeChangedEventArgs e)
{
    RecalcFontSize();
}

private void RecalcFontSize()
{
    if (tb == null) return;
    Size constraint = new Size(tb.ActualWidth, tb.ActualHeight);
    tb.Measure(constraint);
    while (tb.DesiredSize.Height < tb.ActualHeight)
    {
        tb.FontSize += 1;
        tb.Measure(constraint);
    }
    tb.FontSize -= 1;
}

private void text_TextChanged(object sender, TextChangedEventArgs e)
{
    RecalcFontSize();
}

尝试一下,拖动它,更改文本......

What you're asking is more complex than it sounds, but I'll give you an idea:

<DockPanel x:Name="LayoutRoot">
    <TextBox x:Name="text" Text="this is some text and some more text I don't see any problems..." DockPanel.Dock="Top" TextChanged="text_TextChanged"/>
    <TextBlock DockPanel.Dock="Top" Text="{Binding ElementName=tb, Path=FontSize}"/>
    <Border Name="bd" BorderBrush="Black" BorderThickness="1">
        <TextBlock Name="tb" Text="{Binding ElementName=text, Path=Text}" TextWrapping="Wrap"/>
    </Border>
</DockPanel>

And in code behind:

public MainWindow()
{
    this.InitializeComponent();
    RecalcFontSize();
    tb.SizeChanged += new SizeChangedEventHandler(tb_SizeChanged);
}

void tb_SizeChanged(object sender, SizeChangedEventArgs e)
{
    RecalcFontSize();
}

private void RecalcFontSize()
{
    if (tb == null) return;
    Size constraint = new Size(tb.ActualWidth, tb.ActualHeight);
    tb.Measure(constraint);
    while (tb.DesiredSize.Height < tb.ActualHeight)
    {
        tb.FontSize += 1;
        tb.Measure(constraint);
    }
    tb.FontSize -= 1;
}

private void text_TextChanged(object sender, TextChangedEventArgs e)
{
    RecalcFontSize();
}

Try it, drag it around, change the text...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文