diff options
Diffstat (limited to 'Xamarin.Forms.Platform.Tizen/Native/Span.cs')
-rw-r--r-- | Xamarin.Forms.Platform.Tizen/Native/Span.cs | 294 |
1 files changed, 294 insertions, 0 deletions
diff --git a/Xamarin.Forms.Platform.Tizen/Native/Span.cs b/Xamarin.Forms.Platform.Tizen/Native/Span.cs new file mode 100644 index 00000000..6c479e34 --- /dev/null +++ b/Xamarin.Forms.Platform.Tizen/Native/Span.cs @@ -0,0 +1,294 @@ +using System.Text; +using EColor = ElmSharp.Color; + +namespace Xamarin.Forms.Platform.Tizen.Native +{ + /// <summary> + /// Represent a text with attributes applied. + /// </summary> + public class Span + { + string _text; + + /// <summary> + /// Gets or sets the formatted text. + /// </summary> + public FormattedString FormattedText { get; set; } + + /// <summary> + /// Gets or sets the text. + /// </summary> + /// <remarks> + /// Setting Text to a non-null value will set the FormattedText property to null. + /// </remarks> + public string Text + { + get + { + if (FormattedText != null) + { + return FormattedText.ToString(); + } + else + { + return _text; + } + } + set + { + if (value == null) + { + value = ""; + } + else + { + FormattedText = null; + } + _text = value; + } + } + + /// <summary> + /// Gets or sets the color for the text. + /// </summary> + public EColor ForegroundColor { get; set; } + + /// <summary> + /// Gets or sets the background color for the text. + /// </summary> + public EColor BackgroundColor { get; set; } + + /// <summary> + /// Gets or sets the font family for the text. + /// </summary> + public string FontFamily { get; set; } + + /// <summary> + /// Gets or sets the font attributes for the text. + /// See <see cref="FontAttributes"/> for information about FontAttributes. + /// </summary> + public FontAttributes FontAttributes { get; set; } + + /// <summary> + /// Gets or sets the font size for the text. + /// </summary> + public double FontSize { get; set; } + + /// <summary> + /// Gets or sets the line break mode for the text. + /// See <see cref="LineBreakMode"/> for information about LineBreakMode. + /// </summary> + public LineBreakMode LineBreakMode { get; set; } + + /// <summary> + /// Gets or sets the horizontal alignment mode for the text. + /// See <see cref="TextAlignment"/> for information about TextAlignment. + /// </summary> + public TextAlignment HorizontalTextAlignment { get; set; } + + /// <summary> + /// Gets or sets the vertical alignment mode for the text. + /// See <see cref="TextAlignment"/> for information about TextAlignment. + /// </summary> + public TextAlignment VerticalTextAlignment { get; set; } + + /// <summary> + /// Gets or sets the value that indicates whether the text has underline. + /// </summary> + public bool Underline { get; set; } + + /// <summary> + /// Gets or sets the value that indicates whether the text has strike line though it. + /// </summary> + public bool Strikethrough { get; set; } + + /// <summary> + /// Create a new Span instance with default attributes. + /// </summary> + public Span() + { + Text = ""; + FontFamily = ""; + FontSize = -1; + FontAttributes = FontAttributes.None; + ForegroundColor = EColor.White; + BackgroundColor = EColor.Transparent; + HorizontalTextAlignment = TextAlignment.Auto; + VerticalTextAlignment = TextAlignment.Auto; + LineBreakMode = LineBreakMode.MixedWrap; + Underline = false; + Strikethrough = false; + } + + /// <summary> + /// This method return marked up text + /// </summary> + internal string GetMarkupText() + { + StringBuilder sb = new StringBuilder(); + + sb.AppendFormat("<span "); + + sb = PrepareFormattingString(sb); + + sb.Append(">"); + + sb.Append(GetDecoratedText()); + + sb.Append("</>"); + + return sb.ToString(); + } + + /// <summary> + /// This method return text decorated with markup if FormattedText is set or plain text otherwise. + /// </summary> + internal string GetDecoratedText() + { + if (FormattedText != null) + { + return FormattedText.ToMarkupString(); + } + else + { + return ConvertTags(Text); + } + } + + StringBuilder PrepareFormattingString(StringBuilder _formattingString) + { + var foregroundColor = ForegroundColor.ToHex(); + + _formattingString.AppendFormat("color={0} ", foregroundColor); + + _formattingString.AppendFormat("backing_color={0} ", BackgroundColor.ToHex()); + _formattingString.Append("backing=on "); + + if (!string.IsNullOrEmpty(FontFamily)) + { + _formattingString.AppendFormat("font={0} ", FontFamily); + } + + if (FontSize != -1) + { + _formattingString.AppendFormat("font_size={0} ", FontSize); + } + + if ((FontAttributes & FontAttributes.Bold) != 0) + { + _formattingString.Append("font_weight=Bold "); + } + if ((FontAttributes & FontAttributes.Italic) != 0) + { + _formattingString.Append("font_style=italic "); + } + + if (Underline) + { + _formattingString.AppendFormat("underline=on underline_color={0} ", foregroundColor); + } + + if (Strikethrough) + { + _formattingString.AppendFormat("strikethrough=on strikethrough_color={0} ", foregroundColor); + } + + 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; + } + + switch (VerticalTextAlignment) + { + case TextAlignment.Auto: + case TextAlignment.Start: + _formattingString.Append("valign=top "); + break; + + case TextAlignment.End: + _formattingString.Append("valign=bottom "); + break; + + case TextAlignment.Center: + _formattingString.Append("valign=middle "); + break; + } + + switch (LineBreakMode) + { + case LineBreakMode.NoWrap: + _formattingString.Append("wrap=none"); + break; + + case LineBreakMode.CharacterWrap: + _formattingString.Append("wrap=char"); + break; + + case LineBreakMode.WordWrap: + _formattingString.Append("wrap=word"); + break; + + case LineBreakMode.MixedWrap: + _formattingString.Append("wrap=mixed"); + break; + + 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; + } + + return _formattingString; + } + + string ConvertTags(string text) + { + return text.Replace("<", "<") + .Replace(">", ">") + .Replace("\n", "<br>"); + } + + internal string GetStyle() + { + StringBuilder sb = new StringBuilder(); + + sb.Append("DEFAULT='"); + + PrepareFormattingString(sb); + + sb.Append("'"); + + return sb.ToString(); + } + + /// <summary> + /// Converts string value to Span. + /// </summary> + /// <param name="text">The string text</param> + public static implicit operator Span(string text) + { + return new Span { Text = text }; + } + } +} |