fyne的几种multiLine
创建一个MultiLine有一个专门的函数:
widget.NewMultiLineEntry()
进入方法看看源码:
// NewMultiLineEntry creates a new entry that allows multiple lines
func NewMultiLineEntry() *Entry {
e := &Entry{MultiLine: true, Wrapping: fyne.TextTruncate}
e.ExtendBaseWidget(e)
return e
}
可以看到实际上返回的还是Entry,只不过是将MultiLine变量设置为了true,然后有一个wrapping变量。
这样我们可以自己设置wrapping的值。
wrappings是一个TextWrap类型,代表了长度超过widget宽度的文本将如何换行。
type TextWrap int
wrapping有如下几个值:
const (
// TextWrapOff extends the widget's width to fit the text, no wrapping is applied.
// 暂时不考虑这个值
TextWrapOff TextWrap = iota
// TextTruncate trims the text to the widget's width, no wrapping is applied.
// If an entry is asked to truncate it will provide scrolling capabilities.
//
// Deprecated: Use `TextTruncateClip` value of the widget `Truncation` field instead
// 不换行,会产生滚动条,已废弃
TextTruncate
// TextWrapBreak trims the line of characters to the widget's width adding the excess as new line.
// An Entry with text wrapping will scroll vertically if there is not enough space for all the text.
// 换行,如果空间不够,产生垂直滚动条
TextWrapBreak
// TextWrapWord trims the line of words to the widget's width adding the excess as new line.
// An Entry with text wrapping will scroll vertically if there is not enough space for all the text.
// 单词是一个整体换行
TextWrapWord
)
代码:
package main
import (
"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("Base64 Encoder / Decoder")
w.SetContent(makeUI())
w.Resize(fyne.NewSize(400, 300))
w.CenterOnScreen()
w.ShowAndRun()
}
func makeUI() fyne.CanvasObject {
text1 := widget.NewMultiLineEntry()
text1.SetPlaceHolder("text1")
text2 := widget.NewEntry()
text2.MultiLine = true
text2.Wrapping = fyne.TextWrapBreak
text2.SetPlaceHolder("text2")
text3 := widget.NewEntry()
text3.MultiLine = true
text3.Wrapping = fyne.TextWrapWord
text3.SetPlaceHolder("text3")
return container.NewVBox(text1, text2, text3)
}
第一个未换行,第二个换行了,但是单词不是一个整体,第三个是单词整体换行。