WPF TextFormatter 中的缩进

发布于 2024-09-16 14:10:08 字数 642 浏览 1 评论 0原文

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

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

没有任何内容的常规文本
缩进。
    本段有统一
    缩进,所以一切都是
    好的。

但我还需要这个:
    约翰:本段有一个
         不同的缩进
         在第一行。
    乔:所以我不知道怎么办
          实现这一目标。

我找到了 ParagraphIndent 和 FirstLineInParagraph 属性,但我不知道它们是如何工作的,或者是否有用。

提前致谢!! 何塞

I'm making a WPF text-editor using TextFormatter. I need to indent some paragraphs, so I'm using the Indent property from the TextParagraphProperties class.

This works great in this scenario:

Regular text without any
indentation.
    This paragraph has a uniform
    indentation so everything is
    ok.

But I also need this:
    John: This paragraph has a
            diferent indentation
            in the first line.
    Joe: So I don't know how
            to make this happen.

I found the ParagraphIndent and FirstLineInParagraph properties, but I don't know how they works, or if then would be usefull.

Thanks in advance!!
Jose

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北恋 2024-09-23 14:10:08

Jose——

希望这个代码框架能有所帮助...我假设您是从 MSDN 上的高级文本格式化项目开始的,遗憾的是,该项目并不是很先进。下面是您可以引入到 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;
        }
    }
}

Jose --

Hopefully this code skeleton will help... I assume that you started from the Advanced Text Formatting project on MSDN, which, sadly is not very advanced. Here is a code fragment you can introduce to 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();
        }

You'll need to create the class ProxyTextParagraphProperties, which descends from TextParagraphProperties. Here's a snip of what I did to test this:

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;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文