[GH-ISSUE #272] TextView: Vertical Alignment #210

Closed
opened 2026-03-04 01:03:00 +03:00 by kerem · 4 comments
Owner

Originally created by @amery on GitHub (Apr 19, 2019).
Original GitHub issue: https://github.com/rivo/tview/issues/272

Hi, could you provide an example about how to center vertically the text of a TextView?

Originally created by @amery on GitHub (Apr 19, 2019). Original GitHub issue: https://github.com/rivo/tview/issues/272 Hi, could you provide an example about how to center vertically the text of a TextView?
kerem closed this issue 2026-03-04 01:03:00 +03:00
Author
Owner

@diamondburned commented on GitHub (Apr 22, 2019):

I tried implementing a Primitive for that here: https://gitlab.com/diamondburned/spotuify/blob/master/views/loading/loading.go#L34

Do note that this is not a TextView, which means it does not support markup formatting.

<!-- gh-comment-id:485434190 --> @diamondburned commented on GitHub (Apr 22, 2019): I tried implementing a Primitive for that here: https://gitlab.com/diamondburned/spotuify/blob/master/views/loading/loading.go#L34 Do note that this is *not* a TextView, which means it does not support markup formatting.
Author
Owner

@amery commented on GitHub (Apr 22, 2019):

thanks, I'll study it :)

<!-- gh-comment-id:485554225 --> @amery commented on GitHub (Apr 22, 2019): thanks, I'll study it :)
Author
Owner

@rivo commented on GitHub (May 14, 2019):

There is no automatic vertical alignment in a TextView. This would lead to navigation issues so I don't think such a flag would become part of this primitive.

But if you know in advance how many lines your TextView will have, you can center it by putting it in the middle of a three-row Flex where only the top and bottom rows have flexible heights.

If the number of lines is unknown and may even be larger than the available height, I cannot think of a solution with TextView at the moment.

<!-- gh-comment-id:492276909 --> @rivo commented on GitHub (May 14, 2019): There is no automatic vertical alignment in a `TextView`. This would lead to navigation issues so I don't think such a flag would become part of this primitive. But if you know in advance how many lines your `TextView` will have, you can center it by putting it in the middle of a three-row `Flex` where only the top and bottom rows have flexible heights. If the number of lines is unknown and may even be larger than the available height, I cannot think of a solution with `TextView` at the moment.
Author
Owner

@caiguanhao commented on GitHub (Jan 3, 2022):

package main

import (
	"github.com/gdamore/tcell/v2"
	"github.com/rivo/tview"
)

type verticalText struct {
	*tview.TextView
}

func (t *verticalText) Draw(s tcell.Screen) {
	_, _, _, height := t.TextView.GetRect()
	t.TextView.SetBorderPadding(height/2, 0, 0, 0)
	t.TextView.Draw(s)
}

func main() {
	app := tview.NewApplication()
	text := tview.NewTextView().
		SetText("This is vertically-aligned text").
		SetTextAlign(tview.AlignCenter)
	vText := &verticalText{text}
	if err := app.SetRoot(vText, true).Run(); err != nil {
		panic(err)
	}
}

or:

package main

import (
	"github.com/gdamore/tcell/v2"
	"github.com/rivo/tview"
)

func main() {
	app := tview.NewApplication()
	text := tview.NewTextView().
		SetText("This is vertically-aligned text").
		SetTextAlign(tview.AlignCenter)
	text.SetDrawFunc(func(screen tcell.Screen, x, y, w, h int) (int, int, int, int) {
		y += h / 2
		return x, y, w, h
	})
	if err := app.SetRoot(text, true).Run(); err != nil {
		panic(err)
	}
}
<!-- gh-comment-id:1003848373 --> @caiguanhao commented on GitHub (Jan 3, 2022): ```go package main import ( "github.com/gdamore/tcell/v2" "github.com/rivo/tview" ) type verticalText struct { *tview.TextView } func (t *verticalText) Draw(s tcell.Screen) { _, _, _, height := t.TextView.GetRect() t.TextView.SetBorderPadding(height/2, 0, 0, 0) t.TextView.Draw(s) } func main() { app := tview.NewApplication() text := tview.NewTextView(). SetText("This is vertically-aligned text"). SetTextAlign(tview.AlignCenter) vText := &verticalText{text} if err := app.SetRoot(vText, true).Run(); err != nil { panic(err) } } ``` or: ```go package main import ( "github.com/gdamore/tcell/v2" "github.com/rivo/tview" ) func main() { app := tview.NewApplication() text := tview.NewTextView(). SetText("This is vertically-aligned text"). SetTextAlign(tview.AlignCenter) text.SetDrawFunc(func(screen tcell.Screen, x, y, w, h int) (int, int, int, int) { y += h / 2 return x, y, w, h }) if err := app.SetRoot(text, true).Run(); err != nil { panic(err) } } ```
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/tview#210
No description provided.