summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.iOS/Renderers/FormattedStringExtensions.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Xamarin.Forms.Platform.iOS/Renderers/FormattedStringExtensions.cs')
-rw-r--r--Xamarin.Forms.Platform.iOS/Renderers/FormattedStringExtensions.cs82
1 files changed, 82 insertions, 0 deletions
diff --git a/Xamarin.Forms.Platform.iOS/Renderers/FormattedStringExtensions.cs b/Xamarin.Forms.Platform.iOS/Renderers/FormattedStringExtensions.cs
new file mode 100644
index 00000000..0dfa38cd
--- /dev/null
+++ b/Xamarin.Forms.Platform.iOS/Renderers/FormattedStringExtensions.cs
@@ -0,0 +1,82 @@
+#if __UNIFIED__
+using Foundation;
+using UIKit;
+
+#else
+using MonoTouch.Foundation;
+using MonoTouch.UIKit;
+#endif
+
+namespace Xamarin.Forms.Platform.iOS
+{
+ public static class FormattedStringExtensions
+ {
+ public static NSAttributedString ToAttributed(this Span span, Font defaultFont, Color defaultForegroundColor)
+ {
+ if (span == null)
+ return null;
+
+ var font = span.Font != Font.Default ? span.Font : defaultFont;
+
+ var fgcolor = span.ForegroundColor;
+ if (fgcolor.IsDefault)
+ fgcolor = defaultForegroundColor;
+ if (fgcolor.IsDefault)
+ fgcolor = Color.Black; // as defined by apple docs
+
+ return new NSAttributedString(span.Text, font == Font.Default ? null : font.ToUIFont(), fgcolor.ToUIColor(), span.BackgroundColor.ToUIColor());
+ }
+
+ public static NSAttributedString ToAttributed(this FormattedString formattedString, Font defaultFont, Color defaultForegroundColor)
+ {
+ if (formattedString == null)
+ return null;
+ var attributed = new NSMutableAttributedString();
+ foreach (var span in formattedString.Spans)
+ {
+ if (span.Text == null)
+ continue;
+
+ attributed.Append(span.ToAttributed(defaultFont, defaultForegroundColor));
+ }
+
+ return attributed;
+ }
+
+ internal static NSAttributedString ToAttributed(this Span span, Element owner, Color defaultForegroundColor)
+ {
+ if (span == null)
+ return null;
+
+ UIFont targetFont;
+ if (span.IsDefault())
+ targetFont = ((IFontElement)owner).ToUIFont();
+ else
+ targetFont = span.ToUIFont();
+
+ var fgcolor = span.ForegroundColor;
+ if (fgcolor.IsDefault)
+ fgcolor = defaultForegroundColor;
+ if (fgcolor.IsDefault)
+ fgcolor = Color.Black; // as defined by apple docs
+
+ return new NSAttributedString(span.Text, targetFont, fgcolor.ToUIColor(), span.BackgroundColor.ToUIColor());
+ }
+
+ internal static NSAttributedString ToAttributed(this FormattedString formattedString, Element owner, Color defaultForegroundColor)
+ {
+ if (formattedString == null)
+ return null;
+ var attributed = new NSMutableAttributedString();
+ foreach (var span in formattedString.Spans)
+ {
+ if (span.Text == null)
+ continue;
+
+ attributed.Append(span.ToAttributed(owner, defaultForegroundColor));
+ }
+
+ return attributed;
+ }
+ }
+} \ No newline at end of file