How to check if the line was broken?

Asked

Viewed 82 times

0

I use File.ReadAllText to read the text inside a file .txt.

private void label9_TextChanged(object sender, EventArgs e)
{
    string text = File.ReadAllText($@"{pathname}", Encoding.UTF8);

    if (text.Length) // Como fazer aqui ?
    {

    }    
}

Inside of my file .txt has:

Pois diga que irá 
Irá já, Irajá
Pra onde eu só veja você,
Você veja-me só
Marajó, Marajó
Qualquer outro lugar comum, outro lugar qualquer
Guaporé, Guaporé.
Qualquer outro lugar ao sol, outro lugar ao sul.
Céu azul, céu azul.
Onde haja só meu corpo nu
Junto ao seu corpo nu

On the form I have label1 and panel1. The result of the label:

Pois diga que irá 
Irá já, Irajá
Pra onde eu só veja você,
Você veja-me só
Marajó, Marajó
Qualquer outro lugar comum, outro 
lugar qualquer
Guaporé, Guaporé.
Qualquer outro lugar ao sol, outro 
lugar ao sul.
Céu azul, céu azul.
Onde haja só meu corpo nu
Junto ao seu corpo nu

You can see that in the sixth and ninth line was broken automatically, how can I know if some of these lines were broken ? If yes, decrease the font.

On the sixth line she should stay that way:

Qualquer outro lugar comum, outro lugar qualquer

In the ninth line it should stay that way:

Qualquer outro lugar ao sol, outro lugar ao sul

Must respect according to file .txt.

  • puts your code

  • had made a similar code... I went to see, and it was for you also kkkk I think can meet you also in this issue: https://answall.com/a/254130/69359

  • Similar, but this must respect according to .txt :)

1 answer

1


Basically you just check if the text space will be larger than the label width, and while it is, decreases the font.

For this, you use the MeasureString class Graphics and a while, that decreases the source while the Width of the text is greater than label.

Sample code:

private void label1_TextChanged(object sender, EventArgs e)
{
    Graphics g = this.CreateGraphics();
    float p = 14;
    Font f = new Font(((Label)sender).Font.Name, p);
    SizeF s = g.MeasureString(((Label)sender).Text, f);

    while (s.Width >= ((Label)sender).Width - 20)
    {
       p = p - 0.1f;
       f = new Font(((Label)sender).Font.Name, p);
       s = g.MeasureString(((Label)sender).Text, f);
    }

    ((Label)sender).Font = f;
}

Upshot: Resultado:

  • What is float p = 14; ?

  • 1

    is the size of the initial font, would be the largest possible font if it fit on the label

  • Rovann you know how to do this in WPF ?

  • 1

    @Matheusmiranda never did no, I have no practice with WPF =/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.