使用Fyne Go的canvas.NewText()如何使文本可滚动?

huangapple go评论81阅读模式
英文:

How to make the text scrollable with canvas.NewText() with fyne Go

问题

我是你的中文翻译助手,以下是翻译好的内容:

我刚开始学习编程,我想开发一个应用程序,但我需要在我的应用程序中向用户显示非常大的文本。
我不确定widget.NewRichTextWithText()是否适用于此目的。
这段代码按照我预期的方式工作。

package main

import (
	"strings"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/app"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/widget"
)

func main() {
	a := app.New()
	
	w := a.NewWindow("Some Practice")
	
	w.Resize(fyne.NewSize(400, 400))
	
	text := widget.NewRichTextWithText((strings.Repeat("this\n", 100)))
	
	c := container.NewMax(container.NewVScroll(text))
	
	w.SetContent(c)
	
	w.ShowAndRun()
}

但是这个代码不起作用。

package main

import (
	"image/color"
	"strings"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/app"
	"fyne.io/fyne/v2/canvas"
	"fyne.io/fyne/v2/container"
)

func main() {
	a := app.New()
	
	w := a.NewWindow("Some Practice")
	
	w.Resize(fyne.NewSize(400, 400))
	
	text := canvas.NewText(strings.Repeat("this\n", 100), color.White)
	
	c := container.NewMax(container.NewVScroll(text))
	
	w.SetContent(c)
	
	w.ShowAndRun()
}

因为当我运行应用程序时,"\n"显示为无效字符,而不是换行符。
我的目标是使用canvas.NewText()显示非常大的文本,并且可以从上到下滚动,但"\n"字符不起作用。它打印出"this?this?this?"而不是从上到下滚动显示文本。

英文:

I am new to programming and i want to develop a application but i need show to users very large text in my application.
I am not sure widget.NewRichTextWithText() is for this purpose.
This code working as i expected.

package main

import (
	"strings"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/app"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/widget"
)

func main() {
	a := app.New()
	
	w := a.NewWindow("Some Practice")
	
	w.Resize(fyne.NewSize(400, 400))
	
	text := widget.NewRichTextWithText((strings.Repeat("this\n", 100)))
	
	c := container.NewMax(container.NewVScroll(text))
	
	w.SetContent(c)
	
	w.ShowAndRun()
}

But this one does not work.

package main

import (
	"image/color"
	"strings"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/app"
	"fyne.io/fyne/v2/canvas"
	"fyne.io/fyne/v2/container"
)

func main() {
	a := app.New()
	
	w := a.NewWindow("Some Practice")
	
	w.Resize(fyne.NewSize(400, 400))
	
	text := canvas.NewText(strings.Repeat("this\n", 100), color.White)
	
	c := container.NewMax(container.NewVScroll(text))
	
	w.SetContent(c)
	
	w.ShowAndRun()
}

Becuase the " \n " showing as invalid character instead of new line when i run the application.
My aim is showing very large text "top to bottom scrollable" with canvas.NewText() but " \n " character does not work. Instead of top to bottom scrollable showing text it is printing this as this?this?this?

答案1

得分: 1

使用widget.Label来显示“简单”的文本:

text := widget.NewLabel(strings.Repeat("this\n", 100))

请注意,widget.Label可以被“样式化”(例如,可以使其_斜体_或粗体)。

如果您想显示具有不同样式和格式的部分(称为段)的文本,则应使用widget.RichTextwidget.Label只能作为一个整体进行样式化。

英文:

Use widget.Label to display "simple" texts:

text := widget.NewLabel(strings.Repeat("this\n", 100))

Note that widget.Label can be "styled" (e.g. you can make it italic or bold).

widget.RichText should be used if you want to display text that has parts (called segments) with different styles and formatting. widget.Label can only be styled as a whole.

答案2

得分: 1

canvas.Text不会进行智能文本解析,所以你需要自己完成这个任务。
一般来说,widget包中有智能小部件可以为你完成所有这些操作,而canvas包则用于较低级别的绘图,通常用于构建其他小部件。

英文:

The canvas.Text does not do smart text parsing, so you would have to do that yourself.
In general the widget package has smart widgets that do this all for you, and the canvas package is for lower level drawing normally used to build other widgets.

huangapple
  • 本文由 发表于 2022年11月21日 02:36:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/74510859.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定