C#富文本框字体属性

本文关键字:字体 属性 文本 | 更新日期: 2023-09-27 18:28:51

我做了一个简单的文本编辑器。在它中,可以使用一个用于更改字体系列、样式和颜色的组合框。我成功地更改了所选文本的字体属性。我的代码

private void fontFamilySelectedIndexChanged(object sender, EventArgs e)
        {
            if (notesCon[currentNotes].SelectedText != "")
            {
                notesCon[currentNotes].SelectionFont = new Font(fontFamily.SelectedItem.ToString(), notesCon[currentNotes].SelectionFont.Size, notesCon[currentNotes].SelectionFont.Style);                
            }
        }
        private void fontStyleSelectedIndexChanged(object sender, EventArgs e) 
        {
            if (notesCon[currentNotes].SelectedText != "")
            {
                switch (fontStyle.SelectedItem.ToString())
                {
                    case "Bold": notesCon[currentNotes].SelectionFont = new Font(notesCon[currentNotes].SelectionFont.FontFamily, notesCon[currentNotes].SelectionFont.Size, FontStyle.Bold);
                        break;
                    case "Italic": notesCon[currentNotes].SelectionFont = new Font(notesCon[currentNotes].SelectionFont.FontFamily, notesCon[currentNotes].SelectionFont.Size, FontStyle.Italic);
                        break;
                    case "Regular": notesCon[currentNotes].SelectionFont = new Font(notesCon[currentNotes].SelectionFont.FontFamily, notesCon[currentNotes].SelectionFont.Size, FontStyle.Regular);
                        break;
                    case "Strikeout": notesCon[currentNotes].SelectionFont = new Font(notesCon[currentNotes].SelectionFont.FontFamily, notesCon[currentNotes].SelectionFont.Size, FontStyle.Strikeout);
                        break;
                    case "Underline": notesCon[currentNotes].SelectionFont = new Font(notesCon[currentNotes].SelectionFont.FontFamily, notesCon[currentNotes].SelectionFont.Size, FontStyle.Underline);
                        break;
                }
            }            
        }
        private void fontColorSelectedValueChanged(object sender, EventArgs e)
        {
            if (notesCon[currentNotes].SelectedText != "")
            {
                notesCon[currentNotes].SelectionFont = new Font(fontFamily.SelectedItem.ToString(), notesCon[currentNotes].SelectionFont.Size, notesCon[currentNotes].SelectionFont.Style);
                notesCon[currentNotes].SelectionColor = fontColor.SelectedItem.Color;
            }
        }

我的困境:

每当我尝试将富格文本框的当前字体属性设置为新的字体属性时,它都会成功地对其进行更改,但不会保留以前文本的字体属性。在不更改富格文本框中文本以前的字体属性的情况下,我应该如何更改字体属性?请帮忙,提前谢谢!

C#富文本框字体属性

这正是您所需要的。我也有这个问题。首先,您必须迭代所选文本的所有字符,然后应用字体。

private void ChangeFontStyleForSelectedText(string familyName, float? emSize, FontStyle? fontStyle, bool? enableFontStyle)
    {
        _maskChanges = true;
        try
        {
            int txtStartPosition = txtFunctionality.SelectionStart;
            int selectionLength = txtFunctionality.SelectionLength;
            if (selectionLength > 0)
                using (RichTextBox txtTemp = new RichTextBox())
                {
                    txtTemp.Rtf = txtFunctionality.SelectedRtf;
                    for (int i = 0; i < selectionLength; ++i)
                    {
                        txtTemp.Select(i, 1);
                        txtTemp.SelectionFont = RenderFont(txtTemp.SelectionFont, familyName, emSize, fontStyle, enableFontStyle);
                    }
                    txtTemp.Select(0, selectionLength);
                    txtFunctionality.SelectedRtf = txtTemp.SelectedRtf;
                    txtFunctionality.Select(txtStartPosition, selectionLength);
                }
        }
        finally
        {
            _maskChanges = false;
        }
    }

并呈现字体:

/// <summary>
    /// Changes a font from originalFont appending other properties
    /// </summary>
    /// <param name="originalFont">Original font of text
    /// <param name="familyName">Target family name
    /// <param name="emSize">Target text Size
    /// <param name="fontStyle">Target font style
    /// <param name="enableFontStyle">true when enable false when disable
    /// <returns>A new font with all provided properties added/removed to original font</returns>
    private Font RenderFont(Font originalFont, string familyName, float? emSize, FontStyle? fontStyle, bool? enableFontStyle)
    {
        if (fontStyle.HasValue && fontStyle != FontStyle.Regular && fontStyle != FontStyle.Bold && fontStyle != FontStyle.Italic && fontStyle != FontStyle.Underline)
            throw new System.InvalidProgramException("Invalid style parameter to ChangeFontStyleForSelectedText");
        Font newFont;
        FontStyle? newStyle = null;
        if (fontStyle.HasValue)
        {
            if (fontStyle.HasValue && fontStyle == FontStyle.Regular)
                newStyle = fontStyle.Value;
            else if (originalFont != null && enableFontStyle.HasValue && enableFontStyle.Value)
                newStyle = originalFont.Style | fontStyle.Value;
            else
                newStyle = originalFont.Style & ~fontStyle.Value;
        }
        newFont = new Font(!string.IsNullOrEmpty(familyName) ? familyName : originalFont.FontFamily.Name,
                            emSize.HasValue ? emSize.Value : originalFont.Size,
                            newStyle.HasValue ? newStyle.Value : originalFont.Style);
        return newFont;
    }

有关更多详细信息和步骤,您可以阅读本文:http://how-to-code-net.blogspot.ro/2014/01/how-to-make-custom-richtextbox-control.html