[Mono-osx] NSAttributedString patch (appkit additions)

Jesse Jones jesse9jones at gmail.com
Sun May 29 10:21:26 EDT 2011


As far as I can tell none of the AppKit additions for NSAttributedString have bindings and NSString only has two, with those two being declared within NSImage. I needed some of the drawing methods for NSAttributedString and I was not drawing into an NSImage so I added some hand-written extension methods for them.

------------------------------------------------------------------
diff --git a/src/AppKit/NSAttributedString.cs b/src/AppKit/NSAttributedString.cs
new file mode 100644
index 0000000..ae35ef3
--- /dev/null
+++ b/src/AppKit/NSAttributedString.cs
@@ -0,0 +1,86 @@
+//
+// Copyright 2011, Jesse Jones.
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+using System;
+using System.Drawing;
+using System.Reflection;
+using System.Runtime.InteropServices;
+
+using MonoMac;
+using MonoMac.Foundation;
+using MonoMac.ObjCRuntime;
+
+// http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSAttributedString_AppKitAdditions/Reference/Reference.html
+namespace MonoMac.AppKit {
+	// TODO: Missing a fair number of methods.
+	public static class NSAttributedStringAdditions {
+		public static RectangleF BoundingRect (this NSAttributedString str, NSStringDrawingOptions options)
+		{
+			if (str == null)
+				throw new ArgumentNullException ("str");
+				
+			RectangleF result;
+			MonoMac.ObjCRuntime.Messaging.RectangleF_objc_msgSend_stret_int (out result, str.Handle, selBoundingRectWithSizeOptions, (int)options);
+			return result;
+		}
+		
+		public static void DrawAtPoint (this NSAttributedString str, PointF point)
+		{
+			if (str == null)
+				throw new ArgumentNullException ("str");
+			MonoMac.ObjCRuntime.Messaging.void_objc_msgSend_PointF (str.Handle, selDrawAtPoint, point);
+		}
+		
+		public static void DrawInRect (this NSAttributedString str, RectangleF rect, NSDictionary attributes)
+		{
+			if (str == null)
+				throw new ArgumentNullException ("str");
+			if (attributes == null)
+				throw new ArgumentNullException ("attributes");
+			MonoMac.ObjCRuntime.Messaging.void_objc_msgSend_RectangleF_IntPtr (str.Handle, selDrawInRectWithAttributes, rect, attributes.Handle);
+		}
+		
+		public static void DrawWithRect (this NSAttributedString str, RectangleF rect, NSStringDrawingOptions options)
+		{
+			if (str == null)
+				throw new ArgumentNullException ("str");
+			MonoMac.ObjCRuntime.Messaging.void_objc_msgSend_RectangleF_int (str.Handle, selDrawWithRectOptions, rect, (int)options);
+		}
+		
+		public static SizeF StringSize (this NSAttributedString str)
+		{
+			if (str == null)
+				throw new ArgumentNullException ("str");
+			SizeF ret;
+			ret = MonoMac.ObjCRuntime.Messaging.SizeF_objc_msgSend (str.Handle, selSize);
+			return ret;
+		}
+		
+		#region Selectors
+		static IntPtr selBoundingRectWithSizeOptions = Selector.GetHandle ("boundingRectWithSize:options:");
+		static IntPtr selDrawAtPoint = Selector.GetHandle ("drawAtPoint:");
+		static IntPtr selDrawInRectWithAttributes = Selector.GetHandle ("drawInRect:withAttributes:");
+		static IntPtr selDrawWithRectOptions = Selector.GetHandle ("drawWithRect:options:");
+		static IntPtr selSize = Selector.GetHandle ("size");
+		#endregion
+	}
+}
diff --git a/src/Makefile b/src/Makefile
index 776b03d..4342699 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -46,6 +46,7 @@ MONOMAC_SOURCES = \
 	./AppKit/NSActionCell.cs			\
 	./AppKit/NSApplication.cs			\
 	./AppKit/NSApplicationDelegate.cs		\
+	./AppKit/NSAttributedString.cs			\
 	./AppKit/NSBitmapImageRep.cs			\
 	./AppKit/NSButton.cs				\
 	./AppKit/NSBezierPath.cs			\
diff --git a/src/appkit.cs b/src/appkit.cs
index 4ba4657..37d92b3 100644
--- a/src/appkit.cs
+++ b/src/appkit.cs
@@ -6013,6 +6013,7 @@ namespace MonoMac.AppKit {
 		[Bind ("sizeWithAttributes:")]
 		SizeF StringSize ([Target] string str, NSDictionary attributes);
 
+		// TODO: May want to replace this with methods in NSStringAdditions.
 		[Bind ("drawInRect:withAttributes:")]
 		void DrawInRect ([Target] string str, RectangleF rect, NSDictionary attributes);
 	}



More information about the Mono-osx mailing list