有没有办法限制 Flex 富文本编辑器中的字符数?我想应该有,因为它可能在文本区域中。所以,如果我能掌握富文本编辑器中包含的 textarea,我就能做到
2 回答
我认为这在 actionscript 中相当容易,尽管我不确定如何在 mxml 中做到这一点。似乎有两个孩子包含在其中RichTextEditor
,其中一个是TextArea
。根据文档(http://livedocs.adobe.com/flex/3/langref/mx/controls/RichTextEditor.html#propertySummary),您可以像这样访问子控件:
myRTE.toolBar2.setStyle("backgroundColor", 0xCC6633);
myRTE 是您的文本编辑器的实例。所以我的猜测是这样的:
myRTE.textArea.maxChars = 125;
125 是您希望限制的字符数。
我刚遇到这个。
在 textArea 上设置 maxChars 将对文本区域提供限制,但这并不代表用户可以键入的字符数。
当用户键入时,会在幕后添加标记,这大大增加了字符数。
例如,如果我在 RichTextEditor 中键入字母“a”,我会得到 142 的字符数和这个 htmlText:
<TEXTFORMAT LEADING="2"><P ALIGN="LEFT"><FONT FACE="Verdana" SIZE="10" COLOR="#0B333C" LETTERSPACING="0" KERNING="0">a</FONT></P></TEXTFORMAT>
我看不到一个直接的方法来让正确的 maxChar 开箱即用,所以我扩展了 RichTextEditor 并给了它一个 maxChar。如果 maxChar > 0,我添加了一个监听器来“改变”并在事件处理程序中做了这样的事情:
protected function handleTextChange(event:Event) : void
{
var htmlCount:int = htmlText.length;
// if we're within limits, ensure we reset
if (htmlCount < maxChars)
{
textArea.maxChars = 0;
this.errorString = null;
}
// otherwise, produce an error string and set the component so the user
// can't keep typing.
else
{
var textCount:int = textArea.text.length;
textArea.maxChars = textCount;
var msg:String = "Maximum character count exceeded. " +
"You are using " + htmlCount + " of " + maxChars + " characters.";
this.errorString = msg;
}
}
这个想法是仅在错误状态下将 maxChars 应用于文本区域,因此用户无法键入任何其他内容,并且将提示删除一些字符。一旦我们离开错误状态,我们需要将 textArea.maxChars 设置为零,以便它们可以继续。