如何在不适合文本框时减小字体大小?

时间:2018-02-21 22:42:25

标签: c# wpf

按照我的代码:( XAML

<TextBox
    Text="Texto"
    SelectionBrush="#FF54FF50"
    x:Name="textbox_principal" 
    Margin="10,53,0,0" 
    FontSize="24" 
    HorizontalAlignment="Left" 
    Width="341" 
    Height="285" 
    VerticalAlignment="Top" 
    TextChanged="Textbox_principal_TextChanged"
    IsReadOnly="True" 
    CaretBrush="Black" 
    BorderBrush="Black" 
    Foreground="Black" 
    FontWeight="Bold" Grid.ColumnSpan="2" Padding="0,5,0,0" 
    HorizontalContentAlignment="Center" 
    VerticalContentAlignment="Center" 
    TextWrapping="Wrap" 
    VerticalScrollBarVisibility="Auto"/>

如果文字不合适,请减少字体,按照我的代码 C#

private void Textbox_principal_TextChanged(object sender, TextChangedEventArgs e)
{
   //check if the text is large, then decrease font size
}

有关如何调整文本框中字体大小的任何解决方案? 我不想要textblock的解决方案,只需要textbox

我想设置默认大小24,如果文字不适合textbox,我想减少。

更新:(以下代码适用于winforms)

private void label1_TextChanged(object sender, EventArgs e)
{
    Graphics g = CreateGraphics();
    float p = 20;
    Font f = new Font(((Label)sender).Font.Name, p);
    SizeF s = g.MeasureString(((Label)sender).Text, f);

    while (s.Width >= ((Label)sender).Width - 20)
    {
        p = p - 0.1f;
        f = new Font(((Label)sender).Font.Name, p);
        s = g.MeasureString(((Label)sender).Text, f);
    }
    ((Label)sender).Font = f;
}

结果 - Winforms:

enter image description here

2 个答案:

答案 0 :(得分:2)

如果您不需要知道实际的字体大小,可以修改TextBox的控件模板,添加ViewBox,无需任何代码即可完成。

ViewBox会自动调整其内容的大小,使其符合父级的范围。

如果您只想将其应用于TextBox个实例的子集,请将Style x:Key更改为x:Key="ResizingTextBox",并通过设置将其应用于特定控件他们的style属性。

Demo GIF Resizes with typing too!

我使用了以下Style(请注意ViewBox):

<Style x:Key="{x:Type TextBox}" TargetType="{x:Type TextBoxBase}">
            <Setter Property="SnapsToDevicePixels" Value="True" />
            <Setter Property="OverridesDefaultStyle" Value="True" />
            <Setter Property="KeyboardNavigation.TabNavigation" Value="None" />
            <Setter Property="FocusVisualStyle" Value="{x:Null}" />
            <Setter Property="MinWidth" Value="120" />
            <Setter Property="MinHeight" Value="20" />
            <Setter Property="AllowDrop" Value="true" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TextBoxBase}">
                        <Border
                            Name="Border"
                            Padding="2"
                            Background="{StaticResource WindowBackgroundBrush}"
                            BorderBrush="{StaticResource SolidBorderBrush}"
                            BorderThickness="1"
                            CornerRadius="2">
                            <Viewbox>
                                <ScrollViewer x:Name="PART_ContentHost" Margin="0" />
                            </Viewbox>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsEnabled" Value="False">
                                <Setter TargetName="Border" Property="Background" Value="{StaticResource DisabledBackgroundBrush}" />
                                <Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource DisabledBackgroundBrush}" />
                                <Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

