how to change spaces between lines in winforms label?

前端 未结 1 1028
挽巷
挽巷 2021-01-14 12:05

I have a label with unknown text. the text contains \\n. I want a bigger space between the \\n lines. How can I set it?

相关标签:
1条回答
  • 2021-01-14 12:34

    You can create custom label component and override it's measuring and painting, here you can find a snippet of a paint method which implements line spacing.

    Edit: adding a snippet here in case the link is dead:

    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(textIdea, 1);
        g.DrawString(charToDraw, font, brush, pos, 0.0f);
        SizeF sizeChar = g.MeasureString(charToDraw, font);
        pos +=  sizeChar.Width + size.Width * lineSpacing;
    }
    
    0 讨论(0)
提交回复
热议问题