C# 在中心绘制文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/970453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Draw text at center
提问by Prithis
Which is the best way to drawString at the center of a rectangleF? Text font size can be reduced to fit it. In most case the Text is too big to fit with a given font so have to reduce the font.
在矩形F的中心绘制String的最佳方法是什么?可以减小文本字体大小以适应它。在大多数情况下,文本太大而无法容纳给定的字体,因此必须减少字体。
采纳答案by Prithis
It is working for me know. This is what I did
它对我有用,我知道。这就是我所做的
Size textSize = TextRenderer.MeasureText(Text, Font);
float presentFontSize = Font.Size;
Font newFont = new Font(Font.FontFamily, presentFontSize, Font.Style);
while ((textSize.Width>textBoundary.Width || textSize.Height > textBoundary.Height) && presentFontSize-0.2F>0)
{
presentFontSize -= 0.2F;
newFont = new Font(Font.FontFamily,presentFontSize,Font.Style);
textSize = TextRenderer.MeasureText(ButtonText, newFont);
}
stringFormat sf;
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
e.Graphics.DrawString(Text,newFont,Brushes.Black,textBoundary, sf);
回答by Lazarus
Determine the size of the text to be drawn and then determine the offset for the start of the string from the centre of rectangleF and draw it.
确定要绘制的文本的大小,然后确定字符串开头与矩形 F 中心的偏移量并绘制它。
回答by Serapth
Get width/2 and height/2 of the rectangle, then using System.Graphics.MeasureString to get the dimensions of your string, again half them and subtract from your earlier width/height values and you end up with the X,Y coordinate to draw your string at in order for it to be centered.
获取矩形的 width/2 和 height/2,然后使用 System.Graphics.MeasureString 获取字符串的尺寸,再次将它们减半并从之前的宽度/高度值中减去,最终得到 X、Y 坐标绘制你的字符串,以便它居中。
回答by tekBlues
This code centers the text horizontally and vertically:
此代码水平和垂直居中文本:
stringFormat sf;
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
grp.DrawString(text, font, Brushes.Black, rectf, sf);
回答by Fredrik M?rk
I played around with it a bit and found this solution (assuming that the RectangleF rect
and string text
are already defined):
我玩了一下,找到了这个解决方案(假设RectangleF rect
和string text
已经定义):
StringFormat stringFormat = new StringFormat()
{
Alignment = StringAlignment.Center,
LineAlignment = StringAlignment.Center
};
using (Graphics g = this.CreateGraphics())
{
SizeF s = g.MeasureString(text, this.Font);
float fontScale = Math.Max(s.Width / rect.Width, s.Height / rect.Height);
using (Font font = new Font(this.Font.FontFamily, this.Font.SizeInPoints / fontScale, GraphicsUnit.Point))
{
g.DrawString(text, font, Brushes.Black, rect, stringFormat);
}
}
回答by Yas
Easy to use :)
便于使用 :)
public static void DrawStringCenter(Image image, string s, Font font, Color color, RectangleF layoutRectangle)
{
var graphics = Graphics.FromImage(image);
var brush = new SolidBrush(color);
var format = new StringFormat
{
Alignment = StringAlignment.Center,
LineAlignment = StringAlignment.Center
};
graphics.DrawString(s, font, brush, layoutRectangle, format);
}