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