您的位置:首页 > 其它

RichTextEditor控件选中的字符和根据控件属性selection得到的字符存在差异的问题及解决办法

2011-06-03 12:48 357 查看
问题描述:RichTextEditor控件选中的字符和根据控件属性selection.text得到的字符存在差异的问题

 

解决办法:

 

对于在RichTextEditor控件中,选中某段文本,然后对选中的文本进行某种操作,但是使用selection.text得到的结果和选中的文本总是存在差异,不是前面多几个就是后面少几个的。但是对于只选第一行进行解析时结果是对的。

对于RichTextEditor控件,我们可以通过它的属性selectionBeginIndex来得到它的起始索引和selectionEndIndex结束索引。

debug到程序的内部查看到selectionBeginIndex和selectionEndIndex都是正确的,但是不知道为什么返回的字符串和选中的有差异。考虑到第一行没有错误,想到是不是因为换行符导致的错误,系统在选择字符设置selectionBeginIndex和selectionEndIndex属性时计算了换行符,但是在selection.text时却没有计算换行符。根据返回的文本差异可以看出问题和我猜想的一样。

 

至此找到了解决问题的办法,在返回文本时消除掉回车符的影响即可,可以通过计算在选中的文本前有多少行从而计算出换行符的出现次数。

 

因为sqlTextArea(为RichTextEditor的id).selection.text中的selection调用的是TextRange类型对象。

public function get
selection():TextRange

{

        return new TextRange(this, true);

}

 

 

TextRange的构造方法如下:

public function
TextRange(owner:UIComponent,

  modifiesSelection:Boolean = false,

  beginIndex:int = -1, endIndex:int = -1)

{

super();

 

_owner
= owner;

 

try

{

textField
= _owner["textArea"].getTextField();

}

catch(e:Error)

{

textField
= this["_owner"].getTextField();

}

 

_modifiesSelection
= modifiesSelection;

 

if
(!_modifiesSelection)

{

if
(beginIndex < 0)

beginIndex
= 0;

 

if
(beginIndex > textField.length)

beginIndex
= textField.length;

 

if
(endIndex < 0 || endIndex > textField.length)

endIndex
= textField.length;

 

_beginIndex
= beginIndex;

_endIndex
= endIndex;

}

else

{

if
(beginIndex < 0 || beginIndex > textField.length)

beginIndex
= textField.selectionBeginIndex;

 

if
(endIndex < 0 || endIndex > textField.length)

endIndex
= textField.selectionEndIndex;

 

textField.selectable
= true;

 

if
(beginIndex != textField.selectionBeginIndex ||

endIndex
!= textField.selectionEndIndex)

{

textField.setSelection(beginIndex,
endIndex);

}

}

}

 

本来准备重写此类,来消除掉换行符的影响,但是尝试了后发现类TextRange本身没有得到当前被选中文本所在行的方法,而类TextField的对象存在此方法getLineIndexOfChar(index:int);,如果直接修改TextRange的beginIndex和endIndex,那么RichTextEditor控件中的文本的选中状态会发生变化。

 

使用新的TextField来实现,代码如下:

 

var
textfield:TextField = new TextField();

textfield.text
= sqlTextArea.textArea.text;

textfield.multiline
= true;

textfield.wordWrap
= true;

textfield.setSelection(sqlTextArea.textArea.selectionBeginIndex,sqlTextArea.textArea.selectionEndIndex);

var
textlineNum:int =
textfield.getLineIndexOfChar(sqlTextArea.textArea.selectionEndIndex);

var
textlineNumbegin:int =
textfield.getLineIndexOfChar(sqlTextArea.textArea.selectionBeginIndex);

textfield.setSelection(sqlTextArea.textArea.selectionBeginIndex-textlineNumbegin,sqlTextArea.textArea.selectionEndIndex-textlineNum);

//var
textrange:TextRange = new
TextRange(sqlTextArea,true,sqlTextArea.textArea.selectionBeginIndex-textlineNumbegin,sqlTextArea.textArea.selectionEndIndex-textlineNum+1);

var
execsql:String = textfield.selectedText;

 

得到被选中文本selectionBeginIndex所在的行数,和selectionEndIndex的所在的行数,从而在再一次设置

textfield.setSelection(sqlTextArea.textArea.selectionBeginIndex-textlineNumbegin,sqlTextArea.textArea.selectionEndIndex-textlineNum);

时消除掉换行符的影响。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  function string
相关文章推荐