Exemple #1
0
        public ISpanned CreateStyledString(TextStyleParameters style, string text, int startIndex = 0, int endIndex = -1)
        {
            if (text.Length == 0)
            {
                return(null);
            }

            if (endIndex == -1)
            {
                endIndex = text.Length;
            }

            if (startIndex >= endIndex)
            {
                throw new Exception("Unable to create styled string, StartIndex is too high: " + startIndex);
            }

            // Parse the text
            text = ParseString(style, text);

            // If we have HMTL
            if (!string.IsNullOrEmpty(text) && Common.MatchHtmlTags.IsMatch(text))
            {
                return(CreateHtmlString(text, style.Name));
            }
            else
            {
                var builder = new SpannableStringBuilder(text);
                var font    = GetFont(style.Font);
                var span    = new CustomTypefaceSpan("", font, style);

                builder.SetSpan(span, startIndex, endIndex, SpanTypes.ExclusiveExclusive);
                return(builder);
            }
        }
Exemple #2
0
        /// <summary>
        /// Handles a custom tag
        /// </summary>
        /// <returns>void</returns>
        /// <param name="opening">bool</param>
        /// <param name="tag">string</param>
        /// <param name="output">IEditable</param>
        /// <param name="xmlReader">IXMLReader</param>
        public void HandleTag(bool opening, string tag, IEditable output, Org.Xml.Sax.IXMLReader xmlReader)
        {
            TextStyleParameters style = _styles.ContainsKey(tag) ? _styles [tag] : null;

            // Body overwrites the inline styles so we set that at the textview level
            if (style != null)
            {
                var text = output as SpannableStringBuilder;

                if (opening)
                {
                    Start(text, new TextStylesObject());
                }
                else
                {
                    // Retrieve font
                    Typeface font = null;
                    if (!string.IsNullOrEmpty(style.Font))
                    {
                        _instance._typeFaces.TryGetValue(style.Font, out font);
                    }

                    var customSpan = new CustomTypefaceSpan("", font, style);
                    End(style, text, Class.FromType(typeof(TextStylesObject)), customSpan);
                }
            }
        }
Exemple #3
0
        public void Style <T>(T target, string styleID, string text = null, List <CssTag> customTags = null, bool useExistingStyles = true)
        {
            var style    = GetStyle(styleID);
            var type     = typeof(T);
            var textView = (type == typeTextView) ? target as TextView : target as EditText;

            if (textView == null)
            {
                throw new NotSupportedException("The specified type is not supported, please use a TextView or EditText: " + type.ToString());
            }

            text = text ?? textView.Text;
            var isHTML = (!string.IsNullOrEmpty(text) && Common.MatchHtmlTags.IsMatch(text));

            // Style the TextView
            if (isHTML)
            {
                StyleTextView(textView, style, true);
                var html = CreateHtmlString(text, styleID, customTags, useExistingStyles);
                textView.SetText(html, TextView.BufferType.Spannable);
            }
            else if (style.RequiresHtmlTags())
            {
                StyleTextView(textView, style, false);

                var builder = new SpannableStringBuilder(ParseString(style, text));
                var font    = GetFont(style.Font);
                var span    = new CustomTypefaceSpan("", font, style);

                builder.SetSpan(span, 0, builder.Length(), SpanTypes.ExclusiveExclusive);
                textView.SetText(builder, TextView.BufferType.Spannable);
            }
            else
            {
                StyleTextView(textView, style, true);
                textView.SetText(ParseString(style, text), TextView.BufferType.Normal);
            }
        }