包括@Peter Moore说的。为了强制文本正确呈现,还需要执行一个步骤。
VisualLine.cs
的底部是类,它负责绘制实际文字,但无权访问TextView
类。
修改构造函数以包含double lineSpacing
作为参数,并将textLine.Height
的所有实例乘以lineSpacing
。
在VisualLine.Render()
中,通过textView.LineSpacing
作为VisualLineDrawingVisual
的现在修改的构造函数的辅助参数。在这之后,所有东西都应该绘制正确
这里的修改后的代码的完整视图:
TextView.cs
public static readonly DependencyProperty LineSpacingProperty =
DependencyProperty.Register("LineSpacing", typeof(double), typeof(TextView),
new FrameworkPropertyMetadata(1.0));
public double LineSpacing {
get { return (double) GetValue(LineSpacingProperty); }
set { SetValue(LineSpacingProperty, value); }
}
VisualLine.cs
线269
internal void SetTextLines(List<TextLine> textLines) {
this.textLines = textLines.AsReadOnly();
Height = 0;
foreach (TextLine line in textLines)
Height += line.Height * textView.LineSpacing;
}
线335
public double GetTextLineVisualYPosition(TextLine textLine, VisualYPosition yPositionMode) {
if (textLine == null)
throw new ArgumentNullException("textLine");
double pos = VisualTop;
foreach (TextLine tl in TextLines) {
if (tl == textLine) {
switch (yPositionMode) {
case VisualYPosition.LineTop:
return pos;
case VisualYPosition.LineMiddle:
return pos + tl.Height/2 * textView.LineSpacing;
case VisualYPosition.LineBottom:
return pos + tl.Height * textView.LineSpacing;
case VisualYPosition.TextTop:
return pos + tl.Baseline - textView.DefaultBaseline;
case VisualYPosition.TextBottom:
return pos + tl.Baseline - textView.DefaultBaseline + textView.DefaultLineHeight;
case VisualYPosition.TextMiddle:
return pos + tl.Baseline - textView.DefaultBaseline + textView.DefaultLineHeight/2;
case VisualYPosition.Baseline:
return pos + tl.Baseline;
default:
throw new ArgumentException("Invalid yPositionMode:" + yPositionMode);
}
}
else {
pos += tl.Height * textView.LineSpacing;
}
}
throw new ArgumentException("textLine is not a line in this VisualLine");
}
线386
public TextLine GetTextLineByVisualYPosition(double visualTop) {
const double epsilon = 0.0001;
double pos = this.VisualTop;
foreach (TextLine tl in TextLines) {
pos += tl.Height * textView.LineSpacing;
if (visualTop + epsilon < pos)
return tl;
}
return TextLines[TextLines.Count - 1];
}
线701
internal VisualLineDrawingVisual Render() {
Debug.Assert(phase == LifetimePhase.Live);
if (visual == null)
visual = new VisualLineDrawingVisual(this, textView.LineSpacing);
return visual;
}
714线
public VisualLineDrawingVisual(VisualLine visualLine, double lineSpacing) {
this.VisualLine = visualLine;
var drawingContext = RenderOpen();
double pos = 0;
foreach (TextLine textLine in visualLine.TextLines) {
textLine.Draw(drawingContext, new Point(0, pos), InvertAxes.None);
pos += textLine.Height * lineSpacing;
}
this.Height = pos;
drawingContext.Close();
}