summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.iOS/Renderers/ButtonRenderer.cs
blob: 3fa84d1d9ccdefa37962ca27686de839b3807ef0 (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
189
190
using System;
using System.Drawing;
using System.Linq;
using System.ComponentModel;
#if __UNIFIED__
using UIKit;
using CoreGraphics;
#else
using MonoTouch.UIKit;
using MonoTouch.CoreGraphics;
#endif
#if __UNIFIED__
using RectangleF = CoreGraphics.CGRect;
using SizeF = CoreGraphics.CGSize;
using PointF = CoreGraphics.CGPoint;

#else
using nfloat=System.Single;
using nint=System.Int32;
using nuint=System.UInt32;
#endif

namespace Xamarin.Forms.Platform.iOS
{
	public class ButtonRenderer : ViewRenderer<Button, UIButton>
	{
		UIColor _buttonTextColorDefaultDisabled;
		UIColor _buttonTextColorDefaultHighlighted;
		UIColor _buttonTextColorDefaultNormal;

		public override SizeF SizeThatFits(SizeF size)
		{
			var result = base.SizeThatFits(size);
			result.Height = 44; // Apple docs
			//Compensate for the insets
			if (!Control.ImageView.Hidden)
				result.Width += 10;
			return result;
		}

		protected override void Dispose(bool disposing)
		{
			if (Control != null)
				Control.TouchUpInside -= OnButtonTouchUpInside;

			base.Dispose(disposing);
		}

		protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
		{
			base.OnElementChanged(e);

			if (e.NewElement != null)
			{
				if (Control == null)
				{
					SetNativeControl(new UIButton(UIButtonType.RoundedRect));

					_buttonTextColorDefaultNormal = Control.TitleColor(UIControlState.Normal);
					_buttonTextColorDefaultHighlighted = Control.TitleColor(UIControlState.Highlighted);
					_buttonTextColorDefaultDisabled = Control.TitleColor(UIControlState.Disabled);

					Control.TouchUpInside += OnButtonTouchUpInside;
				}

				UpdateText();
				UpdateFont();
				UpdateBorder();
				UpdateImage();
				UpdateTextColor();
			}
		}

		protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
		{
			base.OnElementPropertyChanged(sender, e);

			if (e.PropertyName == Button.TextProperty.PropertyName)
				UpdateText();
			else if (e.PropertyName == Button.TextColorProperty.PropertyName)
				UpdateTextColor();
			else if (e.PropertyName == Button.FontProperty.PropertyName)
				UpdateFont();
			else if (e.PropertyName == Button.BorderWidthProperty.PropertyName || e.PropertyName == Button.BorderRadiusProperty.PropertyName || e.PropertyName == Button.BorderColorProperty.PropertyName)
				UpdateBorder();
			else if (e.PropertyName == VisualElement.BackgroundColorProperty.PropertyName)
				UpdateBackgroundVisibility();
			else if (e.PropertyName == Button.ImageProperty.PropertyName)
				UpdateImage();
		}

		void OnButtonTouchUpInside(object sender, EventArgs eventArgs)
		{
			if (Element != null)
				((IButtonController)Element).SendClicked();
		}

		void UpdateBackgroundVisibility()
		{
			if (Forms.IsiOS7OrNewer)
				return;

			var model = Element;
			var shouldDrawImage = model.BackgroundColor == Color.Default;

			foreach (var control in Control.Subviews.Where(sv => !(sv is UILabel)))
				control.Alpha = shouldDrawImage ? 1.0f : 0.0f;
		}

		void UpdateBorder()
		{
			var uiButton = Control;
			var button = Element;

			if (button.BorderColor != Color.Default)
				uiButton.Layer.BorderColor = button.BorderColor.ToCGColor();

			uiButton.Layer.BorderWidth = (float)button.BorderWidth;
			uiButton.Layer.CornerRadius = button.BorderRadius;

			UpdateBackgroundVisibility();
		}

		void UpdateFont()
		{
			Control.TitleLabel.Font = Element.ToUIFont();
		}

		async void UpdateImage()
		{
			IImageSourceHandler handler;
			var source = Element.Image;
			if (source != null && (handler = Registrar.Registered.GetHandler<IImageSourceHandler>(source.GetType())) != null)
			{
				UIImage uiimage;
				try
				{
					uiimage = await handler.LoadImageAsync(source, scale: (float)UIScreen.MainScreen.Scale);
				}
				catch (OperationCanceledException)
				{
					uiimage = null;
				}
				var button = Control;
				if (button != null && uiimage != null)
				{
					if (Forms.IsiOS7OrNewer)
						button.SetImage(uiimage.ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal), UIControlState.Normal);
					else
						button.SetImage(uiimage, UIControlState.Normal);
					button.ImageView.ContentMode = UIViewContentMode.ScaleAspectFit;

					Control.ImageEdgeInsets = new UIEdgeInsets(0, 0, 0, 10);
					Control.TitleEdgeInsets = new UIEdgeInsets(0, 10, 0, 0);
				}
			}
			else
			{
				Control.SetImage(null, UIControlState.Normal);
				Control.ImageEdgeInsets = new UIEdgeInsets(0, 0, 0, 0);
				Control.TitleEdgeInsets = new UIEdgeInsets(0, 0, 0, 0);
			}
			((IVisualElementController)Element).NativeSizeChanged();
		}

		void UpdateText()
		{
			Control.SetTitle(Element.Text, UIControlState.Normal);
		}

		void UpdateTextColor()
		{
			if (Element.TextColor == Color.Default)
			{
				Control.SetTitleColor(_buttonTextColorDefaultNormal, UIControlState.Normal);
				Control.SetTitleColor(_buttonTextColorDefaultHighlighted, UIControlState.Highlighted);
				Control.SetTitleColor(_buttonTextColorDefaultDisabled, UIControlState.Disabled);
			}
			else
			{
				Control.SetTitleColor(Element.TextColor.ToUIColor(), UIControlState.Normal);
				Control.SetTitleColor(Element.TextColor.ToUIColor(), UIControlState.Highlighted);
				Control.SetTitleColor(_buttonTextColorDefaultDisabled, UIControlState.Disabled);

				if (Forms.IsiOS7OrNewer)
					Control.TintColor = Element.TextColor.ToUIColor();
			}
		}
	}
}