2

I can't understand how to make label with fixed size in Fyne. I can't find any method for it

func main() {
    a := app.New()

    w := a.NewWindow("Fyne Demo")
    label1 := widget.NewLabel("Hello Fyne\n\nline\n\nline\n\nline\n\n\n\nline")
    label2 := widget.NewLabel("Text")
    edit1 := widget.NewEntry()
    btn1 := widget.NewButton("Run", func() {
      label1.SetText("Hello World")
    })
    container := container.NewVBox(label1, label2, edit1, btn1)

    w.SetContent(container)
    w.Resize(fyne.NewSize(300,400))
    w.ShowAndRun()
}

After click on btn1, with changing text it automatically resizing. How to prevent this?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Hotery
  • 21
  • 2
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – AthulMuralidhar Jun 21 '23 at 08:48

2 Answers2

0

if you want to make a Label with a fixed size that does not automatically resize when the text changes, you can follow these steps:

  1. Wrap the Label inside a container: This means putting the Label widget inside another container widget. In this case, you can use the container.NewMax() function to create the container.

  2. Set the desired fixed size: After creating the container, you can use the Resize() method to specify the size you want for the container. This size will determine the fixed dimensions of the Label.

    func main() {
       a := app.New()
       w := a.NewWindow("Fyne Demo")
    
       label1 := widget.NewLabel("Hello Fyne\n\nline\n\nline\n\nline\n\n\n\nline")
       label2 := widget.NewLabel("Text")
       edit1 := widget.NewEntry()
       btn1 := widget.NewButton("Run", func() {
           label1.SetText("Hello World")
       })
    
       label1Container := container.NewFixed(label1)
       label1Container.Resize(fyne.NewSize(200, 100)) 
    
       content := container.NewVBox(label1Container, label2, edit1, btn1)
    
       w.SetContent(content)
       w.Resize(fyne.NewSize(300, 400))
       w.ShowAndRun()}
    

By wrapping the Label in a container and setting the fixed size for the container, you can achieve the effect of a Label with a fixed size in Fyne.

  • go run says "undefined: container.NewFixed", also cant find this method in https://developer.fyne.io/api/v2.0/container/ also tried to use container.NewMax, still nothing, label1 height are shrinks on SetText... – Hotery Jun 21 '23 at 11:29
0

Fixed size widgets are a dangerous concept in a platform-agnostic toolkit, so we make them all minimum size by default. That way it will fit on the smallest of screens.

The window is resizing because your content becomes too large for the window. To stop this from happening the better way is to give the widget more space so it does not have to force the window to be larger. Something like Window.Resize will ask for more room from the OS.

andy.xyz
  • 2,567
  • 1
  • 13
  • 18