我认为这现在应该可以工作了......正如我在上一篇文章中所评论的那样,当它们的 textsize 变得相同时,这会处理 <run> 元素的自动合并:
private void sizeTextSel(int iVal)
{
TextSelection ts = _richTextBox.Selection;
TextPointer tpStart = ts.Start;
TextPointer tpEnd = ts.End;
TextPointer tpRun = tpStart;
int iSelLength = ts.Text.Length;
int iStartRunLength;
object fontSizeSelection;
int iTotalSelected = 0, iSelect = 0,iNew=0;
do
{
iStartRunLength = tpRun.GetTextRunLength(LogicalDirection.Forward);
iSelect = iStartRunLength < iSelLength - iTotalSelected ? iStartRunLength : iSelLength - iTotalSelected;
if (iStartRunLength > 0)
{
iSelect = iSelect == 0 ? 1 : iSelect;
ts.Select(tpRun, tpRun.GetPositionAtOffset(iSelect, LogicalDirection.Forward));
fontSizeSelection = ts.GetPropertyValue(TextElement.FontSizeProperty);
if (fontSizeSelection != null)
ts.ApplyPropertyValue(TextElement.FontSizeProperty, (double)fontSizeSelection + iVal);
iNew = tpRun.GetTextRunLength(LogicalDirection.Forward);
if (iNew==0)
tpRun = tpRun.GetPositionAtOffset(iSelect + 1, LogicalDirection.Forward);
else
tpRun = tpRun.GetPositionAtOffset(iSelect, LogicalDirection.Forward);
}
else
{
if (tpRun.Parent.GetType() == typeof(FlowDocument))
iSelect = 2;
else
iSelect = 0;
tpRun = tpRun.GetPositionAtOffset(1, LogicalDirection.Forward);
}
iTotalSelected += iSelect;
} while (tpRun != null && iTotalSelected < iSelLength);
ts.Select(tpStart, tpEnd);
}