The -12 is compensating for the height of the text. The value used for the Y offset should be half the text height
So using the same example code:
// Project: ButtonRotate
// Created: 2019-02-22
// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "ButtonRotate" )
SetWindowSize( 1024, 768, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window
// set display properties
SetVirtualResolution( 1024, 768 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 30, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts
// Create the button
img= CreateImageColor(20,50,200,255)
spr = CreateSprite(img)
SetSpritesize(spr,70,30)
SetSpritePositionByOffset(spr,300,200)
// create text to go on it
txt =CreateText("Button")
SetTextAlignment(txt,1)
SetTextSize(txt,20)
do
RotateButton(spr,txt,Timer()*40)
Print( ScreenFPS() )
Sync()
loop
function RotateButton(butspr as integer, txt as integer, angle as float)
SetSpriteAngle(butspr,angle)
SetTextAngle(txt,angle)
posx# = GetWorldXFromSprite(butspr,0,-GetTextSize( txt )*0.55)
posy# = GetWorldYFromSprite(butspr,0,-GetTextSize( txt )*0.55)
SetTextPosition(txt,posx#,posy#)
endfunction
It then works no matter what the size of the sprite or the text is.
i actually used 0.55 instead of 0.5 as it centres the text vertically slightly better in my opnion.