summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.MacOS/Extensions/NSTextFieldExtensions.cs
blob: 9905fcd51c6d8968f7e1204d5adc5fcde594af0f (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
´╗┐using AppKit;
using CoreGraphics;

namespace Xamarin.Forms.Platform.MacOS
{
	internal static class NSTextFieldExtensions
	{
		public static NSTextField CreateLabel(string text)
		{
			var textField = new NSTextField();
			textField.StringValue = text;
			textField.DrawsBackground = false;
			textField.Editable = false;
			textField.Bezeled = false;
			textField.Selectable = false;
			textField.SizeToFit();
			textField.CenterTextVertically();
			return textField;
		}

		public static NSTextFieldCell CreateLabelCentered(string text)
		{
			var textField = new VerticallyCenteredTextFieldCell(0);
			textField.StringValue = text;
			textField.DrawsBackground = false;
			textField.Editable = false;
			textField.Bezeled = false;
			textField.Selectable = false;
			return textField;
		}

		public static void CenterTextVertically(this NSTextField self)
		{
			self.CenterTextVertically(self.Frame);
		}

		public static void CenterTextVertically(this NSTextField self, CGRect frame)
		{
			var stringHeight = self.Cell.AttributedStringValue.Size.Height;
			var titleRect = self.Cell.TitleRectForBounds(frame);
			var newTitleRect = new CGRect(titleRect.X, frame.Y + (frame.Height - stringHeight) / 2.0, titleRect.Width,
				stringHeight);
			self.Frame = newTitleRect;
		}
	}
}