using System; using System.Collections.Generic; using System.Linq; namespace Xamarin.Forms.Platform.Tizen.Native { /// /// Represents a text with attributes applied to some parts. /// /// /// Formatted string consists of spans that represent text segments with various attributes applied. /// public class FormattedString { /// /// A flag indicating whether the instance contains just a plain string without any formatting. /// /// /// true if the instance contains an unformatted string. /// readonly bool _just_string; /// /// Holds the unformatted string. /// /// /// The contents of this field are accurate if and only if the _just_string flag is set. /// readonly string _string; /// /// Holds the collection of span elements. /// /// /// Span elements are basically chunks of text with uniform formatting. /// readonly ObservableCollection _spans; /// /// Returns the collection of span elements. /// public IList Spans { get { return _spans; } } /// /// Creates a new FormattedString instance with an empty string. /// public FormattedString() { _just_string = false; _spans = new ObservableCollection(); } /// /// Creates a new FormattedString instance based on given str. /// /// /// A string used to make a new FormattedString instance. /// public FormattedString(string str) { _just_string = true; _string = str; } /// /// Returns the plain text of the FormattedString as an unformatted string. /// /// /// The text content of the FormattedString without any format applied. /// public override string ToString() { if (_just_string) { return _string; } else { return string.Concat(from span in this.Spans select span.Text); } } /// /// Returns the markup text representation of the FormattedString instance. /// /// The string containing a markup text. internal string ToMarkupString() { if (_just_string) { return _string; } else { return string.Concat(from span in Spans select span.GetMarkupText()); } } /// /// Casts the FormattedString to a string. /// /// The FormattedString instance which will be used for the conversion. public static explicit operator string (FormattedString formatted) { return formatted.ToString(); } /// /// Casts the string to a FormattedString. /// /// The text which will be put in a new FormattedString instance. public static implicit operator FormattedString(string text) { return new FormattedString(text); } /// /// Casts the Span to a FormattedString. /// /// The span which will be used for the conversion. public static implicit operator FormattedString(Span span) { return new FormattedString() { Spans = { span } }; } } }