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

问题描述: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);

时消除掉换行符的影响。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值