/// <summary> /// Creates a new attributed string from the specified styled text and theme. /// </summary> /// <param name="styledText">The styled text.</param> /// <param name="theme">The theme to apply.</param> /// <param name="fontSize">The size of the font to use.</param> /// <returns>A new attributed string.</returns> public static NSMutableAttributedString FromStyledText(StyledText styledText, LabelTheme theme, int fontSize) { string text = styledText.ToString(); NSMutableAttributedString attributedText = new NSMutableAttributedString(text); NSRange fullRange = new NSRange(0, text.Length); foreach (StyledText.TextPart part in styledText.Parts) { FontStyle fontStyle = FontStyle.None; if (part.Style == StyledText.Style.Bold) { fontStyle = FontStyle.Bold; } UIFont font = string.IsNullOrEmpty(theme.FontName) ? UIFont.SystemFontOfSize(fontSize) : UIFont.FromName(theme.FontName, fontSize); font = font.ApplyStyle(fontStyle); NSRange range = new NSRange(part.StartIndex, part.Text.Length); attributedText.AddAttribute(UIStringAttributeKey.Font, font, range); } attributedText.AddAttribute(UIStringAttributeKey.BackgroundColor, theme.BackgroundColor.ToUIColor(), fullRange); attributedText.AddAttribute(UIStringAttributeKey.ForegroundColor, theme.FontColor.ToUIColor(), fullRange); return(attributedText); }
public void ToStringTest() { var a = new StyledText("abc", Color.Red); var b = new StyledText("123", a); Assert.AreEqual("abc", a.ToString()); Assert.AreEqual("123", b.ToString()); }
public void IEnumerableToStringWithDelimiterTest() { var a = new StyledText("abc"); var b = new StyledText("123"); var c = new StyledText("xyz"); var v = new StyledText[] { a, b, c }; Assert.AreEqual("abcWWW123WWWxyz", StyledText.ToString(v, "WWW")); }
public void IEnumerableToStringTest() { var a = new StyledText("abc"); var b = new StyledText("123"); var c = new StyledText("xyz"); var v = new StyledText[] { a, b, c }; Assert.AreEqual("abc123xyz", StyledText.ToString(v)); }
public void EmptyIEnumerableToStringTest() { Assert.AreEqual(string.Empty, StyledText.ToString(new StyledText[0])); }
public void NullIEnumerableToStringWithDelimiterTest() { Assert.AreEqual(string.Empty, StyledText.ToString(null, "WWW")); }
public void NullIEnumerableToStringTest() { Assert.AreEqual(string.Empty, StyledText.ToString(null)); }