Answered by:
Text Alignment

Question
-
Hi,
How to align string to Left, Right, Center and Middle? I have done other alignment like middle center,middle left,middle right, top left etc using StringAligment.
Help me to alignment string in Left, Right, Center and Middle.
StringFormat objStringFormat = new StringFormat(); objStringFormat.Alignment = StringAlignment.Center; objStringFormat.LineAlignment = StringAlignment.Center;
Thank You- Edited by Benita Blas Friday, June 11, 2010 6:49 AM
Thursday, December 3, 2009 1:26 PM
Answers
-
Hi,
Please look at the link below,
http://www.kodakgallery.com/gallery/creativeapps/slideShow/Main.jsp?token=994539887113%3A1776755655&cm_mmc=site_email-_-new_site_share-_-core-_-View_photos_button
In the figure,
I have drawn text content as Alignment name. filled circle in Cyan color represent my insertion point specified for drawing string
BottomLeft Alignment is different from Left,
BottomRight Alignment is different from Right ,
BottomCenter Alignment is different from Center
MiddleCenter Alignment is different from middle.
You can watch the different when observed closely. In Left,Right,Center and Middle Alignment Insertion point will touch the text,but in BottomLeft,BottomRight,BottomCenter and MiddleCenter,Inssertion point will not touch the text.
I have attached code for refer:StringFormat objStringFormat = new StringFormat();
if (Justification == "Top Left")
{
objStringFormat.Alignment = StringAlignment.Near;
objStringFormat.LineAlignment = StringAlignment.Near;
}
else if (Justification == "Top Center")
{
objStringFormat.Alignment = StringAlignment.Center;
objStringFormat.LineAlignment = StringAlignment.Near;
}
else if (Justification == "Top Right")
{
objStringFormat.Alignment = StringAlignment.Far;
objStringFormat.LineAlignment = StringAlignment.Near;
}
else if (Justification == "Middle Left")
{
objStringFormat.Alignment = StringAlignment.Near;
objStringFormat.LineAlignment = StringAlignment.Center;
}
else if (Justification == "Middle Center")
{
objStringFormat.Alignment = StringAlignment.Center;
objStringFormat.LineAlignment = StringAlignment.Center;
}
else if (Justification == "Middle Right")
{
objStringFormat.Alignment = StringAlignment.Far;
objStringFormat.LineAlignment = StringAlignment.Center;
}
else if (Justification == "Bottom Left")
{
objStringFormat.Alignment = StringAlignment.Near;
objStringFormat.LineAlignment = StringAlignment.Far;
}
else if (Justification == "Bottom Center")
{
objStringFormat.Alignment = StringAlignment.Center;
objStringFormat.LineAlignment = StringAlignment.Far;
}
else if (Justification == "Bottom Right")
{
objStringFormat.Alignment = StringAlignment.Far;
objStringFormat.LineAlignment = StringAlignment.Far;
}
Graphics g=this.CreateGraphics();
GraphicsPath m_Path=new GraphicsPath();
m_Path.AddString(m_Content, m_FontFamily, m_Font.Style, m_Font.Size, m_InsertionPointF,objStringFormat);
g.DrawPath(m_Pen, m_Path);
In the link below given 18 different Text Alignment possible.And we can align to a point, or to a bounding rect
http://srtsolutions.com/blogs/billwagner/archive/2005/04/13/aligning-text-in-multiple-ways.aspx
Thank You- Marked as answer by Harry Zhu Monday, December 7, 2009 7:52 AM
- Edited by Benita Blas Friday, June 11, 2010 6:51 AM
Friday, December 4, 2009 1:27 PM -
Hello,
You have to set two things,
1) StringAlignment (Specifies the alignment of a text string relative to its layout rectangle. It can be any from these three Near,Far and Center)
2) LineAlignment (Gets or sets the line alignment on the horizontal plane.)
See the bellow example,
NOTE :
When used with the LineAlignment property, this enumeration sets the vertical alignment for a drawn string. When used with the Alignment property, this enumeration sets the horizontal alignment.
private void ShowLineAndAlignment(PaintEventArgs e) { // Construct a new Rectangle . Rectangle displayRectangle = new Rectangle (new Point(40, 40), new Size (80, 80)); // Construct 2 new StringFormat objects StringFormat format1 = new StringFormat(StringFormatFlags.NoClip); StringFormat format2 = new StringFormat(format1); // Set the LineAlignment and Alignment properties for // both StringFormat objects to different values. format1.LineAlignment = StringAlignment.Near; format1.Alignment = StringAlignment.Center; format2.LineAlignment = StringAlignment.Center; format2.Alignment = StringAlignment.Far; // Draw the bounding rectangle and a string for each // StringFormat object. e.Graphics.DrawRectangle(Pens.Black, displayRectangle); e.Graphics.DrawString("Showing Format1", this.Font, Brushes.Red, (RectangleF)displayRectangle, format1); e.Graphics.DrawString("Showing Format2", this.Font, Brushes.Red, (RectangleF)displayRectangle, format2); }
Tejas MerMonday, December 7, 2009 7:49 AM
All replies
-
Hi,
I'm not sure what do you mean .
Could you please be more clear?
what's the difference between
Left, Right, Center and Middle
and middle center,middle left,middle right, top left?
Harry
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.Friday, December 4, 2009 7:09 AM -
Hi,
Please look at the link below,
http://www.kodakgallery.com/gallery/creativeapps/slideShow/Main.jsp?token=994539887113%3A1776755655&cm_mmc=site_email-_-new_site_share-_-core-_-View_photos_button
In the figure,
I have drawn text content as Alignment name. filled circle in Cyan color represent my insertion point specified for drawing string
BottomLeft Alignment is different from Left,
BottomRight Alignment is different from Right ,
BottomCenter Alignment is different from Center
MiddleCenter Alignment is different from middle.
You can watch the different when observed closely. In Left,Right,Center and Middle Alignment Insertion point will touch the text,but in BottomLeft,BottomRight,BottomCenter and MiddleCenter,Inssertion point will not touch the text.
I have attached code for refer:StringFormat objStringFormat = new StringFormat();
if (Justification == "Top Left")
{
objStringFormat.Alignment = StringAlignment.Near;
objStringFormat.LineAlignment = StringAlignment.Near;
}
else if (Justification == "Top Center")
{
objStringFormat.Alignment = StringAlignment.Center;
objStringFormat.LineAlignment = StringAlignment.Near;
}
else if (Justification == "Top Right")
{
objStringFormat.Alignment = StringAlignment.Far;
objStringFormat.LineAlignment = StringAlignment.Near;
}
else if (Justification == "Middle Left")
{
objStringFormat.Alignment = StringAlignment.Near;
objStringFormat.LineAlignment = StringAlignment.Center;
}
else if (Justification == "Middle Center")
{
objStringFormat.Alignment = StringAlignment.Center;
objStringFormat.LineAlignment = StringAlignment.Center;
}
else if (Justification == "Middle Right")
{
objStringFormat.Alignment = StringAlignment.Far;
objStringFormat.LineAlignment = StringAlignment.Center;
}
else if (Justification == "Bottom Left")
{
objStringFormat.Alignment = StringAlignment.Near;
objStringFormat.LineAlignment = StringAlignment.Far;
}
else if (Justification == "Bottom Center")
{
objStringFormat.Alignment = StringAlignment.Center;
objStringFormat.LineAlignment = StringAlignment.Far;
}
else if (Justification == "Bottom Right")
{
objStringFormat.Alignment = StringAlignment.Far;
objStringFormat.LineAlignment = StringAlignment.Far;
}
Graphics g=this.CreateGraphics();
GraphicsPath m_Path=new GraphicsPath();
m_Path.AddString(m_Content, m_FontFamily, m_Font.Style, m_Font.Size, m_InsertionPointF,objStringFormat);
g.DrawPath(m_Pen, m_Path);
In the link below given 18 different Text Alignment possible.And we can align to a point, or to a bounding rect
http://srtsolutions.com/blogs/billwagner/archive/2005/04/13/aligning-text-in-multiple-ways.aspx
Thank You- Marked as answer by Harry Zhu Monday, December 7, 2009 7:52 AM
- Edited by Benita Blas Friday, June 11, 2010 6:51 AM
Friday, December 4, 2009 1:27 PM -
Glad to know you have solve the problem.
Harry
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.Monday, December 7, 2009 6:18 AM -
Hello,
You have to set two things,
1) StringAlignment (Specifies the alignment of a text string relative to its layout rectangle. It can be any from these three Near,Far and Center)
2) LineAlignment (Gets or sets the line alignment on the horizontal plane.)
See the bellow example,
NOTE :
When used with the LineAlignment property, this enumeration sets the vertical alignment for a drawn string. When used with the Alignment property, this enumeration sets the horizontal alignment.
private void ShowLineAndAlignment(PaintEventArgs e) { // Construct a new Rectangle . Rectangle displayRectangle = new Rectangle (new Point(40, 40), new Size (80, 80)); // Construct 2 new StringFormat objects StringFormat format1 = new StringFormat(StringFormatFlags.NoClip); StringFormat format2 = new StringFormat(format1); // Set the LineAlignment and Alignment properties for // both StringFormat objects to different values. format1.LineAlignment = StringAlignment.Near; format1.Alignment = StringAlignment.Center; format2.LineAlignment = StringAlignment.Center; format2.Alignment = StringAlignment.Far; // Draw the bounding rectangle and a string for each // StringFormat object. e.Graphics.DrawRectangle(Pens.Black, displayRectangle); e.Graphics.DrawString("Showing Format1", this.Font, Brushes.Red, (RectangleF)displayRectangle, format1); e.Graphics.DrawString("Showing Format2", this.Font, Brushes.Red, (RectangleF)displayRectangle, format2); }
Tejas MerMonday, December 7, 2009 7:49 AM -
What Benita Blas is suggesting will work, but is a lot of code to achieve something so simple. An alternative to this would be to do something like this.
Create an enumeration of all the possible text alignments. the enumeration makes it much easier to specify how you want the text positioned, with names that have a clean and understandable meaning.
public enum TextAlignment { TopLeft = System.Drawing.StringAlignment.Near << 2 | System.Drawing.StringAlignment.Near, TopCenter = System.Drawing.StringAlignment.Near << 2 | System.Drawing.StringAlignment.Center, TopRight = System.Drawing.StringAlignment.Near << 2 | System.Drawing.StringAlignment.Far, MiddleLeft = System.Drawing.StringAlignment.Center << 2 | System.Drawing.StringAlignment.Near, MiddleCenter = System.Drawing.StringAlignment.Center << 2 | System.Drawing.StringAlignment.Center, MiddleRight = System.Drawing.StringAlignment.Center << 2 | System.Drawing.StringAlignment.Far, BottomLeft = System.Drawing.StringAlignment.Far << 2 | System.Drawing.StringAlignment.Near, BottomCenter = System.Drawing.StringAlignment.Far << 2 | System.Drawing.StringAlignment.Center, BottomRight = System.Drawing.StringAlignment.Far << 2 | System.Drawing.StringAlignment.Far }
next create a helper method that will take a StringFormat object and the desired text alignment. (this is not a requirement but will help make the code more re-useable)
public static void SetTextAlignment(System.Drawing.StringFormat format, TextAlignment alignment) { var shift = ((int)alignment) >> 2; var mod = ((int)alignment) % 4; format.Alignment = (System.Drawing.StringAlignment)mod; format.LineAlignment = (System.Drawing.StringAlignment)shift; }
With these two things you can now modify Benita Blas code to look something like this.
System.Drawing.StringFormat objStringFormat = new System.Drawing.StringFormat(); SetTextAlignment(objStringFormat, TextAlignment.TopLeft); System.Drawing.Graphics g = this.CreateGraphics(); System.Drawing.Drawing2D.GraphicsPath m_Path = new System.Drawing.Drawing2D.GraphicsPath(); m_Path.AddString(m_Content, m_FontFamily, m_Font.Style, m_Font.Size, m_InsertionPointF, objStringFormat); g.DrawPath(m_Pen, m_Path);
In addition to reducing the amount of code that is needed, this should also help make the code more readable and maintainable.
- Proposed as answer by .netrip Thursday, February 16, 2012 11:45 PM
Thursday, February 16, 2012 11:43 PM -
Nice though this is I suspect that two years after the question it's kind of irrelevant to the OP. :)
Regards David R
---------------------------------------------------------------
Object-oriented programming offers a sustainable way to write spaghetti code. - Paul Graham.
Every program eventually becomes rococo, and then rubble. - Alan Perlis
The only valid measurement of code quality: WTFs/minute.Friday, February 17, 2012 10:50 AM