gpt4 book ai didi

c# - 如何检查文本框中的最后一行是否只有一个单词?

转载 作者:太空狗 更新时间:2023-10-29 22:11:44 26 4
gpt4 key购买 nike

在下面的代码中,我将一个字符串分配给文本框。文本框文本被换行,因此如果单词不能放在同一行中,它们将移到下一行。

C#:

textbox.Text = "Norma went to bed. It was eleven o'clock. She turned out the light. She lay in bed. It was dark. It was quiet. She couldn't sleep. She closed her eyes.";

XAML:

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

现在,在上面的示例中,可能会发生在文本框的最后一行,由于换行,单词“Eyes”是唯一的单词。如果最后一行只有一个词,我想减小字体大小,以便最后一行至少有两个词。

所以,简而言之,最后一行不应该只有一个词。它可能有两个或多个单词。

示例:(错误)

Norma went to bed. It was
eleven o'clock. She turned
out the light. She lay in
bed. It was dark. It was
quiet. She couldn't
sleep. She closed her
eyes.

示例:(右)

Norma went to bed. It was
eleven o'clock. She turned
out the light. She lay in
bed. It was dark. It was
quiet. She couldn't
sleep. She closed her eyes.

我不是在问如何增加/减少字体以及应该在什么基础上计算新的字体大小。这是我需要弄清楚的不同问题。但是我的问题的第一步是找出最后一行中是否有一个单词。

如何检查文本框的最后一行是否只有一个词?

最佳答案

您可以使用 LineCount属性(property)和GetLineText方法找到最后一行文本,然后检查它是否包含空格(或任何其他所需的分隔符)。

关于c# - 如何检查文本框中的最后一行是否只有一个单词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48949654/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com