StringFormat Class
The StringFormat class defines how text is formatted when it is drawn on a screen or on a printer. It provides a comprehensive set of properties that control text alignment, wrapping, clipping, hot tracking, and measurement.
Overview
When you draw text using the GDI+ Graphics object, you can optionally provide a StringFormat object to customize the appearance and layout of the text. This allows for precise control over how text is rendered, including:
- Alignment: How text is aligned horizontally and vertically within its bounding box.
- Tab Stops: Defining custom tab stop positions.
- Word/Character Wraps: Specifying how text should break across lines.
- Trimming: How to shorten text that exceeds the available space.
- Digit Substitution: Mapping digits from one language to another.
- Reading Order: Controlling the direction of text (e.g., left-to-right, right-to-left).
Syntax
public sealed class StringFormat : ICloneable
Constructors
StringFormat(StringFormatFlags formatFlags, int languageId): Initializes a new instance of theStringFormatclass with specified format flags and language.StringFormat(StringFormatFlags formatFlags): Initializes a new instance of theStringFormatclass with specified format flags.StringFormat(): Initializes a new instance of theStringFormatclass with default settings.
Properties
Alignment: Gets or sets the horizontal and vertical alignment of the text.DigitLanguage: Gets or sets the language to use for digit substitution.DigitReplacements: Gets or sets the digit substitution method.FormatFlags: Gets or sets a set of flags that control various text formatting options.HotTrackText: Gets or sets a value that indicates whether hot-track text formatting is enabled.LineAlignment: Gets or sets the vertical alignment of text within lines.NoWrap: Gets or sets a value that indicates whether word wrapping is disabled.Trimming: Gets or sets the text trimming behavior.
Methods
Clone(): Creates an exact copy of thisStringFormatobject.Dispose(): Releases all resources used by theStringFormatobject.GetAlignment(): Gets the horizontal and vertical alignment.GetDigitLanguage(): Gets the language used for digit substitution.GetDigitReplacements(): Gets the digit substitution method.GetFormatFlags(): Gets the format flags.GetHotTrackText(): Gets a value indicating whether hot-track text is enabled.GetLineAlignment(): Gets the vertical alignment of lines.GetTabStops(out float[] tabStops): Gets the tab stop positions.GetTrimming(): Gets the text trimming behavior.SetAlignment(StringAlignment align): Sets the horizontal and vertical alignment.SetDigitLanguage(int languageId): Sets the language for digit substitution.SetDigitReplacements(DigitLanguage language, DigitMode mode): Sets the digit substitution method.SetFormatFlags(StringFormatFlags flags): Sets the format flags.SetHotTrackText(bool enable): Enables or disables hot-track text formatting.SetLineAlignment(StringAlignment align): Sets the vertical alignment of lines.SetTabStops(float firstTab, float[] tabStops): Sets the tab stop positions.SetTrimming(StringTrimming trimming): Sets the text trimming behavior.
Enumerations
The StringFormat class uses several enumerations to define its behavior:
StringAlignment: Specifies horizontal and vertical alignment options (Near,Center,Far).StringFormatFlags: A combination of flags that control text layout (e.g.,DirectionRightToLeft,NoClip,HotTrackPrefix).StringTrimming: Specifies how to trim text that overflows its layout rectangle (e.g.,Character,Word,EllipsisCharacter).DigitLanguage: Specifies the language for digit substitution (e.g.,Neutral,ArabicIndic,HindiIndic).DigitMode: Specifies the mode for digit substitution (e.g.,None,Digits,All).
Example Usage
The following example demonstrates how to create a StringFormat object and use it to draw text with specific alignment and trimming.
using System.Drawing;
// Create a Graphics object (assume it's already created and configured)
Graphics graphics = /* ... get your Graphics object ... */;
// Create a StringFormat object for right alignment and word trimming
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Far;
sf.Trimming = StringTrimming.Word;
sf.FormatFlags = StringFormatFlags.DirectionRightToLeft; // Example of another flag
// Define the text and the rectangle to draw it in
string textToDraw = "This is a long piece of text that might need trimming and special alignment.";
Rectangle layoutRect = new Rectangle(50, 50, 200, 100);
// Define a Font and a Brush
Font font = new Font("Arial", 12);
Brush brush = new SolidBrush(Color.Black);
// Draw the string using the StringFormat object
graphics.DrawString(textToDraw, font, brush, layoutRect, sf);
// Dispose of the objects when done
sf.Dispose();
font.Dispose();
brush.Dispose();
Important Note:
Always remember to dispose of StringFormat objects when you are finished with them, especially in performance-critical applications, to release system resources.