带有包装的TextBlock的动态字体大小

Ost*_*tan 4 c# xaml windows-8

我有一个固定大小的TextBlock包装文本.有时短有时长.

如果文本变长,它就不会完全像这样显示

在此输入图像描述

如何使Fontsize灵活,使文本适合静态大小的TextBox?

Zol*_*oth 11

我的解决方案如下:

将fontsize设置为一个值,您不希望任何更大的值.更改字体大小或更改内容时,TextBlock的ActualHeight会更改.我基于此构建了解决方案.您应该为SizeChanged事件创建一个事件处理程序,并将以下代码写入其中.

private void MyTextBlock_SizeChanged(object sender, SizeChangedEventArgs e)
{
    double desiredHeight = 80; // Here you'll write the height you want the text to use

    if (this.MyTextBlock.ActualHeight > desiredHeight)
    {
        // You want to know, how many times bigger the actual height is, than what you want to have.
        // The reason for Math.Sqrt() is explained below in the text.
        double fontsizeMultiplier = Math.Sqrt(desiredHeight / this.MyTextBlock.ActualHeight);

        // Math.Floor() can be omitted in the next line if you don't want a very tall and narrow TextBox.
        this.MyTextBlock.FontSize = Math.Floor(this.MyTextBlock.FontSize * fontsizeMultiplier);
    }

    this.MyTextBlock.Height = desiredHeight; // ActualHeight will be changed if the text is too big, after the text was resized, but in the end you want the box to be as big as the desiredHeight.
}
Run Code Online (Sandbox Code Playgroud)

我使用Math.Sqrt()的原因是,如果你将字体大小设置为之前的一半大,那么字体将使用的区域将是大小的四分之一,之前(因为它变为一半)宽和高一半高).你显然想要保持TextBox的宽度,只改变它的高度.

如果幸运的话,在执行此方法一次后,字体大小将是合适的.但是,根据在字体大小更改后重新包装的文本,您可能会"非常不走运",文本将比您希望的长一行.幸运的是,事件处理程序将再次被调用(因为您更改了字体大小),如果它仍然太大,将再次调整大小.

我试过了,它很快,结果看起来不错.但是,我可以想象,在一个非常不幸的文本和高度选择中,在几次迭代后会达到正确的字体大小.这就是我使用Math.Floor()的原因.总而言之,如果字体大小在12.34或12的末尾并不重要,这样我就不会担心"不吉利"的文本,渲染时间太长.但是我认为如果你不想拥有一个包含大量文本的高文本框(如2000像素),可以省略Math.Floor().