internal static string ParseString(TextStyleParameters style, string text) { // Text transformations if (!string.IsNullOrEmpty(text)) { if (style.TextTransform != TextStyleTextTransform.None) { switch (style.TextTransform) { case TextStyleTextTransform.UpperCase: text = text.ToUpper(); break; case TextStyleTextTransform.LowerCase: text = text.ToLower(); break; case TextStyleTextTransform.Capitalize: text = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(text.ToLower()); break; } } } return(text); }
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); } }
static void UpdateMargins(TextStyleParameters style, ref UIEdgeInsets inset) { inset.Top = (style.PaddingTop > float.MinValue) ? style.PaddingTop : inset.Top; inset.Bottom = (style.PaddingBottom > float.MinValue) ? style.PaddingBottom : inset.Bottom; inset.Left = (style.PaddingLeft > float.MinValue) ? style.PaddingLeft : inset.Left; inset.Right = (style.PaddingRight > float.MinValue) ? style.PaddingRight : inset.Right; }
/// <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); } } }
internal static void StyleUITextField(UITextField target, TextStyleParameters style, bool setFonts) { var attribs = GetStringAttributes(style); target.TextColor = attribs.ForegroundColor; // If setting the font attributes if (setFonts) { target.Font = attribs.Font; } target.TextAlignment = GetAlignment(style.TextAlign); // Padding if (style.Padding != null) { var padding = style.Padding; target.LayoutMargins = new UIEdgeInsets(padding [0], padding [1], padding [2], padding [3]); } else { var curInset = target.LayoutMargins; UpdateMargins(style, ref curInset); target.LayoutMargins = curInset; } }
/// <summary> /// Initializes a new instance of the <see cref="T:TextStyles.Android.CustomHtmlParser"/> class. /// </summary> /// <param name="source">Source.</param> /// <param name="textStyles">Text styles.</param> /// <param name="defaultStyleID">Default style identifier.</param> public CustomHtmlParser(string source, Dictionary <string, TextStyleParameters> textStyles, string defaultStyleID = null) { _htmlSource = source; _styles = textStyles; _defaultStyle = String.IsNullOrEmpty(defaultStyleID) ? null : _styles [defaultStyleID]; _spannableStringBuilder = new SpannableStringBuilder(); _reader = XMLReaderFactory.CreateXMLReader("org.ccil.cowan.tagsoup.Parser"); _imageGetter = null; _tagHandler = new CustomTagHandler(textStyles); }
/// <summary> /// Transforms the text case within the given range. /// </summary> /// <returns>The text range.</returns> /// <param name="text">Text.</param> /// <param name="style">Style.</param> /// <param name="startIndex">Start index.</param> /// <param name="endIndex">End index.</param> static void TransformTextRange(SpannableStringBuilder text, TextStyleParameters style, int startIndex, int endIndex) { if (startIndex == endIndex) { return; } var transformed = TextStyle.ParseString(style, text.SubSequence(startIndex, endIndex)); text.Replace(startIndex, endIndex, transformed); }
internal NSAttributedString ParseHtmlString(TextStyleParameters style, string text) { var attribs = GetStringAttributes(style, DefaultTextSize); text = ParseString(style, text); var prettyString = new NSMutableAttributedString(text); prettyString.AddAttributes(attribs, new NSRange(0, text.Length)); return(prettyString); }
/// <summary> /// Initializes a new instance of the <see cref="T:TextStyles.Android.CustomHtmlParser"/> class. /// </summary> /// <param name="source">Source.</param> /// <param name="textStyles">Text styles.</param> /// <param name="defaultStyleID">Default style identifier.</param> public CustomHtmlParser(TextStyle instance, string source, Dictionary <string, TextStyleParameters> textStyles, string defaultStyleID = null) { _instance = instance; _htmlSource = source; _styles = textStyles; if (!String.IsNullOrEmpty(defaultStyleID) && _styles.ContainsKey(defaultStyleID)) { _defaultStyle = _styles [defaultStyleID]; } _spannableStringBuilder = new SpannableStringBuilder(); _reader = XMLReaderFactory.CreateXMLReader("org.ccil.cowan.tagsoup.Parser"); _imageGetter = null; _tagHandler = new CustomTagHandler(_instance, _styles); }
/// <summary> /// Creates a styled string as an NSAttibutedString /// </summary> /// <returns>The styled string.</returns> /// <param name="style">TextStyleParameters for styling</param> /// <param name="text">Text to style</param> /// <param name="startIndex">Style start index</param> /// <param name="endIndex">Style end index</param> public NSMutableAttributedString CreateStyledString(TextStyleParameters style, string text, int startIndex = 0, int endIndex = -1) { var attribs = GetStringAttributes(style, DefaultTextSize); text = ParseString(style, text); if (endIndex == -1) { endIndex = text.Length; } var prettyString = new NSMutableAttributedString(text); prettyString.SetAttributes(attribs, new NSRange(startIndex, endIndex)); return(prettyString); }
/// <summary> /// End the specified tag /// </summary> /// <param name="style">TextStyleParameters</param> /// <param name="text">SpannableStringBuilder</param> /// <param name="kind">Class</param> /// <param name="newSpan">Java.Lang.Object</param> static void End(TextStyleParameters style, SpannableStringBuilder text, Class kind, Java.Lang.Object newSpan) { var length = text.Length(); var span = GetLast(text, kind); var start = text.GetSpanStart(span); text.RemoveSpan(span); // Parse the text in the span var parsedString = TextStyle.ParseString(style, text.SubSequence(start, length)); // Note this hardcodes the text this way and only works on parsed tags! text.Replace(start, length, parsedString); if (start != length) { text.SetSpan(newSpan, start, length, SpanTypes.InclusiveExclusive); } }
internal static void StyleUILabel(UILabel target, TextStyleParameters style, bool setFonts) { // TODO fix this as its not helping as it stands var attribs = GetStringAttributes(style); target.TextColor = attribs.ForegroundColor; // If setting the font attributes if (setFonts) { target.Font = attribs.Font; } // Lines if (style.Lines > int.MinValue) { target.Lines = style.Lines; } // Text Alignment target.TextAlignment = GetAlignment(style.TextAlign); // Overflow switch (style.TextOverflow) { case TextStyleTextOverflow.Ellipsis: target.LineBreakMode = UILineBreakMode.TailTruncation; break; case TextStyleTextOverflow.Clip: target.LineBreakMode = UILineBreakMode.Clip; break; default: target.LineBreakMode = UILineBreakMode.WordWrap; break; } }
public static bool RequiresHtmlTags(this TextStyleParameters target) { if (target.TextDecoration != TextStyleDecoration.None) { return(true); } if (Math.Abs(target.LetterSpacing) > 0) { return(true); } if (target.FontStyle == TextStyleFontStyle.Italic) { return(true); } if (target.FontWeight == TextStyleFontWeight.Bold) { return(true); } return(false); }
// TODO implement this function for styling plain text views, for html based views perhaps not internal static void StyleTextView(TextView target, TextStyleParameters style, bool isPlainText) { var fontSize = (style.FontSize <= 0f) ? DefaultTextSize : style.FontSize; target.SetTextSize(ComplexUnitType.Sp, fontSize); // Plain text fonts and colors if (isPlainText) { if (!String.IsNullOrEmpty(style.Color)) { target.SetTextColor(Color.White.FromHex(style.Color)); } if (!String.IsNullOrEmpty(style.Font) && instance._typeFaces.ContainsKey(style.Font)) { target.Typeface = instance._typeFaces [style.Font]; target.PaintFlags = target.PaintFlags | PaintFlags.SubpixelText; } } // Lines if (style.Lines > 0) { target.SetLines(style.Lines); } if (!string.IsNullOrEmpty(style.BackgroundColor)) { target.SetBackgroundColor(Color.White.FromHex(style.BackgroundColor)); } // Does nothing on the string attribs, needs to be part of a NSMutableAttributedString if (Math.Abs(style.LineHeight) > 0) { target.SetLineSpacing(0, style.LineHeight / fontSize); } // Assing the text gravity target.TextAlignment = TextAlignment.Gravity; switch (style.TextAlign) { case TextStyleAlign.Center: target.Gravity = GravityFlags.CenterHorizontal; break; case TextStyleAlign.Justified: target.Gravity = GravityFlags.FillHorizontal; break; case TextStyleAlign.Right: target.Gravity = GravityFlags.Right; break; default: target.Gravity = GravityFlags.Left; break; } // Padding target.SetPadding( (style.PaddingLeft > float.MinValue) ? (int)style.PaddingLeft : target.PaddingLeft, (style.PaddingTop > float.MinValue) ? (int)style.PaddingTop : target.PaddingTop, (style.PaddingRight > float.MinValue) ? (int)style.PaddingRight : target.PaddingRight, (style.PaddingBottom > float.MinValue) ? (int)style.PaddingBottom : target.PaddingBottom ); // Overflow // TODO implement this fully switch (style.TextOverflow) { case TextStyleTextOverflow.Ellipsis: target.Ellipsize = TextUtils.TruncateAt.End; break; default: break; } }
public CustomTypefaceSpan(String family, Typeface typeface, TextStyleParameters style) : base(family) { _typeface = typeface; _style = style; }
internal static UIStringAttributes GetStringAttributes(TextStyleParameters style, float defaultTextSize) { var stringAttribs = new UIStringAttributes(); var fontSize = style.FontSize; if (fontSize <= 0f) { fontSize = defaultTextSize; } if (!string.IsNullOrEmpty(style.Font)) { stringAttribs.Font = UIFont.FromName(style.Font, fontSize); } if (!string.IsNullOrEmpty(style.Color)) { stringAttribs.ForegroundColor = UIColor.Clear.FromHex(style.Color); } if (!string.IsNullOrEmpty(style.BackgroundColor)) { stringAttribs.BackgroundColor = UIColor.Clear.FromHex(style.BackgroundColor); } if (style.LetterSpacing > 0f) { stringAttribs.KerningAdjustment = style.LetterSpacing; } // Does nothing on the string attribs, needs to be part of a NSMutableAttributedString if (style.LineHeight != 0f) { var paragraphStyle = new NSMutableParagraphStyle() { LineHeightMultiple = style.LineHeight / fontSize, Alignment = GetAlignment(style.TextAlign) }; stringAttribs.ParagraphStyle = paragraphStyle; } if (style.TextDecoration != TextStyleDecoration.None) { switch (style.TextDecoration) { case TextStyleDecoration.LineThrough: stringAttribs.StrikethroughStyle = NSUnderlineStyle.Single; break; case TextStyleDecoration.Underline: stringAttribs.UnderlineStyle = NSUnderlineStyle.Single; break; } if (!string.IsNullOrEmpty(style.TextDecorationColor)) { stringAttribs.StrikethroughColor = UIColor.Clear.FromHex(style.TextDecorationColor); } } return(stringAttribs); }