2010-09-03 67 views
1

我目前正试图让它在我接受一个字符串时填充第一个文本块直到它溢出,然后它应该在文本块2中开始。目前,我已将它放到字符串所在的位置在最后一个单词的最后一部分切成两部分,然后打到我认为可以放入文本块的最大字符,而下半部分放入2,但是我遇到的问题是它从来没有真正地有可能找出切断文本的位置,因为当它包裹时,留下的空间占据不同的大小。所以我剩下的文本块1末尾有一些文本被截断,使得它看起来像两个词之间缺少一些词。有没有办法以编程方式查找文本块上的溢出?WPF文本溢出

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

这就是我所做的。我把我的描述,并尝试将其适合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); 
+0

你是什么意思的溢出。你有固定宽度的文本块? – 2010-09-04 09:10:46

+0

你只是想要你的文字[对齐](http://en.wikipedia.org/wiki/Justification_%28typesetting%29)? – 2010-09-04 10:11:54

+0

我想要做的是,当文本超出文本块的边界(与包装),而不是切断,其余文本然后被放到第二个文本块 – Andrew 2010-09-16 05:20:10

回答

1

一种方法是在DrawingContext上创建自定义控件,并在其上执行渲染。有了这个,你可以精确地计算出文本将要使用的大小,并因此计算出裁切位置。

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

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

下面的链接可能是有用做上述溶液:

FormattedText

FormattedText.Height

+0

“有了这样的控制,你可以定义一个溢出属性,为每个控件返回未显示的文本,并将其设置为兄弟控件的源。“ – Andrew 2010-09-16 11:52:51

+0

对不起,本来只是一个评论,但我不确定如何去做这件事。我会解释我在上面做得更好 – Andrew 2010-09-16 11:53:34

3

看看与TextBlock的或许使你的代码simplier相关的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>