还需要在某处定义以下资源:

    <LinearGradientBrush x:Key="NormalBrush" StartPoint="0,0" EndPoint="0,1">
        <GradientBrush.GradientStops>
            <GradientStopCollection>
                <GradientStop Offset="0.0" Color="#FFF" />
                <GradientStop Offset="1.0" Color="#CCC" />
            </GradientStopCollection>
        </GradientBrush.GradientStops>
    </LinearGradientBrush>

    <LinearGradientBrush x:Key="HorizontalNormalBrush" StartPoint="0,0" EndPoint="1,0">
        <GradientBrush.GradientStops>
            <GradientStopCollection>
                <GradientStop Offset="0.0" Color="#FFF" />
                <GradientStop Offset="1.0" Color="#CCC" />
            </GradientStopCollection>
        </GradientBrush.GradientStops>
    </LinearGradientBrush>

    <LinearGradientBrush x:Key="LightBrush" StartPoint="0,0" EndPoint="0,1">
        <GradientBrush.GradientStops>
            <GradientStopCollection>
                <GradientStop Offset="0.0" Color="#FFF" />
                <GradientStop Offset="1.0" Color="#EEE" />
            </GradientStopCollection>
        </GradientBrush.GradientStops>
    </LinearGradientBrush>

    <LinearGradientBrush x:Key="HorizontalLightBrush" StartPoint="0,0" EndPoint="1,0">
        <GradientBrush.GradientStops>
            <GradientStopCollection>
                <GradientStop Offset="0.0" Color="#FFF" />
                <GradientStop Offset="1.0" Color="#EEE" />
            </GradientStopCollection>
        </GradientBrush.GradientStops>
    </LinearGradientBrush>

    <LinearGradientBrush x:Key="DarkBrush" StartPoint="0,0" EndPoint="0,1">
        <GradientBrush.GradientStops>
            <GradientStopCollection>
                <GradientStop Offset="0.0" Color="#FFF" />
                <GradientStop Offset="1.0" Color="#AAA" />
            </GradientStopCollection>
        </GradientBrush.GradientStops>
    </LinearGradientBrush>

    <LinearGradientBrush x:Key="PressedBrush" StartPoint="0,0" EndPoint="0,1">
        <GradientBrush.GradientStops>
            <GradientStopCollection>
                <GradientStop Offset="0.0" Color="#BBB" />
                <GradientStop Offset="0.1" Color="#EEE" />
                <GradientStop Offset="0.9" Color="#EEE" />
                <GradientStop Offset="1.0" Color="#FFF" />
            </GradientStopCollection>
        </GradientBrush.GradientStops>
    </LinearGradientBrush>

    <SolidColorBrush x:Key="DisabledForegroundBrush" Color="#888" />

    <SolidColorBrush x:Key="DisabledBackgroundBrush" Color="#EEE" />

    <SolidColorBrush x:Key="WindowBackgroundBrush" Color="#FFF" />

    <SolidColorBrush x:Key="SelectedBackgroundBrush" Color="#DDD" />

    <!--  Border Brushes  -->

    <LinearGradientBrush x:Key="NormalBorderBrush" StartPoint="0,0" EndPoint="0,1">
        <GradientBrush.GradientStops>
            <GradientStopCollection>
                <GradientStop Offset="0.0" Color="#CCC" />
                <GradientStop Offset="1.0" Color="#444" />
            </GradientStopCollection>
        </GradientBrush.GradientStops>
    </LinearGradientBrush>

    <LinearGradientBrush x:Key="HorizontalNormalBorderBrush" StartPoint="0,0" EndPoint="1,0">
        <GradientBrush.GradientStops>
            <GradientStopCollection>
                <GradientStop Offset="0.0" Color="#CCC" />
                <GradientStop Offset="1.0" Color="#444" />
            </GradientStopCollection>
        </GradientBrush.GradientStops>
    </LinearGradientBrush>

    <LinearGradientBrush x:Key="DefaultedBorderBrush" StartPoint="0,0" EndPoint="0,1">
        <GradientBrush.GradientStops>
            <GradientStopCollection>
                <GradientStop Offset="0.0" Color="#777" />
                <GradientStop Offset="1.0" Color="#000" />
            </GradientStopCollection>
        </GradientBrush.GradientStops>
    </LinearGradientBrush>

    <LinearGradientBrush x:Key="PressedBorderBrush" StartPoint="0,0" EndPoint="0,1">
        <GradientBrush.GradientStops>
            <GradientStopCollection>
                <GradientStop Offset="0.0" Color="#444" />
                <GradientStop Offset="1.0" Color="#888" />
            </GradientStopCollection>
        </GradientBrush.GradientStops>
    </LinearGradientBrush>

    <SolidColorBrush x:Key="DisabledBorderBrush" Color="#AAA" />

    <SolidColorBrush x:Key="SolidBorderBrush" Color="#888" />

    <SolidColorBrush x:Key="LightBorderBrush" Color="#AAA" />

    <!--  Miscellaneous Brushes  -->
    <SolidColorBrush x:Key="GlyphBrush" Color="#444" />

