1

我正在使用 TextFormatter 制作 WPF 文本编辑器。我需要缩进一些段落,所以我使用 TextParagraphProperties 类中的 Indent 属性。

这在这种情况下效果很好:

没有任何
缩进的常规文本。
    这一段有一个统一的
    缩进,所以一切都
    很好。

但我也需要这个:
    约翰:这一段             在第一行有不同的
            缩进。     乔:所以我不知道如何             做到这一点。 我找到了 ParagraphIndent 和 FirstLineInParagraph 属性,但我不知道它们是如何工作的,或者是否有用。




提前致谢!!何塞

4

1 回答 1

1

何塞——

希望这个代码框架会有所帮助……我假设您是从 MSDN 上的 Advanced Text Formatting 项目开始的,遗憾的是它不是很先进。这是您可以引入 MainWindow.Xaml.cs 的代码片段:

        TextParagraphProperties textParagraphProperties = new GenericTextParagraphProperties(TextAlignment.Left,
                                                                                             false);
        TextRunCache textRunCache = new TextRunCache();

        // By default, this is the first line of a paragrph
        bool firstLine = true;
        while (textStorePosition < _textStore.Length)
        {
            // Create a textline from the text store using the TextFormatter object.
            TextLine myTextLine = formatter.FormatLine(
                _textStore,
                textStorePosition,
                96 * 6,

                // ProxyTextParagraphProperties will return a different value for the Indent property,
                // depending on whether firstLine is true or not.
                new ProxyTextParagraphProperties(textParagraphProperties, firstLine),
                lastLineBreak,
                textRunCache);

            // Draw the formatted text into the drawing context.
            Debug.WriteLine("linePosition:" + linePosition);
            myTextLine.Draw(dc, linePosition, InvertAxes.None);

            // Update the index position in the text store.
            textStorePosition += myTextLine.Length;

            // Update the line position coordinate for the displayed line.
            linePosition.Y += myTextLine.Height;

            // Figure out if the next line is the first line of a paragraph
            var textRunSpans = myTextLine.GetTextRunSpans();
            firstLine = (textRunSpans.Count > 0) && (textRunSpans[textRunSpans.Count - 1].Value is TextEndOfParagraph);

            lastLineBreak = myTextLine.GetTextLineBreak();
        }

您将需要创建类 ProxyTextParagraphProperties,该类继承自 TextParagraphProperties。这是我为测试它所做的一个片段:

class ProxyTextParagraphProperties : TextParagraphProperties
{
    private readonly TextParagraphProperties _paragraphProperties;
    private readonly bool _firstLineInParagraph;

    public ProxyTextParagraphProperties(TextParagraphProperties textParagraphProperties, bool firstLineInParagraph)
    {
        _paragraphProperties = textParagraphProperties;
        _firstLineInParagraph = firstLineInParagraph;
    }

    public override FlowDirection FlowDirection
    {
        get { return _paragraphProperties.FlowDirection; }
    }

    // implement the same as above for all the other properties...

    // But we need to handle this one specially...
    public override double Indent
    {
        get
        {
            return _firstLineInParagraph ? _paragraphProperties.Indent : 0;
        }
    }
}
于 2010-10-14T17:18:42.023 回答