WPF RichTextBox增加字体大小

时间:2013-09-04 15:20:27

标签: c# wpf fonts richtextbox textrange

正如标题所示,我的目标是增加/减少RichTextBox内当前文本选择的字体大小。

这可能看似微不足道,实际上它是 - 只要TextRange中的所有文本的字体大小相同。当选择包含具有不同字体大小的文本时,

range.GetPropertyValue(TextElement.FontSizeProperty);

我用它来获取字体大小的前一个值(为了知道要设置值的必要条件),返回DependencyProperty.UnsetValue。

这不仅是一个问题,而且还有一个事实,即没有一种方法可以增加大小,只有一种方法可以明确地将其设置为给定值,这会引起问题。

我考虑过尝试用不同属性值解析子范围的TextRange,但这似乎是一种非常复杂的方法来实现应该是微不足道的事情。

我该如何解决这个问题?提前谢谢。

4 个答案:

答案 0 :(得分:1)

我认为任何实施,如果它存在,最终都必须做同样的事情,因此你的选择是有限的:

如果您担心这是很多操作,那么您可能是正确的,我能想到的唯一概念性捷径就是应用ScaleTransform。这当然只会让它看起来有效,而且不会改变选择的属性。

如果您担心这会在您的用户代码中出现混乱,那么您也是对的,但我建议您自己创建RichTextBox并自己实现IncrementSizeOfSelection和DecrementSizeOfSelection。

答案 1 :(得分:1)

我认为现在应该可行了......正如我在上一篇文章中所评论的那样,它负责自动合并<运行>当他们的文本化成为相同的元素时:

    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);
    }

答案 2 :(得分:0)

通过RichTextBox的选择获取TextRange,它将为您提供TextRange,并且您的fontsize具有double值。试试这个

 public void SetFontSizeForSelection(TextRange selection, double newFontSize)
        {
            if ((newFontSize - 0) < double.Epsilon)
                newFontSize = 1;
            selection.ApplyPropertyValue(TextElement.FontSizeProperty, newFontSize);
        }

答案 3 :(得分:0)

我认为必要的操作并不是太多。 我已尝试使用多种不同的文字大小 - 此示例仅在所选文本包含在&lt; RUN&gt;元素。但是应该可以扩展不同元素的代码,例如&lt; span&gt; 然而,我不知道如何处理类似的事情     &LT;段落&gt;粗体&gt;粗体&lt; / Bold&gt;&lt; /段落&gt;

    private void sizeTextSel(int iVal)
    {
        TextSelection ts = _richTextBox.Selection;
        TextPointer tpStart = ts.Start;
        TextPointer tpEnd = ts.End;
        TextPointer tpRun = tpStart;

        int iSelLength = tpStart.GetOffsetToPosition(tpEnd);
        int iStartRunLength;
        object fontSizeSelection;
        int iTotalSelected = 0,iSelect;

        do
        {                   
            iStartRunLength = tpRun.GetTextRunLength(LogicalDirection.Forward);
            iSelect = iStartRunLength < iSelLength - iTotalSelected ? iStartRunLength : iSelLength - iTotalSelected;
            ts.Select(tpRun, tpRun.GetPositionAtOffset(iSelect,LogicalDirection.Forward));
            fontSizeSelection = ts.GetPropertyValue(TextElement.FontSizeProperty);
            if (fontSizeSelection != null)
                ts.ApplyPropertyValue(TextElement.FontSizeProperty, (double)fontSizeSelection + iVal);

            tpRun = tpRun.GetPositionAtOffset(iSelect + 1);
            while (tpRun != null && tpRun.Parent.GetType() != typeof(Run))
            {
                tpRun = tpRun.GetPositionAtOffset(1);
                //iOffset +=1;
            }
            iTotalSelected += iSelect+1;

        } while (tpRun != null && iTotalSelected < iSelLength);

        ts.Select(tpStart, tpEnd);
    }