Exemplo n.º 1
0
            public bool OnActionItemClicked(ActionMode mode, IMenuItem item)
            {
                CharacterStyle         cs;
                int                    start = edt.SelectionStart;
                int                    end   = edt.SelectionEnd;
                SpannableStringBuilder ssb   = new SpannableStringBuilder(edt.Text);

                switch (item.ItemId)
                {
                case Resource.Id.bold:
                    cs = new StyleSpan(TypefaceStyle.Bold);
                    ssb.SetSpan(cs, start, end, SpanTypes.ExclusiveExclusive);
                    edt.Text = ssb.ToString();
                    return(true);

                case Resource.Id.italic:
                    cs = new StyleSpan(TypefaceStyle.Italic);
                    ssb.SetSpan(cs, start, end, SpanTypes.ExclusiveExclusive);
                    edt.Text = ssb.ToString();
                    return(true);

                case Resource.Id.underline:
                    cs = new UnderlineSpan();
                    ssb.SetSpan(cs, start, end, SpanTypes.ExclusiveExclusive);
                    edt.Text = ssb.ToString();
                    return(true);

                case Resource.Id.strikethrough:
                    cs = new StrikethroughSpan();
                    ssb.SetSpan(cs, start, end, SpanTypes.ExclusiveExclusive);
                    edt.Text = ssb.ToString();
                    return(true);
                }
                return(false);
            }
Exemplo n.º 2
0
 void SetSpan(int start, int end, StrikethroughSpan span)
 {
     if (start == end)
     {
         EditText.EditableText.SetSpan(span, start, end, SpanTypes.InclusiveInclusive);
     }
     else
     {
         EditText.EditableText.SetSpan(span, start, end, SpanTypes.ExclusiveInclusive);
     }
 }
Exemplo n.º 3
0
        void DisableSpan(int start, int end, StrikethroughSpan span)
        {
            var spans = EditText.EditableText.GetSpans(start, end, Java.Lang.Class.FromType(typeof(StrikethroughSpan)));

            for (int i = spans.Length - 1; i >= 0; i--)
            {
                int spanEnd   = EditText.EditableText.GetSpanEnd(spans[i]);
                int spanStart = EditText.EditableText.GetSpanStart(spans[i]);
                EditText.EditableText.RemoveSpan(spans[i]);

                if (spanStart < start)
                {
                    EditText.EditableText.SetSpan(span, spanStart, start, SpanTypes.ExclusiveExclusive);
                }
                if (spanEnd > end)
                {
                    EditText.EditableText.SetSpan(span, end, spanEnd, SpanTypes.ExclusiveExclusive);
                }
            }
        }
Exemplo n.º 4
0
        private static void HandleFormatRun(ISpannable spannableString, IAttributedTextRun run)
        {
            var attributes = run.Attributes;
            var start      = run.Start;
            var end        = start + run.Length;

            var fontName = attributes.GetFontName();

            if (fontName != null)
            {
                var typefaceSpan = new TypefaceSpan(fontName);
                spannableString.SetSpan(typefaceSpan, start, end, SpanTypes.ExclusiveExclusive);
            }

            if (attributes.GetBold() && attributes.GetItalic())
            {
                var span = new StyleSpan(TypefaceStyle.BoldItalic);
                spannableString.SetSpan(span, start, end, SpanTypes.ExclusiveExclusive);
            }
            else if (attributes.GetBold())
            {
                var span = new StyleSpan(TypefaceStyle.Bold);
                spannableString.SetSpan(span, start, end, SpanTypes.ExclusiveExclusive);
            }
            else if (attributes.GetItalic())
            {
                var span = new StyleSpan(TypefaceStyle.Italic);
                spannableString.SetSpan(span, start, end, SpanTypes.ExclusiveExclusive);
            }

            if (attributes.GetUnderline())
            {
                var span = new UnderlineSpan();
                spannableString.SetSpan(span, start, end, SpanTypes.ExclusiveExclusive);
            }

            var foregroundColor = attributes.GetForegroundColor()?.ParseAsInts()?.ToColor();

            if (foregroundColor != null)
            {
                var span = new ForegroundColorSpan((global::Android.Graphics.Color)foregroundColor);
                spannableString.SetSpan(span, start, end, SpanTypes.ExclusiveExclusive);
            }

            var backgroundColor = attributes.GetBackgroundColor()?.ParseAsInts()?.ToColor();

            if (backgroundColor != null)
            {
                var span = new BackgroundColorSpan((global::Android.Graphics.Color)backgroundColor);
                spannableString.SetSpan(span, start, end, SpanTypes.ExclusiveExclusive);
            }

            if (attributes.GetSubscript())
            {
                var span = new SubscriptSpan();
                spannableString.SetSpan(span, start, end, SpanTypes.ExclusiveExclusive);
            }

            if (attributes.GetSuperscript())
            {
                var span = new SuperscriptSpan();
                spannableString.SetSpan(span, start, end, SpanTypes.ExclusiveExclusive);
            }

            if (attributes.GetStrikethrough())
            {
                var span = new StrikethroughSpan();
                spannableString.SetSpan(span, start, end, SpanTypes.ExclusiveExclusive);
            }

            if (attributes.GetUnorderedList())
            {
                var bulletSpan = new BulletSpan();
                spannableString.SetSpan(bulletSpan, start, end, SpanTypes.ExclusiveExclusive);
            }
        }