I'm creating a text editor and I want users to be able to select only part of a textfield and change the color. The problem I'm running into is the ColorPicker gains focus (I'm guessing) and the textfield loses it's "selection."
我正在创建一个文本编辑器,我希望用户只能选择部分文本字段并更改颜色。我遇到的问题是ColorPicker获得了焦点(我猜)并且文本域失去了它的“选择”。
All examples of text editing show the entire textfield changing color. Which isn't exactly what I want.
所有文本编辑示例都显示整个文本字段更改颜色。这不正是我想要的。
Any help/insight would be appreciated.
任何帮助/见解将不胜感激。
1 个解决方案
#1
1
Figure out what it is that causes your text field selection to be lost (e.g. could it be the click on the color picker component?). Then get the selectionBeginIndex
and selectionEndIndex
from the text field before that happens, and store it in a persistant (e.g. class member) variable.
找出导致文本字段选择丢失的原因(例如,是否可以点击颜色选择器组件?)。然后在发生之前从文本字段中获取selectionBeginIndex和selectionEndIndex,并将其存储在持久性(例如,类成员)变量中。
When the user selects a color, use the setTextFormat()
to change the color, passing in the stored start and end indices.
当用户选择颜色时,使用setTextFormat()更改颜色,传入存储的开始和结束索引。
A quick example:
一个简单的例子:
var beginIndex : uint;
var endIndex : uint;
function beforeFocusIsLost() : void {
beginIndex = myTextField.selectionBeginIndex;
endIndex = myTextField.selectionEndIndex;
}
function whenColorIsPicked() : void {
var tf : TextFormat;
tf = new TextFormat();
tf.color = myColor;
myTextField.setTextFormat(tf, beginIndex, endIndex);
}
If you want to keep the selection, you can also reset it after the color has been set using the TextField.setSelection() method.
如果要保留选择,还可以在使用TextField.setSelection()方法设置颜色后重置它。
EDIT: Note that if the selection is just hidden, maybe what you're after is simply TextField.alwaysShowSelection. Try setting it to true on your text field, and all of the above may be redundant.
编辑:请注意,如果选择只是隐藏,也许你所追求的只是TextField.alwaysShowSelection。尝试在文本字段中将其设置为true,并且上述所有操作都可能是多余的。
myTextField.alwaysShowSelection = true;
#1
1
Figure out what it is that causes your text field selection to be lost (e.g. could it be the click on the color picker component?). Then get the selectionBeginIndex
and selectionEndIndex
from the text field before that happens, and store it in a persistant (e.g. class member) variable.
找出导致文本字段选择丢失的原因(例如,是否可以点击颜色选择器组件?)。然后在发生之前从文本字段中获取selectionBeginIndex和selectionEndIndex,并将其存储在持久性(例如,类成员)变量中。
When the user selects a color, use the setTextFormat()
to change the color, passing in the stored start and end indices.
当用户选择颜色时,使用setTextFormat()更改颜色,传入存储的开始和结束索引。
A quick example:
一个简单的例子:
var beginIndex : uint;
var endIndex : uint;
function beforeFocusIsLost() : void {
beginIndex = myTextField.selectionBeginIndex;
endIndex = myTextField.selectionEndIndex;
}
function whenColorIsPicked() : void {
var tf : TextFormat;
tf = new TextFormat();
tf.color = myColor;
myTextField.setTextFormat(tf, beginIndex, endIndex);
}
If you want to keep the selection, you can also reset it after the color has been set using the TextField.setSelection() method.
如果要保留选择,还可以在使用TextField.setSelection()方法设置颜色后重置它。
EDIT: Note that if the selection is just hidden, maybe what you're after is simply TextField.alwaysShowSelection. Try setting it to true on your text field, and all of the above may be redundant.
编辑:请注意,如果选择只是隐藏,也许你所追求的只是TextField.alwaysShowSelection。尝试在文本字段中将其设置为true,并且上述所有操作都可能是多余的。
myTextField.alwaysShowSelection = true;