示例#1
0
        public static ISpanned CreateStyledString(TextStyleParameters style, string text, int startIndex = 0, int endIndex = -1)
        {
            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);

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

            if (isHTML)
            {
                return(CreateHtmlString(text, style.Name));
            }
            else
            {
                var builder = new SpannableStringBuilder(text);
                var font    = instance.GetFont(style.Font);
                var span    = new CustomTypefaceSpan("", font, style);

                builder.SetSpan(span, startIndex, endIndex, SpanTypes.ExclusiveExclusive);
                return(builder);
            }
        }
示例#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;

            // Body overwrites the inline styles so we set that at the textview level
            if (_textStyles.TryGetValue(tag, out style))
            {
                var text = output as SpannableStringBuilder;

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

                    var customSpan = new CustomTypefaceSpan("", font, style);
                    End(style, text, Class.FromType(typeof(TextStylesObject)), customSpan);
                }
            }
        }
示例#3
0
        public static void Style <T> (T target, string styleID, string text = null, List <CssTagStyle> customTags = null, bool useExistingStyles = true, Encoding encoding = null)
        {
            var style    = GetStyle(styleID);
            var type     = typeof(T);
            var isHTML   = (!string.IsNullOrEmpty(text) && Common.MatchHtmlTags.IsMatch(text));
            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;

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

                var builder = new SpannableStringBuilder(ParseString(style, text));
                var font    = instance.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);
            }
        }
示例#4
0
        /// <summary>
        /// Convert the source to an ISpanned instance.
        /// </summary>
        public ISpanned Convert()
        {
            _reader.ContentHandler = this;

            try {
                _reader.Parse(new InputSource(new StringReader(_htmlSource)));
            } catch (Exception e) {
                // We are reading from a string. There should not be IO problems.
                throw e;
            }

            // Fix flags and range for paragraph-type markup.
            int startIndex;
            int endIndex;

            var obj = _spannableStringBuilder.GetSpans(0, _spannableStringBuilder.Length(), Java.Lang.Class.FromType(typeof(IParagraphStyle)));

            for (int i = 0; i < obj.Length; i++)
            {
                startIndex = _spannableStringBuilder.GetSpanStart(obj [i]);
                endIndex   = _spannableStringBuilder.GetSpanEnd(obj [i]);

                // If the last line of the range is blank, back off by one.
                if (endIndex - 2 >= 0)
                {
                    if (_spannableStringBuilder.CharAt(endIndex - 1) == '\n' &&
                        _spannableStringBuilder.CharAt(endIndex - 2) == '\n')
                    {
                        endIndex--;
                    }
                }

                if (endIndex == startIndex)
                {
                    _spannableStringBuilder.RemoveSpan(obj [i]);
                }
                else
                {
                    _spannableStringBuilder.SetSpan(obj [i], startIndex, endIndex, SpanTypes.Paragraph);
                }
            }

            // loop through spans and apply text formating where needed
            if (_defaultStyle?.TextTransform != TextStyleTextTransform.None)
            {
                var allSpans = _spannableStringBuilder.GetSpans(0, _spannableStringBuilder.Length(), Java.Lang.Class.FromType(typeof(Java.Lang.Object)));

                // Pre first span
                var firstSpanIndex = (allSpans.Length > 0) ? _spannableStringBuilder.GetSpanStart(allSpans [0]) : 0;
                if (firstSpanIndex > 0)
                {
                    TransformTextRange(_spannableStringBuilder, _defaultStyle, 0, firstSpanIndex);
                }

                // In between spans & last span
                for (int i = 0; i < allSpans.Length; i++)
                {
                    startIndex = _spannableStringBuilder.GetSpanEnd(allSpans [i]);
                    endIndex   = (i + 1 < allSpans.Length) ? _spannableStringBuilder.GetSpanStart(allSpans [i + 1]) : _spannableStringBuilder.Length();
                    TransformTextRange(_spannableStringBuilder, _defaultStyle, startIndex, endIndex);
                }
            }

            // Apply the default style to the entire text range
            if (_defaultStyle != null)
            {
                Typeface font = null;
                if (!string.IsNullOrEmpty(_defaultStyle.Font))
                {
                    TextStyle.Instance._typeFaces.TryGetValue(_defaultStyle.Font, out font);

                    var customSpan = new CustomTypefaceSpan("", font, _defaultStyle);
                    _spannableStringBuilder.SetSpan(customSpan, 0, _spannableStringBuilder.Length(), SpanTypes.ExclusiveExclusive);
                }
            }

            return(_spannableStringBuilder);
        }