using System; using System.Text; using EColor = ElmSharp.Color; using Specific = Xamarin.Forms.PlatformConfiguration.TizenSpecific; namespace Xamarin.Forms.Platform.Tizen.Native { /// /// Represent a text with attributes applied. /// public class Span { static EColor s_defaultLineColor = EColor.Black; string _text; /// /// Gets or sets the formatted text. /// public FormattedString FormattedText { get; set; } /// /// Gets or sets the text. /// /// /// Setting Text to a non-null value will set the FormattedText property to null. /// public string Text { get { if (FormattedText != null) { return FormattedText.ToString(); } else { return _text; } } set { if (value == null) { value = ""; } else { FormattedText = null; } _text = value; } } /// /// Gets or sets the color for the text. /// public EColor ForegroundColor { get; set; } /// /// Gets or sets the background color for the text. /// public EColor BackgroundColor { get; set; } /// /// Gets or sets the font family for the text. /// public string FontFamily { get; set; } /// /// Gets or sets the font attributes for the text. /// See for information about FontAttributes. /// public FontAttributes FontAttributes { get; set; } /// /// Gets or sets the font size for the text. /// public double FontSize { get; set; } /// /// Gets or sets the font weight for the text. /// public string FontWeight { get; set; } /// /// Gets or sets the line break mode for the text. /// See for information about LineBreakMode. /// public LineBreakMode LineBreakMode { get; set; } /// /// Gets or sets the horizontal alignment mode for the text. /// See for information about TextAlignment. /// public TextAlignment HorizontalTextAlignment { get; set; } /// /// Gets or sets the value that indicates whether the text has underline. /// public bool Underline { get; set; } /// /// Gets or sets the value that indicates whether the text has strike line though it. /// public bool Strikethrough { get; set; } /// /// Create a new Span instance with default attributes. /// public Span() { Text = ""; FontFamily = ""; FontSize = -1; FontWeight = Specific.FontWeight.None; FontAttributes = FontAttributes.None; ForegroundColor = EColor.Default; BackgroundColor = EColor.Default; HorizontalTextAlignment = TextAlignment.None; LineBreakMode = LineBreakMode.None; Underline = false; Strikethrough = false; } /// /// This method return marked up text /// internal string GetMarkupText() { StringBuilder sb = new StringBuilder(); sb.AppendFormat(""); sb.Append(GetDecoratedText()); sb.Append(""); return sb.ToString(); } /// /// This method return text decorated with markup if FormattedText is set or plain text otherwise. /// public string GetDecoratedText() { if (FormattedText != null) { return FormattedText.ToMarkupString(); } else { return ConvertTags(Text); } } StringBuilder PrepareFormattingString(StringBuilder _formattingString) { if (!ForegroundColor.IsDefault) { _formattingString.AppendFormat("color={0} ", ForegroundColor.ToHex()); } if (!BackgroundColor.IsDefault) { _formattingString.AppendFormat("backing_color={0} backing=on ", BackgroundColor.ToHex()); } if (!string.IsNullOrEmpty(FontFamily)) { _formattingString.AppendFormat("font={0} ", FontFamily); } if (FontSize != -1) { _formattingString.AppendFormat("font_size={0} ", Forms.ConvertToEflFontPoint(FontSize)); } if ((FontAttributes & FontAttributes.Bold) != 0) { _formattingString.Append("font_weight=Bold "); } else { // FontWeight is only available in case of FontAttributes.Bold is not used. if(FontWeight != Specific.FontWeight.None) { _formattingString.AppendFormat("font_weight={0} ", FontWeight); } } if ((FontAttributes & FontAttributes.Italic) != 0) { _formattingString.Append("font_style=italic "); } if (Underline) { _formattingString.AppendFormat("underline=on underline_color={0} ", ForegroundColor.IsDefault ? s_defaultLineColor.ToHex() : ForegroundColor.ToHex()); } if (Strikethrough) { _formattingString.AppendFormat("strikethrough=on strikethrough_color={0} ", ForegroundColor.IsDefault ? s_defaultLineColor.ToHex() : ForegroundColor.ToHex()); } switch (HorizontalTextAlignment) { case TextAlignment.Auto: _formattingString.Append("align=auto "); break; case TextAlignment.Start: _formattingString.Append("align=left "); break; case TextAlignment.End: _formattingString.Append("align=right "); break; case TextAlignment.Center: _formattingString.Append("align=center "); break; case TextAlignment.None: break; } switch (LineBreakMode) { case LineBreakMode.HeadTruncation: _formattingString.Append("ellipsis=0.0"); break; case LineBreakMode.MiddleTruncation: _formattingString.Append("ellipsis=0.5"); break; case LineBreakMode.TailTruncation: _formattingString.Append("ellipsis=1.0"); break; case LineBreakMode.NoWrap: case LineBreakMode.CharacterWrap: case LineBreakMode.WordWrap: case LineBreakMode.MixedWrap: case LineBreakMode.None: break; } return _formattingString; } string ConvertTags(string text) { return text.Replace("&", "&") .Replace("<", "<") .Replace(">", ">") .Replace(Environment.NewLine, "
"); } public string GetStyle() { StringBuilder sb = new StringBuilder(); sb.Append("DEFAULT='"); PrepareFormattingString(sb); sb.Append("'"); return sb.ToString(); } /// /// Converts string value to Span. /// /// The string text public static implicit operator Span(string text) { return new Span { Text = text }; } } }