summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.iOS/Renderers/FontExtensions.cs
blob: cbc2e6253a775a69c7bf40e988bfcf38e077828b (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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using UIKit;

namespace Xamarin.Forms.Platform.iOS
{
	public static class FontExtensions
	{
		static readonly Dictionary<ToUIFontKey, UIFont> ToUiFont = new Dictionary<ToUIFontKey, UIFont>();

		public static UIFont ToUIFont(this Font self)
		{
			var size = (float)self.FontSize;
			if (self.UseNamedSize)
			{
				switch (self.NamedSize)
				{
					case NamedSize.Micro:
						size = 12;
						break;
					case NamedSize.Small:
						size = 14;
						break;
					case NamedSize.Medium:
						size = 17; // as defined by iOS documentation
						break;
					case NamedSize.Large:
						size = 22;
						break;
					default:
						size = 17;
						break;
				}
			}

			var bold = self.FontAttributes.HasFlag(FontAttributes.Bold);
			var italic = self.FontAttributes.HasFlag(FontAttributes.Italic);

			if (self.FontFamily != null)
			{
				try
				{
					if (UIFont.FamilyNames.Contains(self.FontFamily))
					{
						var descriptor = new UIFontDescriptor().CreateWithFamily(self.FontFamily);

						if (bold || italic)
						{
							var traits = (UIFontDescriptorSymbolicTraits)0;
							if (bold)
								traits = traits | UIFontDescriptorSymbolicTraits.Bold;
							if (italic)
								traits = traits | UIFontDescriptorSymbolicTraits.Italic;

							descriptor = descriptor.CreateWithTraits(traits);
							return UIFont.FromDescriptor(descriptor, size);
						}
					}

					return UIFont.FromName(self.FontFamily, size);
				}
				catch
				{
					Debug.WriteLine("Could not load font named: {0}", self.FontFamily);
				}
			}
			if (bold && italic)
			{
				var defaultFont = UIFont.SystemFontOfSize(size);
				var descriptor = defaultFont.FontDescriptor.CreateWithTraits(UIFontDescriptorSymbolicTraits.Bold | UIFontDescriptorSymbolicTraits.Italic);
				return UIFont.FromDescriptor(descriptor, 0);
			}
			if (bold)
				return UIFont.BoldSystemFontOfSize(size);
			if (italic)
				return UIFont.ItalicSystemFontOfSize(size);
			return UIFont.SystemFontOfSize(size);
		}

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

		internal static UIFont ToUIFont(this Label label)
		{
			var values = label.GetValues(Label.FontFamilyProperty, Label.FontSizeProperty, Label.FontAttributesProperty);
			return ToUIFont((string)values[0], (float)(double)values[1], (FontAttributes)values[2]) ?? UIFont.SystemFontOfSize(UIFont.LabelFontSize);
		}

		internal static UIFont ToUIFont(this IFontElement element)
		{
			return ToUIFont(element.FontFamily, (float)element.FontSize, element.FontAttributes);
		}

		static UIFont _ToUIFont(string family, float size, FontAttributes attributes)
		{
			var bold = (attributes & FontAttributes.Bold) != 0;
			var italic = (attributes & FontAttributes.Italic) != 0;

			if (family != null)
			{
				try
				{
					UIFont result;
					if (UIFont.FamilyNames.Contains(family))
					{
						var descriptor = new UIFontDescriptor().CreateWithFamily(family);

						if (bold || italic)
						{
							var traits = (UIFontDescriptorSymbolicTraits)0;
							if (bold)
								traits = traits | UIFontDescriptorSymbolicTraits.Bold;
							if (italic)
								traits = traits | UIFontDescriptorSymbolicTraits.Italic;

							descriptor = descriptor.CreateWithTraits(traits);
							result = UIFont.FromDescriptor(descriptor, size);
							if (result != null)
								return result;
						}
					}

					result = UIFont.FromName(family, size);
					if (result != null)
						return result;
				}
				catch
				{
					Debug.WriteLine("Could not load font named: {0}", family);
				}
			}

			if (bold && italic)
			{
				var defaultFont = UIFont.SystemFontOfSize(size);

				var descriptor = defaultFont.FontDescriptor.CreateWithTraits(UIFontDescriptorSymbolicTraits.Bold | UIFontDescriptorSymbolicTraits.Italic);
				return UIFont.FromDescriptor(descriptor, 0);
			}
			if (bold)
				return UIFont.BoldSystemFontOfSize(size);
			if (italic)
				return UIFont.ItalicSystemFontOfSize(size);

			return UIFont.SystemFontOfSize(size);
		}

		static UIFont ToUIFont(string family, float size, FontAttributes attributes)
		{
			var key = new ToUIFontKey(family, size, attributes);

			lock (ToUiFont)
			{
				UIFont value;
				if (ToUiFont.TryGetValue(key, out value))
					return value;
			}

			var generatedValue = _ToUIFont(family, size, attributes);

			lock (ToUiFont)
			{
				UIFont value;
				if (!ToUiFont.TryGetValue(key, out value))
					ToUiFont.Add(key, value = generatedValue);
				return value;
			}
		}

		struct ToUIFontKey
		{
			internal ToUIFontKey(string family, float size, FontAttributes attributes)
			{
				_family = family;
				_size = size;
				_attributes = attributes;
			}
#pragma warning disable 0414 // these are not called explicitly, but they are used to establish uniqueness. allow it!
			string _family;
			float _size;
			FontAttributes _attributes;
#pragma warning restore 0414
		}
	}
}