Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

TextAlign() not working

in Bugs Posts: 15

I am trying to make my text aligned to the left, but it stays aligned to the center. Can I have some help??

Comments

  • Posts: 1,865
    @DJMoffinz - could you post a little code to see what you are trying to do.
  • edited November 6 Posts: 15
    -- Area Calculator
    
    -- Use this function to perform your initial setup
    function setup()
        Pi = math.pi
        CenterX = WIDTH/2
        CenterY = HEIGHT/2
        CirclePos = WIDTH/4 +100
        parameter.number("Diameter",10,600,200)
    end
    
    -- This function gets called once every frame
    function draw()
        textAlign(LEFT)
        TextPos = CirclePos + (Diameter/2) + 100
        LineThickness = 2.5+2.5*Diameter/600
        -- This sets a dark background color 
        background(25)
    
        strokeWidth(LineThickness)
    
        fontSize(14.5+(2.5+2.5*Diameter/600))
    
        -- Do your drawing here
        fill(255)
    
        if Diameter <= 181 then
            text("Diameter: "..string.format("%.2f",Diameter).."px",TextPos,CenterY)
    
        elseif Diameter >= 181 then
            text("Diameter: "..string.format("%.2f",Diameter).."px",CirclePos,(CenterY+12.5)+LineThickness)
            pushStyle()
            textAlign(CENTER)
            fontSize(17)
        end
        text("Radius: "..string.format("%.2f",Diameter/2) .."px",TextPos,CenterY -20)
        text("Area: "..string.format("%.2f",Pi*((Diameter/2)^2)).."px",TextPos,CenterY -40)
        text("Circumference: "..string.format("%.2f",Diameter*Pi).."px",TextPos,CenterY -60)
        pushStyle()
        noFill()
        ellipse(CirclePos,CenterY,Diameter)
    
        line(CirclePos - (Diameter/2 -4),CenterY,CirclePos + (Diameter/2 -4),CenterY)
    end
    
  • dave1707dave1707 Mod
    Posts: 8,671

    @DJMoffinz If you want to use string.format to align text, here’s an example. You have to use a fixed width font, in this case Courier. Then you use values to define the sizes of the values you display. A minus sign before a value aligns left.

    viewer.mode=FULLSCREEN
    
    function setup()
        font("Courier-Bold")
        fill(255)
        t1="Diameter"
        v1=123.4567
        t2="Size"
        v2=23.65
        t3="Radius"
        v3=25.7654
    end
    
    function draw()
        background(0)
        text(string.format("%-15s%10.2f",t1,v1),WIDTH/2,HEIGHT/2+100)
        text(string.format("%-15s%10.2f",t2,v2),WIDTH/2,HEIGHT/2+50)
        text(string.format("%-15s%10.2f",t3,v3),WIDTH/2,HEIGHT/2)   
    end
    
Sign In or Register to comment.