有没有办法限制Flex富文本编辑器中的字符数?我想应该有,因为它在文本区域中是可能的。因此,如果我可以获得富文本编辑器中包含的文本区域,我就可以这样做
发布于 2009-06-11 19:41:16
我认为这在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是您想要限制的字符数量。
发布于 2014-07-26 01:52:25
我就是碰到了这个。
在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,我向"change“添加了一个侦听器,并在事件处理程序中执行了类似以下操作:
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设置为零,这样它们才能继续。
https://stackoverflow.com/questions/983084
复制相似问题