In the code below, I am assigning a string to text box. The text box text is wrapped, so words will be shifted to the next line if they cannot fit in same line.
在下面的代码中,我将字符串分配给文本框。文本框文本被包装,因此如果单词不能放在同一行中,则单词将移动到下一行。
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" />
Now, with the example above, it may happen that on the last line in the text box the word "Eyes" is the only word due to wrapping. If the last line has only one word, I would like to decrease the font size so that the last line has at least two words.
现在,通过上面的例子,可能会发生在文本框的最后一行,“Eyes”这个词是由于包装而唯一的单词。如果最后一行只有一个单词,我想减小字体大小,以便最后一行至少有两个单词。
So, in short, the last line should never have one word only. It may have two or more words.
所以,简而言之,最后一行不应该只有一个单词。它可能有两个或更多的单词。
Example: (Wrong)
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.
Example: (Right)
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.
I am not asking how to increase/decrease the font and on what basis the new font size should be calculated. That is different question that I need to figure out. But first step of my problem is to find out if there is a single word in the last line.
我不是问如何增加/减少字体以及在什么基础上应该计算新的字体大小。这是我需要弄清楚的另一个问题。但问题的第一步是找出最后一行中是否有一个单词。
How do I check if the last line in a text box has only one word?
如何检查文本框中的最后一行是否只有一个单词?
6
Perhaps a simple solution is to add a non-breaking space between the last words, ie:
也许一个简单的解决方案是在最后一个单词之间添加一个不间断的空格,即:
"...She closed her\u00a0eyes."
8
You can use the LineCount
property and the GetLineText
method to find the last line of text and then check if it contains a space (or any other desired separator) in it.
您可以使用LineCount属性和GetLineText方法查找最后一行文本,然后检查其中是否包含空格(或任何其他所需的分隔符)。
本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2018/02/23/2819387cb42adc062e1ff9b094979118.html。