public bool AddSmiles(Context context, ISpannable spannable) { var hasChanges = false; foreach (var entry in Emoticons) { var smiley = entry.Key; var smileyImage = entry.Value; var indices = spannable.ToString().IndexesOf(smiley); foreach (var index in indices) { var set = true; foreach (ImageSpan span in spannable.GetSpans(index, index + smiley.Length, Java.Lang.Class.FromType(typeof(ImageSpan)))) { if (spannable.GetSpanStart(span) >= index && spannable.GetSpanEnd(span) <= index + smiley.Length) { spannable.RemoveSpan(span); } else { set = false; break; } } if (set) { hasChanges = true; spannable.SetSpan(new ImageSpan(context, smileyImage), index, index + smiley.Length, SpanTypes.ExclusiveExclusive); } } } return(hasChanges); }
private void MakeLinkClickable(ISpannable strBuilder, URLSpan span) { var start = strBuilder.GetSpanStart(span); var end = strBuilder.GetSpanEnd(span); var flags = strBuilder.GetSpanFlags(span); var clickable = new MyClickableSpan((HtmlLabel)Element, span); strBuilder.SetSpan(clickable, start, end, flags); strBuilder.RemoveSpan(span); }
private static void MakeLinkClickable(ISpannable spannable, URLSpan span) { var start = spannable.GetSpanStart(span); var end = spannable.GetSpanEnd(span); var flags = spannable.GetSpanFlags(span); var clickableLink = new ClickableLinkSpan(span.URL); spannable.SetSpan(clickableLink, start, end, flags); spannable.RemoveSpan(span); }
/** * Convert emoji characters of the given Spannable to the according emojicon. * * @param context * @param text * @param emojiSize * @param index * @param length */ public static void AddEmojis(Context context, ISpannable text, int emojiSize, int index, int length) { int textLength = text.Length(); int textLengthToProcessMax = textLength - index; int textLengthToProcess = length < 0 || length >= textLengthToProcessMax ? textLength : (length+index); // remove spans throughout all text //EmojiconSpan[] var oldSpans = text.GetSpans(0, textLength,Java.Lang.Class.FromType(typeof(EmojiconSpan))); for (int i = 0; i < oldSpans.Length; i++) { text.RemoveSpan(oldSpans[i]); } int skip; for (int i = index; i < textLengthToProcess; i += skip) { skip = 0; int icon = 0; char c = text.CharAt(i); if (IsSoftBankEmoji(c)) { icon = GetSoftbankEmojiResource(c); skip = icon == 0 ? 0 : 1; } if (icon == 0) { int unicode = Character.CodePointAt(text, i); skip = Character.CharCount(unicode); if (unicode > 0xff) { icon = GetEmojiResource(context, unicode); } if (icon == 0 && i + skip < textLengthToProcess) { int followUnicode = Character.CodePointAt(text, i + skip); if (followUnicode == 0x20e3) { int followSkip = Character.CharCount(followUnicode); switch (unicode) { case 0x0031: icon = Resource.Drawable.emoji_0031; break; case 0x0032: icon = Resource.Drawable.emoji_0032; break; case 0x0033: icon = Resource.Drawable.emoji_0033; break; case 0x0034: icon = Resource.Drawable.emoji_0034; break; case 0x0035: icon = Resource.Drawable.emoji_0035; break; case 0x0036: icon = Resource.Drawable.emoji_0036; break; case 0x0037: icon = Resource.Drawable.emoji_0037; break; case 0x0038: icon = Resource.Drawable.emoji_0038; break; case 0x0039: icon = Resource.Drawable.emoji_0039; break; case 0x0030: icon = Resource.Drawable.emoji_0030; break; case 0x0023: icon = Resource.Drawable.emoji_0023; break; default: followSkip = 0; break; } skip += followSkip; } else { int followSkip = Character.CharCount(followUnicode); switch (unicode) { case 0x1f1ef: icon = (followUnicode == 0x1f1f5) ? Resource.Drawable.emoji_1f1ef_1f1f5 : 0; break; case 0x1f1fa: icon = (followUnicode == 0x1f1f8) ? Resource.Drawable.emoji_1f1fa_1f1f8 : 0; break; case 0x1f1eb: icon = (followUnicode == 0x1f1f7) ? Resource.Drawable.emoji_1f1eb_1f1f7 : 0; break; case 0x1f1e9: icon = (followUnicode == 0x1f1ea) ? Resource.Drawable.emoji_1f1e9_1f1ea : 0; break; case 0x1f1ee: icon = (followUnicode == 0x1f1f9) ? Resource.Drawable.emoji_1f1ee_1f1f9 : 0; break; case 0x1f1ec: icon = (followUnicode == 0x1f1e7) ? Resource.Drawable.emoji_1f1ec_1f1e7 : 0; break; case 0x1f1ea: icon = (followUnicode == 0x1f1f8) ? Resource.Drawable.emoji_1f1ea_1f1f8 : 0; break; case 0x1f1f7: icon = (followUnicode == 0x1f1fa) ? Resource.Drawable.emoji_1f1f7_1f1fa : 0; break; case 0x1f1e8: icon = (followUnicode == 0x1f1f3) ? Resource.Drawable.emoji_1f1e8_1f1f3 : 0; break; case 0x1f1f0: icon = (followUnicode == 0x1f1f7) ? Resource.Drawable.emoji_1f1f0_1f1f7 : 0; break; default: followSkip = 0; break; } skip += followSkip; } } } if (icon > 0) { text.SetSpan(new EmojiconSpan(context, icon, emojiSize), i, i + skip, SpanTypes.ExclusiveExclusive); } } }
public long SaveText(IEditable Text, Bundle args) { long NoteNumber = 0; string date = DateTime.Now.ToString("dd MMM yyyyг. HH:mm"); ContentValues cv = new ContentValues(); SpannableFactory a = new SpannableFactory(); ISpannable saveText = a.NewSpannable(Text); Java.Lang.Object[] span = saveText.GetSpans(0, saveText.Length(), Java.Lang.Class.FromType(typeof(ImageSpan))); if (span != null) { for (int i = 0; i < span.Length; i++) { saveText.RemoveSpan(span[i]); } } if (Build.VERSION.SdkInt >= BuildVersionCodes.N) { cv.Put(COLUMN_TEXT, Html.ToHtml(saveText, ToHtmlOptions.ParagraphLinesIndividual)); } else { cv.Put(COLUMN_TEXT, Html.ToHtml(saveText)); } cv.Put(COLUMN_EDITINGTIME, date); if (args != null) { WritableDatabase.Update(TEXTTABLE, cv, "_id == ?", new string[] { args.GetString(Databasehelper.COLUMN_ID) }); WritableDatabase.ExecSQL("DELETE from " + CONTENTTABLE + " Where _id == " + args.GetString(Databasehelper.COLUMN_ID)); //Delete old image NoteNumber = Convert.ToInt32(args.GetString(COLUMN_ID)); } else { long id = 1; cursor = WritableDatabase.RawQuery("Select _id from " + Databasehelper.TEXTTABLE + " ORDER BY _id DESC LIMIT 1", null); if (cursor.MoveToFirst()) { id = cursor.GetLong(cursor.GetColumnIndex("_id")); cursor.Close(); id++; } cv.Put(COLUMN_ID, id); WritableDatabase.Insert(TEXTTABLE, null, cv); NoteNumber = id; } Java.Lang.Object[] spans = Text.GetSpans(0, Text.Length(), Java.Lang.Class.FromType(typeof(ImageSpan))); //Insert Image in Database if (spans != null) { for (int i = 0; i < spans.Length; i++) { int start = Text.GetSpanStart(spans[i]); int end = Text.GetSpanEnd(spans[i]); string source; source = Text.ToString().Substring(start + 1, end - start - 2); SaveBitmapBase(NoteNumber, source, start, end, ((BitmapDrawable)((ImageSpan)spans[i]).Drawable).Bitmap); } } return(NoteNumber); }
// Taken from https://gist.githubusercontent.com/Cheesebaron/5034440/raw/f962c41c95f8d94457ef9a60e19fe7efa2a50d61/SpannableTools.cs public static bool AddSmiles(Context context, ISpannable spannable) { var hasChanges = false; foreach (var entry in Emoticons) { var smiley = entry.Key; var smileyImage = entry.Value; var indices = spannable.ToString().IndexesOf(smiley); var index = 0; foreach (var token in spannable.ToString().Split(' ')) { if (token.Equals (smiley)) { var set = true; foreach (ImageSpan span in spannable.GetSpans(index, index + smiley.Length, Java.Lang.Class.FromType(typeof(ImageSpan)))) { if (spannable.GetSpanStart (span) >= index && spannable.GetSpanEnd (span) <= index + smiley.Length) spannable.RemoveSpan (span); else { set = false; break; } } if (set) { hasChanges = true; spannable.SetSpan (new ImageSpan (context, smileyImage), index, index + smiley.Length, SpanTypes.ExclusiveExclusive); } } index += token.Length + 1; } } return hasChanges; }