10

HTMLで行うことができるように、テキストブロックにワードラップの提案を提供する可能性はありますか、<SHY> (soft hyphen)または<WBR> (word break)さらに複雑で保守性が低いzero-width-space &#8203;

現時点では、Textblock は必要に応じて単語を分割し、次のような単語の折り返しで終了します。

スタック
オーバーフロー

私が欲しいのは:

スタックオーバー
フロー

または少なくとも:

スタックオーバー
フロー

必要なものを達成するための推奨される方法があれば、私に知らせてください。

4

2 に答える 2

4

true に設定TextBlock.IsHypenationEnabledすると、実際にはこれと同様のことが行われますが、タグを使用する場合は、次のような方法を使用できます。

    /// <summary>
    /// Adds break to a TextBlock according to a specified tag
    /// </summary>
    /// <param name="text">The text containing the tags to break up</param>
    /// <param name="tb">The TextBlock we are assigning this text to</param>
    /// <param name="tag">The tag, eg <br> to use in adding breaks</param>
    /// <returns></returns>
    public string WordWrap(string text, TextBlock tb, string tag)
    {
        //get the amount of text that can fit into the textblock
        int len = (int)Math.Round((2 * tb.ActualWidth / tb.FontSize));
        string original = text.Replace(tag, "");
        string ret = "";
        while (original.Length > len)
        {
            //get index where tag occurred
            int i = text.IndexOf(tag);
            //get index where whitespace occurred
            int j = original.IndexOf(" ");
            //does tag occur earlier than whitespace, then let's use that index instead!
            if (j > i && j < len)
                i = j;
            //if we usde index of whitespace, there is no need to hyphenate
            ret += (i == j) ? original.Substring(0, i) + "\n" : original.Substring(0, i) + "-\n";
            //if we used index of whitespace, then let's remove the whitespace
            original = (i == j) ? original.Substring(i + 1) : original.Substring(i);
            text = text.Substring(i + tag.Length);
        }
        return ret + original;
    }

このようにして、次のように言うことができます。

textBlock1.Text = WordWrap("StackOver<br>Flow For<br>Ever", textBlock1, "<br>");

これは出力されます:

ちょうどテスト済み

ただし、タグなしで IsHyphenated のみを使用すると、次のようになります。

ハイプネーテッド シナリオ 1

その間:

textBlock1.Text = WordWrap("StackOver<br>Flow In<br> U", textBlock1, "<br>");

出力します:

ここに <brs> を追加しません

タグなしの IsHyphenated :

IsHyphenated シナリオ 2

編集:フォント サイズを小さくすると、投稿した最初のコードが、ユーザー指定の区切りに空白が発生する場所に区切りを追加することを好ま ないことがわかりました。

于 2012-07-13T07:06:02.347 に答える
3

TextFormatterをカスタムと組み合わせて使用​​してTextSource、テキストの分割方法と折り返し方法を制御します。

TextSource からクラスを派生させる必要があり、実装でコンテンツ/文字列を分析し、ラッピング ルールを提供する必要がありTextEndOfLineますTextCharacters

の実装に役立つ例TextSourceは次のとおりです。

非常に高度な例については、「AvalonEdit」も使用しています。

GlyphRunリッチ フォーマットが不要かどうかを調べることもできます。

于 2012-07-12T13:27:50.023 に答える