Wednesday, January 04, 2006

UITypeEditor for Font Name properties

We’re making big changes and enhancements (I guess) in our current development of the next release of our ImageDraw control for ASP.NET
One of the big changes we made is that now, ImageDraw will support layers and a lot of imaging actions out of the box. We’ve included an action that will allow you to create a Polaroid version of a specified image. Polaroid will support text that you can include onto it a.k.a. captions.
So, we designed a Polaroid class that has a Font property in order you can specified the font style you want to use for Polaroid’s captions.

In a first place, we though in using the FontInfo class of ASP.NET, the same used by WebControl class for its Font property. But we discard it when we did realize that it has not an appropriated TypeConverter needed for State Management and because it is a Sealed Class i.e. we can’t inherit from it and provide a custom TypeConverter.

In second place, we went for System.Drawing.Font class. It seemed to be the correct class because it has all properties we need to draw text using GDI+ and it has a built-in TypeConverter! Wrong choice I’d say. Its TypeConverter seems not to be prepared to ASP.NET serialization, I mean, when a Font object is serialized on a WebForm, some read-only properties of Font class, such as Bold, are serialized on the WebForm making the Designer begins throwing exception about you can’t set read-only properties.

Without success on built-in Font classes, we decided to design one and we called it Font, of course. We made it very simple with properties like Name, Size, Bold, Italic, Underline, and Strikeout.
If you try our products, you’ll see that we always make great efforts on design-time experience. When we can imitate a design-time feature of built-in ASP.NET controls, we’ll do it. And Font Name property is one of those cases.

When you drag & drop an ASP.NET control like Label, you’ll find that it has an “expandable” Font property. This Font property allows you to set a Font Name by selecting one of its installed Fonts list.
As I stated before, we went for that beautiful Font List to include it into our Font Name property. You can easily found it using tools like Reflector going to System.Web.UI.WebControls.FontInfo class and selecting Name property. You’ll realize that the font names list is UITypeEditor class in the System.Drawing.Design.dll assembly called System.Drawing.Design.FontNameEditor.
So, add the following attribute to our Name property and... that’s it?

[
Editor(typeof(FontNameEditor), typeof(UITypeEditor))
]

public string Name
{
get{}
set{}
}


Well, that’s not it! Compile your project and you’ll get the following i.e. a Name property with no button picker to drop down the font list.

To solve the issue, we must add another attribute that is involved with Font list editor. It’s a built-in TypeConverter for Font Names called System.Drawing.FontConverter.FontNameConverter. Basically, this class gets all installed Fonts and together the FontNameEditor class, both make the trick.
Change your Name property to look like the following and you’ll get your Font Names list finally.

[
TypeConverter(typeof(System.Drawing.FontConverter.FontNameConverter)),
Editor(typeof(FontNameEditor), typeof(UITypeEditor))
]

public string Name
{
get{}
set{}
}

0 Comments:

Post a Comment

<< Home