summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.MacOS/Controls/VerticallyCenteredTextFieldCell.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Xamarin.Forms.Platform.MacOS/Controls/VerticallyCenteredTextFieldCell.cs')
-rw-r--r--Xamarin.Forms.Platform.MacOS/Controls/VerticallyCenteredTextFieldCell.cs36
1 files changed, 36 insertions, 0 deletions
diff --git a/Xamarin.Forms.Platform.MacOS/Controls/VerticallyCenteredTextFieldCell.cs b/Xamarin.Forms.Platform.MacOS/Controls/VerticallyCenteredTextFieldCell.cs
new file mode 100644
index 00000000..397f632c
--- /dev/null
+++ b/Xamarin.Forms.Platform.MacOS/Controls/VerticallyCenteredTextFieldCell.cs
@@ -0,0 +1,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;
+ }
+ }
+} \ No newline at end of file