基本模板和资源来自MSDN

这是完整的XAML,在一个地方作为演示:

<Window
    x:Class="FontTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="clr-namespace:FontTest"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Title="MainWindow"
    Width="525"
    Height="350"
    mc:Ignorable="d">
    <Window.Resources>
        <LinearGradientBrush x:Key="NormalBrush" StartPoint="0,0" EndPoint="0,1">
            <GradientBrush.GradientStops>
                <GradientStopCollection>
                    <GradientStop Offset="0.0" Color="#FFF" />
                    <GradientStop Offset="1.0" Color="#CCC" />
                </GradientStopCollection>
            </GradientBrush.GradientStops>
        </LinearGradientBrush>

        <LinearGradientBrush x:Key="HorizontalNormalBrush" StartPoint="0,0" EndPoint="1,0">
            <GradientBrush.GradientStops>
                <GradientStopCollection>
                    <GradientStop Offset="0.0" Color="#FFF" />
                    <GradientStop Offset="1.0" Color="#CCC" />
                </GradientStopCollection>
            </GradientBrush.GradientStops>
        </LinearGradientBrush>

        <LinearGradientBrush x:Key="LightBrush" StartPoint="0,0" EndPoint="0,1">
            <GradientBrush.GradientStops>
                <GradientStopCollection>
                    <GradientStop Offset="0.0" Color="#FFF" />
                    <GradientStop Offset="1.0" Color="#EEE" />
                </GradientStopCollection>
            </GradientBrush.GradientStops>
        </LinearGradientBrush>

        <LinearGradientBrush x:Key="HorizontalLightBrush" StartPoint="0,0" EndPoint="1,0">
            <GradientBrush.GradientStops>
                <GradientStopCollection>
                    <GradientStop Offset="0.0" Color="#FFF" />
                    <GradientStop Offset="1.0" Color="#EEE" />
                </GradientStopCollection>
            </GradientBrush.GradientStops>
        </LinearGradientBrush>

        <LinearGradientBrush x:Key="DarkBrush" StartPoint="0,0" EndPoint="0,1">
            <GradientBrush.GradientStops>
                <GradientStopCollection>
                    <GradientStop Offset="0.0" Color="#FFF" />
                    <GradientStop Offset="1.0" Color="#AAA" />
                </GradientStopCollection>
            </GradientBrush.GradientStops>
        </LinearGradientBrush>

        <LinearGradientBrush x:Key="PressedBrush" StartPoint="0,0" EndPoint="0,1">
            <GradientBrush.GradientStops>
                <GradientStopCollection>
                    <GradientStop Offset="0.0" Color="#BBB" />
                    <GradientStop Offset="0.1" Color="#EEE" />
                    <GradientStop Offset="0.9" Color="#EEE" />
                    <GradientStop Offset="1.0" Color="#FFF" />
                </GradientStopCollection>
            </GradientBrush.GradientStops>
        </LinearGradientBrush>

        <SolidColorBrush x:Key="DisabledForegroundBrush" Color="#888" />

        <SolidColorBrush x:Key="DisabledBackgroundBrush" Color="#EEE" />

        <SolidColorBrush x:Key="WindowBackgroundBrush" Color="#FFF" />

        <SolidColorBrush x:Key="SelectedBackgroundBrush" Color="#DDD" />

        <!--  Border Brushes  -->

        <LinearGradientBrush x:Key="NormalBorderBrush" StartPoint="0,0" EndPoint="0,1">
            <GradientBrush.GradientStops>
                <GradientStopCollection>
                    <GradientStop Offset="0.0" Color="#CCC" />
                    <GradientStop Offset="1.0" Color="#444" />
                </GradientStopCollection>
            </GradientBrush.GradientStops>
        </LinearGradientBrush>

        <LinearGradientBrush x:Key="HorizontalNormalBorderBrush" StartPoint="0,0" EndPoint="1,0">
            <GradientBrush.GradientStops>
                <GradientStopCollection>
                    <GradientStop Offset="0.0" Color="#CCC" />
                    <GradientStop Offset="1.0" Color="#444" />
                </GradientStopCollection>
            </GradientBrush.GradientStops>
        </LinearGradientBrush>

        <LinearGradientBrush x:Key="DefaultedBorderBrush" StartPoint="0,0" EndPoint="0,1">
            <GradientBrush.GradientStops>
                <GradientStopCollection>
                    <GradientStop Offset="0.0" Color="#777" />
                    <GradientStop Offset="1.0" Color="#000" />
                </GradientStopCollection>
            </GradientBrush.GradientStops>
        </LinearGradientBrush>

        <LinearGradientBrush x:Key="PressedBorderBrush" StartPoint="0,0" EndPoint="0,1">
            <GradientBrush.GradientStops>
                <GradientStopCollection>
                    <GradientStop Offset="0.0" Color="#444" />
                    <GradientStop Offset="1.0" Color="#888" />
                </GradientStopCollection>
            </GradientBrush.GradientStops>
        </LinearGradientBrush>

        <SolidColorBrush x:Key="DisabledBorderBrush" Color="#AAA" />

        <SolidColorBrush x:Key="SolidBorderBrush" Color="#888" />

        <SolidColorBrush x:Key="LightBorderBrush" Color="#AAA" />

        <!--  Miscellaneous Brushes  -->
        <SolidColorBrush x:Key="GlyphBrush" Color="#444" />

        <SolidColorBrush x:Key="LightColorBrush" Color="#DDD" />
        <Style x:Key="{x:Type TextBox}" TargetType="{x:Type TextBoxBase}">
            <Setter Property="SnapsToDevicePixels" Value="True" />
            <Setter Property="OverridesDefaultStyle" Value="True" />
            <Setter Property="KeyboardNavigation.TabNavigation" Value="None" />
            <Setter Property="FocusVisualStyle" Value="{x:Null}" />
            <Setter Property="MinWidth" Value="120" />
            <Setter Property="MinHeight" Value="20" />
            <Setter Property="AllowDrop" Value="true" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TextBoxBase}">
                        <Border
                            Name="Border"
                            Padding="2"
                            Background="{StaticResource WindowBackgroundBrush}"
                            BorderBrush="{StaticResource SolidBorderBrush}"
                            BorderThickness="1"
                            CornerRadius="2">
                            <Viewbox>
                                <ScrollViewer x:Name="PART_ContentHost" Margin="0" />
                            </Viewbox>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsEnabled" Value="False">
                                <Setter TargetName="Border" Property="Background" Value="{StaticResource DisabledBackgroundBrush}" />
                                <Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource DisabledBackgroundBrush}" />
                                <Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

    </Window.Resources>
    <Grid>
        <TextBox
            x:Name="textbox_principal"
            Padding="0,5,0,0"
            HorizontalAlignment="Left"
            VerticalAlignment="Top"
            HorizontalContentAlignment="Center"
            VerticalContentAlignment="Center"
            BorderBrush="Black"
            CaretBrush="Black"
            FontSize="24"
            FontWeight="Bold"
            Foreground="Black"
            SelectionBrush="#FF54FF50"
            Text="Texto"
            TextWrapping="Wrap"
            VerticalScrollBarVisibility="Auto" />
    </Grid>
</Window>

多线似乎也正常工作。您只需使用&#x0a;嵌入换行符,如下所示:

Text="This is line 1&#x0a;This is line 2!"

如果您添加AcceptsReturn="True",它甚至会在您输入时有效。

enter image description here

答案 1 :(得分:1)

我找到了解决方案。

private void Textbox_TextChanged(object sender, TextChangedEventArgs e)
{            
    textbox.FontSize = 24;
    textbox.UpdateLayout();

    ScrollViewer sv = FindVisualChild<ScrollViewer>(textbox);

    if (sv != null)
    {
        Visibility VerticalScrollbarVisibility = sv.ComputedVerticalScrollBarVisibility;
        if (VerticalScrollbarVisibility == Visibility.Visible)
        {
            while (VerticalScrollbarVisibility == Visibility.Visible)
            {
                textbox.FontSize = textbox.FontSize - 1;
                textbox.UpdateLayout();
                VerticalScrollbarVisibility = sv.ComputedVerticalScrollBarVisibility;
            }
        }
    }            
}