コード例 #1
0
        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);
        }
コード例 #2
0
        private XTouchableSpan GetPressedSpan(TextView textView, ISpannable spannable, MotionEvent e)
        {
            try
            {
                int x = (int)e.GetX();
                int y = (int)e.GetY();

                x -= textView.TotalPaddingLeft;
                y -= textView.TotalPaddingTop;

                x += textView.ScrollX;
                y += textView.ScrollY;

                Layout layout           = textView.Layout;
                int    verticalLine     = layout.GetLineForVertical(y);
                int    horizontalOffset = layout.GetOffsetForHorizontal(verticalLine, x);

                var link = spannable.GetSpans(horizontalOffset, horizontalOffset, Class.FromType(typeof(XTouchableSpan)));

                if (link?.Length > 0)
                {
                    var sdfs = (XTouchableSpan)link[0];
                    return(sdfs);
                }
            }
            catch (Exception exception)
            {
                Methods.DisplayReportResultTrack(exception);
            }

            return(null !);
        }
コード例 #3
0
        private TouchableSpan GetTouchableSpan(TextView textView, ISpannable span, MotionEvent e)
        {
            var x = (int)e.GetX();
            var y = (int)e.GetY();

            x -= textView.TotalPaddingLeft;
            y -= textView.TotalPaddingTop;

            x += textView.ScrollX;
            y += textView.ScrollY;

            var touchedLine = textView.Layout.GetLineForVertical(y);
            var touchOffset = textView.Layout.GetOffsetForHorizontal(touchedLine, x);

            _touchBounds.Left   = textView.Layout.GetLineLeft(touchedLine);
            _touchBounds.Top    = textView.Layout.GetLineTop(touchedLine);
            _touchBounds.Right  = textView.Layout.GetLineRight(touchedLine);
            _touchBounds.Bottom = textView.Layout.GetLineBottom(touchedLine);

            TouchableSpan touchableSpan = null;

            if (_touchBounds.Contains(x, y))
            {
                var spans = span.GetSpans(touchOffset, touchOffset, Java.Lang.Class.FromType(typeof(TouchableSpan)));
                touchableSpan = spans.Length > 0 ? (TouchableSpan)spans[0] : null;
            }
            return(touchableSpan);
        }
コード例 #4
0
        /// <summary>
        /// Since they are several over write of the Test property during layout we have to set this field as long as it is not definitly set
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void nativeBtn_AfterTextChanged(object sender, AfterTextChangedEventArgs e)
        {
            ISpannable spannable = e.Editable;
            var        indice    = spannable.ToString().IndexOf(_iconButton.Icon);
            var        spans     = spannable.GetSpans(indice, indice + _iconButton.Icon.Length, Java.Lang.Class.FromType(typeof(TypefaceSpan))).ToList();

            if (spans.Count == 0)
            {
                _nativeBtn.SetText(_iconSpan, TextView.BufferType.Spannable);
            }
        }
コード例 #5
0
        public override bool OnTouchEvent(TextView widget, ISpannable buffer, MotionEvent e)
        {
            if (e.Action == MotionEventActions.Up)
            {
                int touchX = (int)e.GetX();
                int touchY = (int)e.GetY();

                var layout = widget.Layout;

                int touchedLine = widget.Layout.GetLineForVertical(touchY);
                int touchOffset = widget.Layout.GetOffsetForHorizontal(touchedLine, touchX);

                _touchedLineBounds.Left   = layout.GetLineLeft(touchedLine);
                _touchedLineBounds.Top    = layout.GetLineTop(touchedLine);
                _touchedLineBounds.Right  = layout.GetLineWidth(touchedLine) + _touchedLineBounds.Left;
                _touchedLineBounds.Bottom = layout.GetLineBottom(touchedLine);

                if (_touchedLineBounds.Contains(touchX, touchY))
                {
                    object[] spans = buffer.GetSpans(touchOffset, touchOffset, Java.Lang.Class.FromType(typeof(ClickableSpan)));
                    foreach (object span in spans)
                    {
                        if (span is URLSpan)
                        {
                            var spanTarget = (URLSpan)span;
                            if (LinkClicked != null)
                            {
                                switch (spanTarget.URL.Split(':')[0])
                                {
                                case "taxonid":
                                    LinkClicked(widget, new TaxonLinkClickedEventArgs()
                                    {
                                        TaxonId = int.Parse(spanTarget.URL.Replace("taxonid://", ""))
                                    });
                                    break;

                                case "http":
                                case "https":
                                    Launcher.OpenAsync(new Uri(spanTarget.URL));
                                    break;
                                }
                                return(true);
                            }
                        }
                    }
                }

                return(base.OnTouchEvent(widget, buffer, e));
            }
            else
            {
                return(false);
            }
        }
