gpt4 book ai didi

c# - WPF 文本溢出

转载 作者:太空宇宙 更新时间:2023-11-03 20:39:39 25 4
gpt4 key购买 nike

我目前正在尝试做到这一点,以便当我获取一个字符串时,它会填满第一个文本 block 直到溢出,然后它应该从文本 block 2 开始。目前,我将它放在字符串被分成两部分的位置在最后一个词的最后一个词到达我猜想是可以放入文本 block 1 的最大字符数之前的片段,而后半部分在 2 中,但我遇到的问题是它永远不可能真正计算出来在哪里截断文本,因为当它换行时,剩下的空间会占据不同的大小。所以我剩下的文本 block 1 在末尾有一些截断的文本,看起来两者之间缺少一些单词。有没有办法以编程方式找到文本 block 上的溢出?

ps- 文本 block 是在运行时在 C# 中创建的,而不是 wpf 标记。

这就是我的工作。我采用 myDescription 并尝试将其放入 myDesc[0],然后根据我估计的大小将其放入 [2]。问题是,如果我猜测大小阈值太大,它会在 myDesc[0] 上留下一个 ... 或截断词,如果我估计它太小,它就会有巨大的尴尬差距。没有我切断的号码也没有。

TextBlock[] myDesc = new TextBlock[2];
string myDescription = infoLoader.games[gameID].description[currentLanguage];
string[] myWords = myDescription.Split(' ');

string firstPart = "";
string secondPart = "";


int currentWord = 0;


// New and improved way
int currentLine = 0;
int charsInLine = 0;
while (currentWord < myWords.Length)
{
// Determine the size of the word based on the number of characters and size of certain characters in it.
int myWLength = myWords[currentWord].Length;
int iCount = 0;
for (int i = 0; i < myWords[currentWord].Length; i++)
{
if (myWords[currentWord][i] == 'm' || myWords[currentWord][i] == 'M')
{
Console.Write("M or m. ");
myWLength++;
}
else if (myWords[currentWord][i] == 'i' || myWords[currentWord][i] == 'l' || myWords[currentWord][i] == 'I' || myWords[currentWord][i] == 'j' || myWords[currentWord][i] == 'í' || myWords[currentWord][i] == 't')
{
iCount++;
}
}
iCount = (iCount / 2);
myWLength -= iCount;
if (myWords[currentWord] == "SKIP")
{
firstPart += "\n";
currentLine++;
currentWord++;
}
else if (currentLine < 4)
{
// firstPart.
if (charsInLine + myWLength < 20)
{
// Add It.
firstPart += myWords[currentWord];
firstPart += " ";
charsInLine += myWLength;
charsInLine += 1;
currentWord++;

}
else
{
// New Line.
//firstPart += " " + currentLine + " ";
firstPart += "\n";
charsInLine = 0;
currentLine++;
}
} else if (currentLine < 6)
{
if (charsInLine + myWLength < 21)
{
// Add It.
firstPart += myWords[currentWord];
firstPart += " ";
charsInLine += myWLength;
charsInLine += 1;
currentWord++;

}
else
{
// New Line.
//firstPart += "\n";
charsInLine = 0;
currentLine++;
}
}
else
{
// secondPart.
secondPart += myWords[currentWord];
secondPart += " ";
currentWord++;
}
}

myDesc[0] = new TextBlock();
myDesc[0].Text = firstPart;
myDesc[0].TextWrapping = TextWrapping.Wrap;
myDesc[0].TextTrimming = TextTrimming.CharacterEllipsis;
myDesc[0].Background = descBGBrush;
myDesc[0].FontFamily = new FontFamily("Arial");
myDesc[0].FontSize = 12.0;
myDesc[0].Width = 118;
myDesc[0].Height = 83;
Canvas.SetLeft(myDesc[0], 132);
Canvas.SetTop(myDesc[0], 31);

myDesc[1] = new TextBlock();
myDesc[1].Text = secondPart;
myDesc[1].TextWrapping = TextWrapping.Wrap;
myDesc[1].Background = descBGBrush;
myDesc[1].FontSize = 12.0;
myDesc[1].FontFamily = new FontFamily("Arial");
myDesc[1].Width = 236;
myDesc[1].Height = 43;
Canvas.SetLeft(myDesc[1], 16);
Canvas.SetTop(myDesc[1], 115);

最佳答案

看看 TextWrapping与 TextBlock 关联的属性可能会使您的代码更简单。

<StackPanel>
<TextBlock Text="One line of text"/>
<TextBlock Width="50" TextWrapping="WrapWithOverflow" Text="One line of text"/>
<TextBlock Width="50" TextWrapping="Wrap" Text="One line of text"/>
</StackPanel>

关于c# - WPF 文本溢出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3640141/

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