在下面的代码中,我将字符串分配给文本框。文本框文本被包装,因此如果单词不能放在同一行中,则单词将移动到下一行。
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" />
现在,通过上面的示例,可能会在文本框的最后一行出现“&#34; Eyes&#34;是包装的唯一单词。如果最后一行只有一个单词,我想减小字体大小,以便最后一行至少有两个单词。
因此,简而言之,最后一行不应该只有一个单词。它可能有两个或更多的单词。
示例:(错误)
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.
我不是问如何增加/减少字体以及在什么基础上应该计算新的字体大小。这是我需要弄清楚的另一个问题。但问题的第一步是找出最后一行中是否有一个单词。
如何检查文本框中的最后一行是否只有一个单词?
答案 0 :(得分:8)
您可以使用LineCount
属性和GetLineText
方法查找最后一行文本,然后检查其中是否包含空格(或任何其他所需的分隔符)。
答案 1 :(得分:6)
也许一个简单的解决方案是在最后一个单词之间添加non-breaking space,即:
"...She closed her\u00a0eyes."