コード例 #6
0
        private static List <AttributedTextRun> CreateRuns(
            ISpannable target,
            TextWriter writer)
        {
            var runs = new List <AttributedTextRun>();

            var spans = target.GetSpans(0, target.Length(), null);

            if (spans != null)
            {
                foreach (var span in spans)
                {
                    System.Console.WriteLine(span);
                }
            }

            writer.Write(target.ToString());
            return(runs);
        }
コード例 #7
0
        /**
         * Determines the touched location inside the TextView's text and returns the ClickableSpan found under it (if any).
         *
         * @return The touched ClickableSpan or null.
         */
        protected ClickableSpanWithText FindClickableSpanUnderTouch(TextView textView, ISpannable text, MotionEvent e)
        {
            // So we need to find the location in text where touch was made, regardless of whether the TextView
            // has scrollable text. That is, not the entire text is currently visible.
            var touchX = (int)e.GetX();
            var touchY = (int)e.GetY();

            // Ignore padding.
            touchX -= textView.TotalPaddingLeft;
            touchY -= textView.TotalPaddingTop;

            // Account for scrollable text.
            touchX += textView.ScrollX;
            touchY += textView.ScrollY;

            var layout      = textView.Layout;
            var touchedLine = layout.GetLineForVertical(touchY);
            var touchOffset = layout.GetOffsetForHorizontal(touchedLine, touchX);

            _touchedLineBounds.Left   = layout.GetLineLeft(touchedLine);
            _touchedLineBounds.Top    = layout.GetLineTop(touchedLine);
            _touchedLineBounds.Right  = layout.GetLineWidth(touchedLine) + _touchedLineBounds.Left;
            _touchedLineBounds.Bottom = layout.GetLineBottom(touchedLine);

            if (_touchedLineBounds.Contains(touchX, touchY))
            {
                // Find any ClickableSpan that lies under the touched area
                var spans = text.GetSpans(touchOffset, touchOffset, SpanClass);
                foreach (var span in spans)
                {
                    if (span is ClickableSpan)
                    {
                        return(ClickableSpanWithText.OfSpan(textView, span as ClickableSpan));
                    }
                }

                // No ClickableSpan found under the touched location.
                return(null);
            }

            // Touch lies outside the line's horizontal bounds where no spans should exist.
            return(null);
        }
コード例 #8
0
        public override bool OnTouchEvent(TextView widget, ISpannable buffer, MotionEvent e)
        {
            var action = e.Action;

            if (action == MotionEventActions.Up || action == MotionEventActions.Down)
            {
                int x      = (int)e.GetX() - widget.TotalPaddingLeft + widget.ScrollX;
                int y      = (int)e.GetY() - widget.TotalPaddingTop + widget.ScrollY;
                var layout = widget.Layout;
                int line   = layout.GetLineForVertical(y);
                int off    = layout.GetOffsetForHorizontal(line, x);

                var span = buffer.GetSpans(off, off, Java.Lang.Class.FromType(typeof(CommandableSpan))).OfType <CommandableSpan>().FirstOrDefault();

                if (span != null)
                {
                    if (action == MotionEventActions.Up)
                    {
                        span.OnClick(widget);
                        span.IsHighlight = false;
                    }
                    else if (action == MotionEventActions.Down)
                    {
                        span.IsHighlight = true;
                    }
                    widget.Invalidate();
                    currentSpan = span;
                    return(true);
                }
                else
                {
                    if (currentSpan != null)
                    {
                        currentSpan.IsHighlight = false;
                        currentSpan             = null;
                        widget.Invalidate();
                    }
                }
            }
            base.OnTouchEvent(widget, buffer, e);
            return(false);
        }
コード例 #9
0
        public override bool OnTouchEvent(TextView widget, ISpannable buffer, MotionEvent e)
        {
            var action = e.ActionMasked;

            if (action == MotionEventActions.Up || action == MotionEventActions.Down)
            {
                var x = e.GetX();
                var y = e.GetY();
                x -= widget.TotalPaddingLeft;
                y -= widget.TotalPaddingTop;
                x += widget.ScrollX;
                y += widget.ScrollY;

                var layout = widget.Layout;
                var line   = layout.GetLineForVertical((int)y);
                var off    = layout.GetOffsetForHorizontal(line, x);

                var link = buffer.GetSpans(off, off, Java.Lang.Class.FromType(typeof(ClickableSpan)));
                if (link.Length > 0)
                {
                    if (action == MotionEventActions.Up)
                    {
                        (link[0] as ClickableSpan).OnClick(widget);
                    }
                    else
                    {
                        Selection.SetSelection(buffer, buffer.GetSpanStart(link[0]), buffer.GetSpanEnd(link[0]));
                    }
                    return(true);
                }
                else
                {
                    Selection.RemoveSelection(buffer);
                }
            }

            return(false);
        }
コード例 #10
0
ファイル: EmojiconHandler.cs プロジェクト: Lina84/EmotIcons
        /**
         * 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);
            }
            }
        }
コード例 #11
0
        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);
        }
コード例 #12
0
ファイル: SpannableTools.cs プロジェクト: kishoredbn/Android
		// 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;
		}