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);
时消除掉换行符的影响。
解决办法:
对于在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);
时消除掉换行符的影响。
相关文章推荐
- RichTextEditor控件选中的字符和根据控件属性selection得到的字符存在差异的问题及解决办法
- 关于flex中RichTextEditor控件,监听Ctrl+s事件和去除Ctrl+s按下时在RichTextEditor生成的不可知字符的问题
- 在DataList使用 用户控件 并 绑定属性 时可能的问题的解决办法
- EasyUi 打开对话框后控件赋值及赋值后不显示的问题解决办法
- 98/2000/xp/2003局域网访问xp机器共享可能存在的问题和解决办法
- 使用jQuery设置元素选中的问题及解决办法
- asp.net updatePanel easyUI控件 失效问题解决办法
- 问题“ConnectionString属性尚未初始化”之解决办法
- jqGrid翻页时数据选中丢失问题的解决办法
- C#控件TreeView在选中后图标改变的解决办法
- Win7/Win8中注册控件失败,提示“DllRegisterServer的调用失败,错误代码为0x******** ” 问题的解决办法
- 关于AutoCompleteTextView 为什么必须输入两个字符才能出现补全提示的问题的解决办法
- VS2008编译出现问题:error C2485: “__restrict”: 无法识别的扩展属性 解决办法
- td8.0描述字段中文字符解决办法(字体大小控件下载)
- 关于火车票12306网站出现“此网站的安全证书存在问题”解决办法
- jQuery在异步请求数据返回后,调用$("selector").html(data.content);之后因为一些特殊字符或者',"不能显示内容的问题解决办法
- c#程序运行报错提示相应的设置outputpath和assemblyname属性的问题解决办法!
- Silverlight控件自适应利器UseLayoutRounding属性(解决自适应抖动问题)
- uitextfield 偶尔无法选中问题解决办法
- 解决布局控件的属性重新设置问题----Margin