summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.WP8/FontExtensions.cs
blob: dc4690ea50e6e4a5f8c4aa0a399c4e199cdc0777 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
´╗┐using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;

namespace Xamarin.Forms.Platform.WinPhone
{
	public static class FontExtensions
	{
		public static void ApplyFont(this Control self, Font font)
		{
			if (font.UseNamedSize)
			{
				switch (font.NamedSize)
				{
					case NamedSize.Micro:
						self.FontSize = (double)System.Windows.Application.Current.Resources["PhoneFontSizeSmall"] - 3;
						break;
					case NamedSize.Small:
						self.FontSize = (double)System.Windows.Application.Current.Resources["PhoneFontSizeSmall"];
						break;
					case NamedSize.Medium:
						self.FontSize = (double)System.Windows.Application.Current.Resources["PhoneFontSizeMedium"];
						// use medium instead of normal as this is the default for non-labels
						break;
					case NamedSize.Large:
						self.FontSize = (double)System.Windows.Application.Current.Resources["PhoneFontSizeLarge"];
						break;
					default:
						throw new ArgumentOutOfRangeException();
				}
			}
			else
				self.FontSize = font.FontSize;

			if (!string.IsNullOrEmpty(font.FontFamily))
				self.FontFamily = new FontFamily(font.FontFamily);
			else
				self.FontFamily = (FontFamily)System.Windows.Application.Current.Resources["PhoneFontFamilySemiBold"];

			if (font.FontAttributes.HasFlag(FontAttributes.Italic))
				self.FontStyle = FontStyles.Italic;
			else
				self.FontStyle = FontStyles.Normal;

			if (font.FontAttributes.HasFlag(FontAttributes.Bold))
				self.FontWeight = FontWeights.Bold;
			else
				self.FontWeight = FontWeights.Normal;
		}

		public static void ApplyFont(this TextBlock self, Font font)
		{
			if (font.UseNamedSize)
			{
				switch (font.NamedSize)
				{
					case NamedSize.Micro:
						self.FontSize = (double)System.Windows.Application.Current.Resources["PhoneFontSizeSmall"] - 3;
						break;
					case NamedSize.Small:
						self.FontSize = (double)System.Windows.Application.Current.Resources["PhoneFontSizeSmall"];
						break;
					case NamedSize.Medium:
						self.FontSize = (double)System.Windows.Application.Current.Resources["PhoneFontSizeNormal"];
						// use normal instead of  medium as this is the default
						break;
					case NamedSize.Large:
						self.FontSize = (double)System.Windows.Application.Current.Resources["PhoneFontSizeLarge"];
						break;
					default:
						throw new ArgumentOutOfRangeException();
				}
			}
			else
				self.FontSize = font.FontSize;

			if (!string.IsNullOrEmpty(font.FontFamily))
				self.FontFamily = new FontFamily(font.FontFamily);
			else
				self.FontFamily = (FontFamily)System.Windows.Application.Current.Resources["PhoneFontFamilyNormal"];

			if (font.FontAttributes.HasFlag(FontAttributes.Italic))
				self.FontStyle = FontStyles.Italic;
			else
				self.FontStyle = FontStyles.Normal;

			if (font.FontAttributes.HasFlag(FontAttributes.Bold))
				self.FontWeight = FontWeights.Bold;
			else
				self.FontWeight = FontWeights.Normal;
		}

		public static void ApplyFont(this System.Windows.Documents.TextElement self, Font font)
		{
			if (font.UseNamedSize)
			{
				switch (font.NamedSize)
				{
					case NamedSize.Micro:
						self.FontSize = (double)System.Windows.Application.Current.Resources["PhoneFontSizeSmall"] - 3;
						break;
					case NamedSize.Small:
						self.FontSize = (double)System.Windows.Application.Current.Resources["PhoneFontSizeSmall"];
						break;
					case NamedSize.Medium:
						self.FontSize = (double)System.Windows.Application.Current.Resources["PhoneFontSizeNormal"];
						// use normal instead of  medium as this is the default
						break;
					case NamedSize.Large:
						self.FontSize = (double)System.Windows.Application.Current.Resources["PhoneFontSizeLarge"];
						break;
					default:
						throw new ArgumentOutOfRangeException();
				}
			}
			else
				self.FontSize = font.FontSize;

			if (!string.IsNullOrEmpty(font.FontFamily))
				self.FontFamily = new FontFamily(font.FontFamily);
			else
				self.FontFamily = (FontFamily)System.Windows.Application.Current.Resources["PhoneFontFamilyNormal"];

			if (font.FontAttributes.HasFlag(FontAttributes.Italic))
				self.FontStyle = FontStyles.Italic;
			else
				self.FontStyle = FontStyles.Normal;

			if (font.FontAttributes.HasFlag(FontAttributes.Bold))
				self.FontWeight = FontWeights.Bold;
			else
				self.FontWeight = FontWeights.Normal;
		}

		internal static void ApplyFont(this Control self, IFontElement element)
		{
			self.FontSize = element.FontSize;

			if (!string.IsNullOrEmpty(element.FontFamily))
				self.FontFamily = new FontFamily(element.FontFamily);
			else
				self.FontFamily = (FontFamily)System.Windows.Application.Current.Resources["PhoneFontFamilySemiBold"];

			if (element.FontAttributes.HasFlag(FontAttributes.Italic))
				self.FontStyle = FontStyles.Italic;
			else
				self.FontStyle = FontStyles.Normal;

			if (element.FontAttributes.HasFlag(FontAttributes.Bold))
				self.FontWeight = FontWeights.Bold;
			else
				self.FontWeight = FontWeights.Normal;
		}

		internal static bool IsDefault(this IFontElement self)
		{
			return self.FontFamily == null && self.FontSize == Device.GetNamedSize(NamedSize.Default, typeof(Label), true) && self.FontAttributes == FontAttributes.None;
		}
	}
}