summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.MacOS/Controls/VerticallyCenteredTextFieldCell.cs
blob: 397f632ce7b26d0f626ff9ef4dbe6b584f653a86 (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
using System;
using AppKit;
using CoreGraphics;

namespace Xamarin.Forms.Platform.MacOS
{
	sealed class VerticallyCenteredTextFieldCell : NSTextFieldCell
	{
		readonly nfloat _yOffset;

		public VerticallyCenteredTextFieldCell(nfloat yOffset, NSFont font = null)
		{
			if (font != null)
				Font = font;
			_yOffset = yOffset;
		}

		public override CGRect DrawingRectForBounds(CGRect theRect)
		{
			// Get the parent's idea of where we should draw.
			CGRect newRect = base.DrawingRectForBounds(theRect);

			// Ideal size for the text.
			CGSize textSize = CellSizeForBounds(theRect);

			// Center in the rect.
			nfloat heightDelta = newRect.Size.Height - textSize.Height;
			if (heightDelta > 0)
			{
				newRect.Size = new CGSize(newRect.Width, newRect.Height - heightDelta);
				newRect.Location = new CGPoint(newRect.X, newRect.Y + heightDelta / 2 + _yOffset);
			}
			return newRect;
		}
	}
}