Documentation
¶
Overview ¶
Package richtext provides rendering of text containing multiple fonts, styles, and levels of interactivity.
Example ¶
package main
import (
"image/color"
"log"
"github.com/nanorele/gio-x/richtext"
"github.com/nanorele/gio/app"
"github.com/nanorele/gio/font/gofont"
"github.com/nanorele/gio/gesture"
"github.com/nanorele/gio/op"
"github.com/nanorele/gio/text"
"github.com/nanorele/gio/unit"
"github.com/nanorele/gio/widget/material"
)
func main() {
var (
fonts = gofont.Collection()
th = material.NewTheme()
black = color.NRGBA{A: 255}
green = color.NRGBA{G: 170, A: 255}
blue = color.NRGBA{B: 170, A: 255}
red = color.NRGBA{R: 170, A: 255}
)
th.Shaper = text.NewShaper(text.WithCollection(fonts))
go func() {
w := new(app.Window)
// allocate persistent state for interactive text. This
// needs to be persisted across frames.
var state richtext.InteractiveText
interactColors := []color.NRGBA{black, green, blue, red}
interactColorIndex := 0
var ops op.Ops
for {
e := w.Event()
switch e := e.(type) {
case app.DestroyEvent:
panic(e.Err)
case app.FrameEvent:
gtx := app.NewContext(&ops, e)
// define the text that you want to present. This can be persisted
// across frames, recomputed every frame, or modified in any way between
// frames.
var spans []richtext.SpanStyle = []richtext.SpanStyle{
{
Content: "Hello ",
Color: black,
Size: unit.Sp(24),
Font: fonts[0].Font,
},
{
Content: "in ",
Color: green,
Size: unit.Sp(36),
Font: fonts[0].Font,
},
{
Content: "rich ",
Color: blue,
Size: unit.Sp(30),
Font: fonts[0].Font,
},
{
Content: "text\n",
Color: red,
Size: unit.Sp(40),
Font: fonts[0].Font,
},
{
Content: "Interact with me!",
Color: interactColors[interactColorIndex%len(interactColors)],
Size: unit.Sp(40),
Font: fonts[0].Font,
Interactive: true,
},
}
// process any interactions with the text since the last frame.
for {
span, event, ok := state.Update(gtx)
if !ok {
break
}
content, _ := span.Content()
switch event.Type {
case richtext.Click:
log.Println(event.ClickData.Kind)
if event.ClickData.Kind == gesture.KindClick {
interactColorIndex++
gtx.Execute(op.InvalidateCmd{})
}
case richtext.Hover:
w.Option(app.Title("Hovered: " + content))
case richtext.Unhover:
w.Option(app.Title("Unhovered: " + content))
case richtext.LongPress:
w.Option(app.Title("Long-pressed: " + content))
}
}
// render the rich text into the operation list
richtext.Text(&state, th.Shaper, spans...).Layout(gtx)
// render the operation list
e.Frame(gtx.Ops)
}
}
}()
app.Main()
}
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var LongPressDuration time.Duration = 250 * time.Millisecond
LongPressDuration is the default duration of a long press gesture. Override this variable to change the detection threshold.
Functions ¶
This section is empty.
Types ¶
type Event ¶
type Event struct {
Type EventType
// ClickData is only populated if Type == Clicked
ClickData gesture.ClickEvent
}
Event describes an interaction with rich text.
type InteractiveSpan ¶
type InteractiveSpan struct {
// contains filtered or unexported fields
}
InteractiveSpan holds the persistent state of rich text that can be interacted with by the user. It can report clicks, hovers, and long-presses on the text.
func (*InteractiveSpan) Content ¶
func (i *InteractiveSpan) Content() (string, map[string]interface{})
Content returns the text content of the interactive span as well as the metadata associated with it.
func (*InteractiveSpan) Get ¶
func (i *InteractiveSpan) Get(key string) interface{}
Get looks up a metadata property on the interactive span.
func (*InteractiveSpan) Layout ¶
func (i *InteractiveSpan) Layout(gtx layout.Context) layout.Dimensions
Layout adds the pointer input op for this interactive span and updates its state. It uses the most recent pointer.AreaOp as its input area.
type InteractiveText ¶
type InteractiveText struct {
Spans []InteractiveSpan
// contains filtered or unexported fields
}
InteractiveText holds persistent state for a block of text containing spans that may be interactive.
func (*InteractiveText) Update ¶
func (i *InteractiveText) Update(gtx layout.Context) (*InteractiveSpan, Event, bool)
Update returns the first span with unprocessed events and the events that need processing for it.
type SpanStyle ¶
type SpanStyle struct {
Font font.Font
Size unit.Sp
Color color.NRGBA
Content string
Interactive bool
// contains filtered or unexported fields
}
SpanStyle describes the appearance of a span of styled text.
type TextStyle ¶
type TextStyle struct {
State *InteractiveText
Styles []SpanStyle
Alignment text.Alignment
WrapPolicy styledtext.WrapPolicy
// LineHeight controls the distance between the baselines of lines of text.
// If zero, a sensible default will be used.
LineHeight unit.Sp
// LineHeightScale applies a scaling factor to the LineHeight. If zero, a
// sensible default will be used.
LineHeightScale float32
*text.Shaper
}
TextStyle presents rich text.