Align buttontext
Posted: Fri Jul 31, 2015 11:23 am
It took some effort, but I found how to scale button-size with font-size and text-length.
It is a contribution to the topic "Aligning Text In A Button" at viewtopic.php?f=26&p=6681#p6681
It is a contribution to the topic "Aligning Text In A Button" at viewtopic.php?f=26&p=6681#p6681
Code: Select all
'Align buttontext, by Dutchman, july 2015
'----- set display
FontSize=20
Font=12
DATA "Courier","Courier-Bold","Courier-BoldOblique" '1-3
DATA "Courier-Oblique","CourierNewPS-BoldItalicMT" '4-5
DATA "CourierNewPS-BoldMT","CourierNewPS-ItalicMT" '6-7
DATA "CourierNewPSMT","Menlo-Bold","Menlo-BoldItalic"'8-10
DATA "Menlo-Italic","Menlo-Regular" '11-12
GOSUB Initialise
'
'----- Presets
Random$.min=5 'letters
Random$.max=12 'letters
Aligned$.Space$=" " 'minimum space
'
'----- Set scaled size
length=2*Random$.max+LEN(Aligned$.Space$)
bw=(length+1)*0.6*FontSize 'button-width
bh=2*fontsize 'buttonheight
'
'----- MAIN
PRINT "Font is: """;FontName$;""""
dy=1.2*bh
FOR n=1 TO 5
BUTTON n TEXT Aligned$(Random$,Random$,length) AT 10,n*dy SIZE bw,bh
NEXT n
FILL COLOR 1,0,0
BUTTON n TEXT "QUIT" AT 10,n*dy SIZE bw,bh
DO ! UNTIL BUTTON_PRESSED(STR$(n,"#"))
END
'
'===== Functions and Subroutines
DEF Aligned$(w1$,w2$,n)
'NOTE: LEN(w1$&w2$&Space$) <= n
nn=LEN(w1$&w2$&Space$)
x$="" ! WHILE LEN(x$)<n-nn ! x$&=" " ! END WHILE
Txt$=w1$&Space$&x$&w2$
RETURN txt$
END DEF
'
DEF Random$
txt$=""
FOR i=1 TO min+RND(max-min+1)
txt$&=CHR$(97+RND(123-97))
NEXT i
RETURN txt$
END DEF
'
Initialise:
OPTION BASE 1
DIM Font$(12)
FOR i=1 TO 12 ! READ Font$(i)
NEXT i
FontName$=Font$((ABS(Font)-1)%12+1)
SET OUTPUT FONT NAME FontName$
SET OUTPUT FONT SIZE FontSize
SET BUTTONS CUSTOM
SET BUTTONS FONT NAME FontName$
SET BUTTONS FONT SIZE FontSize
DRAW COLOR 1,1,0
FILL COLOR 0,0,1
RETURN