c# - 当文本框不适合时如何减小字体大小?

标签 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

最佳答案

如果你不需要知道实际的字体大小,你可以修改控件模板为一个TextBox,添加一个ViewBox,不需要任何代码。

ViewBox 自动调整其内容的大小,使其适合父级的边界。

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

Demo GIF Resizes with typing too!

我使用了以下样式(注意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>

多线似乎也能正常工作。你只需要用 嵌入换行符,就像这样:

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

如果您添加 AcceptsReturn="True",它甚至可以在您键入时工作。

enter image description here

关于c# - 当文本框不适合时如何减小字体大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48916775/

相关文章:

c# - 我可以禁用 Xceed DoubleUpDown 控制箭头吗?

c# - 矩形不改变颜色WPF

c# - 当另一个控件与它重叠时隐藏控件

WPF ItemTemplateSelector 到单个项目

c# - DataTable - foreach 行,第一行除外

c# - .Net CLR 如何在内部实现 "Interface"?

c# - 将 double[,] 转换为 IntPtr C#

c# - MVC4包安装错误

c# - 静态类的竞争条件?

WPF TabControl 和 DataTemplates