WPF 文本块缩放和换行

发布于 2024-09-25 02:39:00 字数 386 浏览 1 评论 0原文

我有一个文本块,需要

  1. 在给定空间内以可能的最大字体大小显示文本(字符较少,字母较大,反之亦然)
  2. 将文本包装在给定空间内。

我尝试在视图框中使用文本框,如下所示,但如果我不指定文本块宽度和高度,则文本换行不起作用。如果我确实将宽度和高度指定为与视图框相同的大小,显然不会发生缩放。

<Viewbox Stretch="Fill" Width="100" Height="100">
<TextBlock TextWrapping="Wrap"/>
</Viewbox>

还有其他方法可以实现这一目标吗?或者我应该考虑编写一个算法来根据文本量手动增加字体大小? 任何帮助表示赞赏。

I have a textblock which needs to

  1. show text in the maximum font size possible within the given space (less characters bigger letters and vice-versa)
  2. wrap the text within the given space.

I tried using a textbox inside a viewbox like below but the text-wrapping doesn't work if i don't specify the textblock width and height. If i do specify the width and height to the same size as the viewbox, obviously zooming doesn't happen.

<Viewbox Stretch="Fill" Width="100" Height="100">
<TextBlock TextWrapping="Wrap"/>
</Viewbox>

Is there any other way to acheive this? Or should i think about writing an algorithm to increase font size manually based on the amount of text?
Any help is appreciated.

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

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

发布评论

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

评论(1

灯下孤影 2024-10-02 02:39:00

试试这个代码。

XAML:

<TextBlock x:Name="textBlock"
           Text="Something text"
           TextWrapping="Wrap"
           FontSize="1"
           Width="100"
           Opacity="0" />

隐藏代码:

while (textBlock.ActualHeight <= 100)
{
    textBlock.FontSize += 0.1;
    textBlock.UpdateLayout();
}
textBlock.FontSize -= 0.1;
textBlock.Opacity = 1;

Try this code.

XAML:

<TextBlock x:Name="textBlock"
           Text="Something text"
           TextWrapping="Wrap"
           FontSize="1"
           Width="100"
           Opacity="0" />

Code behind:

while (textBlock.ActualHeight <= 100)
{
    textBlock.FontSize += 0.1;
    textBlock.UpdateLayout();
}
textBlock.FontSize -= 0.1;
textBlock.Opacity = 1;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文