RichTextBox编辑器之设置文本的Font属性

1.首先,讲解一下Font属性, public Font ( FontFamily family,float emSize,FontStyle style)
参数
family
新 Font 的 FontFamily。

emSize
新字体的全身大小(以磅值为单位)。

style
新字体的 FontStyle。

第三个参数就是用来设置字体样式的,FontStyle是个枚举类型,可以从MSDN里查到它的说明如下:
FontStyle 枚举
指定应用到文本的字形信息。
此枚举有一个 FlagsAttribute 属性,允许其成员值按位组合。
而它的成员如下:
成员名称 说明
Bold 加粗文本。
Italic 倾斜文本。
Regular 普通文本。

Strikeout 中间有直线通过的文本。

Underline 带下划线的文本。

补充:如果想要FontStyle可以拥有多个属性,如同时拥有加粗和倾斜效果,则可以通过位运算符 | ,快速得到,其它的操作类似。


2.讲一个具体调控选中文本的加粗效果的事例(其它效果可照搬)

//设置粗体
        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            if (this.fileContent.SelectionFont != null)  //当前选定文本只具有一种字体,字体Font只要其中的三个属性中的一个不同,就不属于统一字体
            {//获取或设置当前选定文本或插入点的字体。
                Font oldFont, newFont;
                oldFont = this.fileContent.SelectionFont;
                if (oldFont.Bold)
                {
                    newFont = new Font(oldFont, oldFont.Style ^ FontStyle.Bold);  //通过异或操作,去掉粗体属性
                }
                else
                {
                    newFont = new Font(oldFont, oldFont.Style | FontStyle.Bold);  
                }
                this.fileContent.SelectionFont = newFont;
                this.fileContent.Focus();  
            }
            else  //选定的文本具有多种字体
            {
                RichTextBox tempRichTextBox = new RichTextBox();  //将要存放被选中文本的副本  
                //得到实际富文本框中所选取的文本信息
                int curRtbStart = this.fileContent.SelectionStart;
                int len = this.fileContent.SelectionLength;
                //设置副本信息
                int tempRtbStart = 0;
                tempRichTextBox.Rtf = this.fileContent.SelectedRtf;
                //设置个计数器
                int amount = 0;
                for (int i = 0; i < len; i++)
                {
                    tempRichTextBox.Select(tempRtbStart + i, 1);  //一个一个选中每一个字
                    if(tempRichTextBox.SelectionFont.Bold == true)
                    {
                        amount++;
                    }
                }
                //如果其中至少有一个而不是全部是粗体,那么全部设置成粗体
                if(amount>0&&amount<len)
                {
                    for (int i = 0; i < len; i++)
                    {
                        tempRichTextBox.Select(tempRtbStart + i, 1);  //一个一个选中每一个字
                        if (tempRichTextBox.SelectionFont.Bold != true)
                        {
                            Font oldFont, newFont;
                            oldFont = tempRichTextBox.SelectionFont; 
                            newFont = new Font(oldFont, oldFont.Style | FontStyle.Bold);
                            tempRichTextBox.SelectionFont = newFont;
                        }
                    }
                }
                //如果其中全部为粗体,那么全部去掉它的粗体属性
                if(amount == len)
                {
                    for (int i = 0; i < len; i++)
                    {
                        tempRichTextBox.Select(tempRtbStart + i, 1);  //一个一个选中每一个字
                        if (tempRichTextBox.SelectionFont.Bold == true)
                        {
                            Font oldFont, newFont;
                            oldFont = tempRichTextBox.SelectionFont;
                            newFont = new Font(oldFont, oldFont.Style ^ FontStyle.Bold);
                            tempRichTextBox.SelectionFont = newFont;
                        }
                    }
                }
                //如果其中全部不为粗体,那么全部设置为粗体
                if(amount == 0)
                {
                    for (int i = 0; i < len; i++)
                    {
                        tempRichTextBox.Select(tempRtbStart + i, 1);  //一个一个选中每一个字
                        if (tempRichTextBox.SelectionFont.Bold != true)
                        {
                            Font oldFont, newFont;
                            oldFont = tempRichTextBox.SelectionFont;
                            newFont = new Font(oldFont, oldFont.Style | FontStyle.Bold);
                            tempRichTextBox.SelectionFont = newFont;
                        }
                    }
                }


                tempRichTextBox.Select(tempRtbStart, len);
                this.fileContent.SelectedRtf = tempRichTextBox.SelectedRtf; //将设置格式后的副本拷贝给原型  
                this.fileContent.Select(curRtbStart, len);   
            }
        }

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值