Planet JFX

Centering Text

118pages on
this wiki
Add New Page
Add New Page Talk0

Summary Edit

On the mailing list was a discussion of how to center text in nodes.


Code Edit

import javafx.ui.*;
import javafx.ui.canvas.*;

var contentText = "Sardegna"; //very nice island, go there once ;-)
var myFont = Font {face: VERDANA, style: [BOLD], size: 50};

Frame {
    Canvas {
        var: canvas
        height: 70
        width: 450
        border: null
        Group {
            var w = bind canvas.width
            var h = bind canvas.height
            [Rect {
                width: bind w
                height: bind h
                fill: LinearGradient {
                    x1: 0
                    y1: 0
                    x2: 1
                    y2: 0
                    [Stop {offset: 0, color: green},
                    Stop {offset: .5, color: new Color(.5, 1, 0, 1)},
                    Stop {offset: 1, color: green}]
                stroke: green
                strokeWidth: 1
            Text {
                transform: bind translate(w/2, h/2)
                //verticalAlignment: BASELINE
                valign: MIDDLE
                halign: CENTER
                content: contentText
                font: myFont
                fill: darkgreen

Also on Fandom

Random Wiki