Created
December 21, 2016 21:45
-
-
Save rlingineni/54f16329ecb5206ad3bf769a762deb42 to your computer and use it in GitHub Desktop.
Hashtag Detection Xamarin.Forms
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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