我正在使用TextFormatter制作WPF文本编辑器。我需要缩进一些段落,所以我使用TextParagraphProperties类中的Indent属性。
在这种情况下,这很有效:
没有任何
的常规文本
压痕。
本段有统一的
缩进所以一切都是
ok了。
但我也需要这个:
约翰:这一段有一个
不同的缩进
在第一行。
乔:所以我不知道如何
实现这一目标
我找到了ParagraphIndent和FirstLineInParagraph属性,但我不知道它们是如何工作的,或者当时是否有用。
提前致谢!! 何
答案 0 :(得分:1)
何塞 -
希望这段代码框架有所帮助......我假设你是从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;
}
}
}