我正在使用Visual Studio 10,.NET Framework 4,并且正在为文本输入创建一个富文本框控件。我只是通过编写
来格式化文本*RichTextBox*.SelectionFont = new Font(currentFontFamily, currentFontSize, currentFontStyle);
方法
当我现在通过 RichTextBox .Rtf属性访问Rtf格式化的字符串时,它可以工作,但只包含第一个格式。
例如:
您好 世界
结果:
{\rtf1\ansi\ansicpg1252\deff0\deflang1031{\fonttbl{\f0\fnil\fcharset0 Verdana;}}
{\colortbl ;\red0\green0\blue0;}
\viewkind4\uc1\pard\cf1\b\f0\fs18 Hello World\par
}
实际上是 Hello World
这一切都是一样的。字体,大小,颜色,格式等。任何人都可以帮忙??
答案 0 :(得分:1)
这可能是因为SelectionStart和SelectionLength属性无效。如果通过这些属性明确限制选择,则RTF输出将按预期进行:
richTextBox1.Text = "Hello World";
// limit selection
richTextBox1.SelectionStart = 0;
richTextBox1.SelectionLength = 5;
richTextBox1.SelectionFont = new Font("Tahoma", 12, FontStyle.Bold);
MessageBox.Show(richTextBox1.Rtf);
变为
{\rtf1\ansi\deff0{\fonttbl{\f0\fnil\fcharset0 Tahoma;}{\f1\fnil\fcharset0 Microsoft Sans Serif;}}
\viewkind4\uc1\pard\lang1031\b\f0\fs24 Hello\b0\f1\fs17 World\par
}
修改强>
正如下面提到的提问者,问题的真正原因是调用TrimEnd():
richTextBox1.Text.TrimEnd()
一旦他删除它,一切都按预期工作。
答案 1 :(得分:0)
我发现了问题。
sealed trait Tree
case class Node(var left: Tree, var right: Tree, var value: String) extends Tree
case object EmptyNode extends Tree
导致Rtf丢失一些格式。我认为它只会影响文本本身,而不是格式化(特别是当删除空白字符时) - 我想我错了。
再次感谢 viertausend 寻求帮助