Check if the user typed a specific word after entering the word in <Editor>

So, I'm looking for code that checks to see if the user has written one of the specific words (Select, From, Where). If true, then the text color of this word should change (TextChanged event). I do not know how I should do this, that this is possible.

I use Xamarin.Formsand made into the Page.xamlfollowing code:

<Editor x:Name="qEditor" 
     Text="Input"
     TextChanged="Handle_TextChanged"
     BackgroundColor="White" 
     HorizontalOptions="FillAndExpand"
     HeightRequest="75"/>

Therefore, whenever someone entered one of the specific words, the color of that word alone should directly change to red.

Code in Page.xaml.cs:

void Handle_TextChanged(object sender, TextChangedEventArgs e)
{
    string input = e.NewTextValue;
    string[] qInput = input.Split(' ');
    //Code here
}

I searched a lot on Google and Stackoverflow and found something , but uses Containsone that doesn't work for my code.

- , , , . C# Xamarin.Forms.

+4
2

Xamarin .

Editor, . , bindable FormattedString. , , TextChanged ( Regex ).

FormattedText bindable-property, Spannable android AttributedString iOS.

enter image description here

public class ExEditor : Editor
{
    public static readonly BindableProperty FormattedTextProperty =
        BindableProperty.Create(
            "FormattedText", typeof(FormattedString), typeof(ExEditor),
            defaultValue: default(FormattedString));

    public FormattedString FormattedText
    {
        get { return (FormattedString)GetValue(FormattedTextProperty); }
        set { SetValue(FormattedTextProperty, value); }
    }

    public ExEditor()
    {
        TextChanged += ExEditor_TextChanged;
    }

    void ExEditor_TextChanged(object sender, TextChangedEventArgs e)
    {
        if (string.IsNullOrWhiteSpace(Text))
            return;

        var pattern = @"\b(SELECT|WHERE|AND|OR)\b";
        var words = Regex.Split(Text, pattern, RegexOptions.IgnoreCase | RegexOptions.Multiline);

        var formattedString = new FormattedString();
        foreach (var word in words)
            formattedString.Spans.Add(new Span
            {
                Text = word,
                BackgroundColor = BackgroundColor,
                FontSize = FontSize,
                FontFamily = FontFamily,
                FontAttributes = FontAttributes,
                ForegroundColor = Regex.IsMatch(word, pattern, RegexOptions.IgnoreCase) ? Color.Red : TextColor
            });

        FormattedText = formattedString; 
    }       
}

iOS Renderer

[assembly: ExportRenderer(typeof(ExEditor), typeof(ExEditorRenderer))]
namespace SampleApp.iOS
{
    public class ExEditorRenderer : EditorRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Editor> e)
        {
            base.OnElementChanged(e);

            if (Control == null || Element == null)
                return;

            UpdateTextOnControl();
        }

        protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);

            if (e.PropertyName == nameof(ExEditor.FormattedText)
                || e.PropertyName == nameof(Editor.FontFamily)
                || e.PropertyName == nameof(Editor.FontSize)
                || e.PropertyName == nameof(Editor.TextColor)
                || e.PropertyName == nameof(Editor.BackgroundColor)
                || e.PropertyName == nameof(Editor.FontAttributes))
                {
                    UpdateTextOnControl();
                }
        }

        void UpdateTextOnControl()
        {
            var caretPos = Control.GetOffsetFromPosition(Control.BeginningOfDocument, Control.SelectedTextRange.Start);

            if (Element is ExEditor formsElement)
                if (formsElement.FormattedText != null)
                    Control.AttributedText = formsElement.FormattedText.ToAttributed(new Font(), Element.TextColor);

            var newPosition = Control.GetPosition(Control.BeginningOfDocument, offset: caretPos);
            Control.SelectedTextRange = Control.GetTextRange(newPosition, newPosition);
        }
    }
}

Android Renderer

[assembly: ExportRenderer(typeof(ExEditor), typeof(ExEditorRenderer))]
namespace SampleApp.Android
{
    public class ExEditorRenderer : EditorRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Editor> e)
        {
            base.OnElementChanged(e);

            if (Control == null || Element == null)
                return;

            UpdateTextOnControl();
        }

        protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);

            if (e.PropertyName == nameof(ExEditor.FormattedText)
                || e.PropertyName == nameof(Editor.FontFamily)
                || e.PropertyName == nameof(Editor.FontSize)
                || e.PropertyName == nameof(Editor.TextColor)
                || e.PropertyName == nameof(Editor.BackgroundColor)
                || e.PropertyName == nameof(Editor.FontAttributes))
            {
                UpdateTextOnControl();
            }
        }

        void UpdateTextOnControl()
        {
            var caretPos = Control.SelectionStart;

            if (Element is ExEditor formsElement)
                if (formsElement.FormattedText != null)
                    Control.SetText(formsElement.FormattedText.ToAttributed(new Font(), Element.TextColor, Control),
                                    TextView.BufferType.Spannable);

            Control.SetSelection(caretPos);
        }
    }
}

<!-- make sure to map local prefix to control namespace -->
<local:ExEditor Text="Select * from Table where text='1' and type='str'" 
        VerticalOptions="Start" />
+4

FormattedText Xamarin.Forms.Label

:

    private string[] wordsRed = { "Select", "From", "Where"}; // some examples
    private bool isSpecialColor = false;
    private Span span;

    /// <summary>
    /// Handles the text changed.
    /// </summary>
    /// <param name="sender">Sender.</param>
    /// <param name="e">E.</param>
    void Handle_TextChanged(object sender, TextChangedEventArgs e)
    {
        string input = e.NewTextValue;
        string[] qInput = input.Split(' ');

        CheckInput(qInput);
    }

    /// <summary>
    /// Checks the input.
    /// </summary>
    /// <param name="qInput">Q input.</param>
    private void CheckInput(string[] qInput)
    {
        // Check if the last typed character is a word separator.
        if (input[input.Length-1] == ' ') 
        {
            //
            // Loop through the string[] with the words you want to give a color on.
            //
            for (int i = 0; i < wordsRed.Length; i++)
            {
                //
                // Check on the last word that is typed
                //
                if (qInput[qInput.Length-1] == wordsRed[i])
                {
                    // Create a span with the word and color the foreground red
                    span = new Span
                    {
                        Text = qInput[qInput.Length-1] + " ",
                        ForegroundColor = Color.Green,
                    };

                    // Add the span to the Label
                    aLabel.FormattedText.Spans.Add(item: span);
                    // set boolean true because there is made a span with a red foreground.
                    isSpecialColor = true;
                }
                else
                {
                    // Do nothing.
                }
            }

            // Check if the word altready is colored.
            if (isSpecialColor)
            {
                // Set the boolean to false for the next check.
                isSpecialColor = false;
            }
            else
            {
                // Create span with the word and color it black.
                span = new Span
                {
                    Text = qInput[i] + " ",
                    ForegroundColor = Color.Black,
                };
                aLabel.FormattedText.Spans.Add(item: span);
            }
        }
        else 
        {                
            // Do nothing.
        }
    }
+1

Source: https://habr.com/ru/post/1686633/


All Articles