Skip to content

Instantly share code, notes, and snippets.

@rlingineni
Created December 21, 2016 21:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rlingineni/54f16329ecb5206ad3bf769a762deb42 to your computer and use it in GitHub Desktop.
Save rlingineni/54f16329ecb5206ad3bf769a762deb42 to your computer and use it in GitHub Desktop.
Hashtag Detection Xamarin.Forms
void Handle_MessageChanged(object sender, TextChangedEventArgs e)
{
Editor editor = sender as Editor;
string val = editor.Text; //Get Current Text
if (val.Length > 140)//If it is more than your character restriction
{
val = val.Remove(val.Length - 1);// Remove Last character
editor.Text = val; //Set the Old value
}
DetectHashTags(val);
}
void DetectHashTags(string input)
{
string[] words = input.Split(' ');
List<string> hashtags = new List<string>();
TagLayout.Children.Clear(); //TagLayout is a horizontal oriented stacklayout that we will add our detected hashtag labels too
for (int i = 0; i < words.Length; i++)
{
string word = words[i];
if (word.StartsWith("#", StringComparison.Ordinal) && i != words.Length-1)
{
if (!hashtags.Contains(word))
{
Debug.WriteLine(word);
hashtags.Add(word);
Label HashTagLabel = new Label()
{
FontSize = 12,
TextColor = Color.FromHex("#2196F3"),
BackgroundColor = Color.FromHex("#e4e4e4"),
VerticalOptions = LayoutOptions.StartAndExpand,
Text = word
};
TagLayout.Children.Add(HashTagLabel);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment