RichText
The rich text widget displays text using many different styles, and can also include other elements such as images, hyperlinks and lists. It is the basis of the Label widget, which is a simpler API for the common case of a single run of text.
Content is provided as a list of widget.RichTextSegment values
passed to widget.NewRichText(). The widget package includes
segments for text, hyperlinks, images, lists, separators and code
blocks. A widget.TextSegment combines a Text string with a
widget.RichTextStyle, and the package provides presets such as
widget.RichTextStyleHeading and widget.RichTextStyleBlockquote.
Building the segment list by hand is flexible but verbose, so for
most documents it is easier to use widget.NewRichTextFromMarkdown()
which parses a markdown string into the appropriate segments.
You can later call ParseMarkdown(...) to replace the content.
By default, a rich text will grow to fit its content - set the
Wrapping field (for example to fyne.TextWrapWord) and place it
inside a scroll container to display longer documents.
package main
import (
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/widget"
)
func main() {
myApp := app.New()
myWindow := myApp.NewWindow("RichText Widget")
text := widget.NewRichTextFromMarkdown(`
# Rich Text
This paragraph has some *emphasis* and some **strong** words,
as well as a [link](https://fyne.io/).
* it can show lists
* and ` + "`code`" + ` as well`)
text.Wrapping = fyne.TextWrapWord
myWindow.SetContent(container.NewScroll(text))
myWindow.Resize(fyne.NewSize(300, 250))
myWindow.ShowAndRun()
}