【问题标题】:WPF Text overflowWPF 文本溢出
【发布时间】:2010-09-03 23:03:12
【问题描述】:

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

ps- 文本块是在运行时在 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);

【问题讨论】:

  • 溢出是什么意思。你有恒定宽度的文本块吗?
  • 你只是想要你的文字Justified吗?
  • 我想要做的是,当文本超出文本块的边界时(带有换行),而不是切断,剩余的文本然后被放入第二个文本块

标签: c# wpf textblock word-wrap


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    一种方法是创建一个自定义控件,您可以在其中通过DrawingContext 进行渲染。有了这个,您可以准确计算文本将使用的大小,从而计算剪切位置。

    使用这样的控件,您可以定义一个溢出属性,为每个控件返回未显示的文本并将其设置为同级控件的源。

    当然,这不是一项简单的工作,但这是一种可行的方法。您也可以从 TextBlock 派生,然后在默认处理逻辑中计算文本并添加 DP 以提供溢出文本。

    以下链接可能有助于解决上述问题:

    FormattedText

    FormattedText.Height

    【讨论】:

    • "使用这样的控件,您可以定义一个溢出属性,为每个控件返回未显示的文本并将其设置为同级控件的源。"
    • 抱歉,本来是要发表评论的,但我不确定该怎么做。我会在上面解释我做得更好的地方
    猜你喜欢
    • 2017-11-19
    • 2018-06-05
    • 2014-05-22
    • 1970-01-01
    • 2013-03-31
    • 2021-06-06
    • 2021-11-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多