FairyGUI.InputTextField.ReplaceSelection C# (CSharp) Method

ReplaceSelection() public method

public ReplaceSelection ( string value ) : void
value string
return void
        public void ReplaceSelection(string value)
        {
            string leftText = null;
            string rightText = null;
            if (_selectionStart != null)
            {
                TextField.CharPosition cp = (TextField.CharPosition)_selectionStart;
                ClearSelection();

                if (cp.caretIndex < _caretPosition)
                {
                    leftText = textField.text.Substring(0, cp.caretIndex);
                    rightText = textField.text.Substring(_caretPosition);

                    _caretPosition = cp.caretIndex;
                }
                else
                {
                    leftText = textField.text.Substring(0, _caretPosition);
                    rightText = textField.text.Substring(cp.caretIndex);
                }
            }
            else
            {
                if (string.IsNullOrEmpty(value))
                    return;

                leftText = textField.text.Substring(0, _caretPosition);
                rightText = textField.text.Substring(_caretPosition);
            }

            if (!string.IsNullOrEmpty(value))
            {
                value = ValidateInput(value);
                if (leftText.Length + rightText.Length + value.Length > maxLength)
                    value = value.Substring(0, Math.Max(0, maxLength - leftText.Length - rightText.Length));

                this.text = leftText + value + rightText;
                _caretPosition += value.Length;
            }
            else
                this.text = leftText + rightText;

            //这里立即更新,使内含的图片等对象能够立即创建,避免延迟在Update里才创建,那样会引起闪烁
            textField.Redraw();

            onChanged.Call();
        }

Usage Example

示例#1
0
        void HandleTextInput()
        {
            InputTextField textField = (InputTextField)_focused;

            if (!textField.editable)
            {
                return;
            }

            if (_keyboard != null)
            {
                if (textField.keyboardInput)
                {
                    string s = _keyboard.GetInput();
                    if (s != null)
                    {
                        if (_keyboard.supportsCaret)
                        {
                            textField.ReplaceSelection(s);
                        }
                        else
                        {
                            textField.ReplaceText(s);
                        }
                    }

                    if (_keyboard.done)
                    {
                        this.focus = null;
                    }
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(Input.inputString))
                {
                    StringBuilder sb  = new StringBuilder();
                    int           len = Input.inputString.Length;
                    for (int i = 0; i < len; ++i)
                    {
                        char ch = Input.inputString[i];
                        if (ch >= ' ')
                        {
                            sb.Append(ch.ToString());
                        }
                    }
                    if (sb.Length > 0)
                    {
                        textField.ReplaceSelection(sb.ToString());
                    }
                }
            }
        }
All Usage Examples Of FairyGUI.InputTextField::ReplaceSelection