/// <summary>
        /// Creates and styles a new Text container (UIlabel, UITextView, UITextField)
        /// </summary>
        /// <param name="styleID">The CSS selector name for the style</param>
        /// <param name="text">Text to display including html tags</param>
        /// <param name="customTags">A list of custom <c>CSSTagStyle</c> instances that set the styling for the html</param>
        /// <param name="useExistingStyles">Existing CSS styles willl be used If set to <c>true</c></param>
        /// <param name="encoding">String encoding type</param>
        /// <typeparam name="T">Text container type (UIlabel, UITextView, UITextField)</typeparam>
        public static T Create <T> (string styleID, string text = "", List <CssTagStyle> customTags = null, bool useExistingStyles = true)
        {
            var isHTML = (!string.IsNullOrEmpty(text) && Common.MatchHtmlTags.IsMatch(text));
            var target = Activator.CreateInstance <T> ();

            // If this is a plain string view, style it and return it
            if (!string.IsNullOrEmpty(text) && !isHTML)
            {
                Style <T> (target, styleID, text);
                return(target);
            }
            else if (isHTML)
            {
                SetBaseStyle(styleID, ref customTags);
            }

            var formattedText = isHTML ?
                                TextStyle.CreateHtmlString(text, customTags, useExistingStyles) :
                                TextStyle.CreateStyledString(styleID, text);

            var type = typeof(T);

            if (type == typeLabel)
            {
                var label = target as UILabel;
                label.AttributedText = formattedText;
                if (!isHTML)
                {
                    StyleUILabel(label, GetStyle(styleID), true);
                }
            }
            else if (type == typeTextView)
            {
                var textView = target as UITextView;
                textView.AttributedText = formattedText;
                if (!isHTML)
                {
                    StyleUITextView(textView, GetStyle(styleID), true);
                }
            }
            else if (type == typeTextField)
            {
                var textField = target as UITextField;
                textField.AttributedText = formattedText;
                if (!isHTML)
                {
                    StyleUITextField(textField, GetStyle(styleID), true);
                }
            }
            else
            {
                throw new NotSupportedException("The specified type is not supported, please use a UILabel, UITextView or UITextField: " + type.ToString());
            }

            return(target);
        }
Exemple #2
0
        public void UpdateText(string value = null)
        {
            if (!String.IsNullOrEmpty(value))
            {
                _rawText = value;
            }

            var style = TextStyle.GetStyle(StyleID);

            TextValue = TextStyle.ParseString(style, _rawText);

            AttributedValue = ContainsHtml ? TextStyle.CreateHtmlString(TextValue, CustomTags) : TextStyle.CreateStyledString(style, TextValue);
        }