简体   繁体   中英

line spacing in label (autosize = false) C#

I know its possible to do line spacing in Label. Can someone help me do this pls? I need a label with double spacing or 1.5 spacing.

I found this code but its not working:

    private void label1_Paint(object sender, PaintEventArgs e)
    {
        string text = "Sri Lanka";
        Graphics g = e.Graphics;
        Font font = new Font("Arial", 10);
        Brush brush = new SolidBrush(Color.Black);
        float lineSpacing = 0.5f;

        SizeF size = g.MeasureString("A", font);

        float pos = 0.0f;
        for (int i = 0; i < text.Length; ++i)
        {
            string charToDraw = new string(text, 1);
            g.DrawString(charToDraw, font, brush, pos, 0.0f);
            SizeF sizeChar = g.MeasureString(charToDraw, font);
            pos += sizeChar.Width + size.Width * lineSpacing;
        }
    } 

It gives me the error: the best overloaded match for 'string.string(char, int)' has some invalid arguments on this code " string charToDraw = new string(text, 1); ".

That's because you are passing a string as the first parameter and there is no overloaded string constructor that takes a string as the first parameter.

If you are trying to access the string character by character, try:

for (int i = 0; i < text.Length; ++i)
{
    char charToDraw = text[i];

But if you are trying to pass it character by character to DrawString() you will need a string version of the single char, so it would be easier to do this:

for (int i = 0; i < text.Length; ++i)
{
    string charToDraw = text.SubString(i, 1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM