hello, I created a class of type canvas and paint I added a text with Drawstring. the text comes out slightly to the left. How can I do to make it completely centered?
g.drawstring s,(g.width-g.stringwidth(s))\2,y
Or if you want it in the middle vertically too:
//constant string to store the text
Const s = "Your text here"
//draw the text completely in the centre of the graphics area
g.DrawString(s,(g.width - g.stringwidth(s)) \\ 2, (g.height - g.stringwidth(s)) / 2)
HSH
actually it would be
//constant string to store the text
Const s = "Your text here"
//draw the text completely in the centre of the graphics area
g.DrawString(s,(g.width - g.stringwidth(s)) \\ 2, (g.height-g.textascent)\\2)
[quote=63531:@Dave S]actually it would be
//constant string to store the text
Const s = "Your text here"
//draw the text completely in the centre of the graphics area
g.DrawString(s,(g.width - g.stringwidth(s)) \\ 2, (g.height-g.textascent)\\2)
[/quote]
Thankyou. I did not know this! And sorry for misinforming. :\
[quote=63529:@Oliver Scott-Brown]//constant string to store the text
Const s = “Your text here”
//draw the text completely in the centre of the graphics area
g.DrawString(s,(g.width - g.stringwidth(s)) \ 2, (g.height - g.stringwidth(s)) / 2)[/quote]
I meant to write this: g.DrawString(s,(g.width - g.stringwidth(s)) \ 2, (g.height - g.stringheight(s)) / 2)
, for one of the lines.
Wouldn’t this also work? Of coarse g.textascent would be better because it is slightly more convenient and possible a little faster in performance. I may not be right? But I always seem to make myself look stupid on the forums.
Of course
Oops… and I have been corrected for this one before. Atleast I am learning how to spell from making posts on the forums. Thanks.
You could do that… but I think TextAscent would be more accurate (if only by a few pixels)…
And Oliver… making mistakes is how we learn… there is no such thing as stupid questions…
[quote=63541:@Dave S]You could do that… but I think TextAscent would be more accurate (if only by a few pixels)…
And Oliver… making mistakes is how we learn… there is no such thing as stupid questions…[/quote]
I know, thank you; but I am not scared of asking questions and looking stupid, I am actually referring to misinforming people but I guess I get people correcting me anyway so I will either inform correctly or be informed of a misinformation (either way I learn something). And sorry for the long sentence. Again, thanks.
Also remember, we are talking Software Developement here… and while there are WRONG ways to do things, there are also many, many different CORRECT ways to do things as well… Some are better than others… some are more personal preference…
And don’t worry… if you state something that is totally wrong… two things will happen
- someone will correct your mistake
- you hopefully will have learned something
Which is the whole purpose of this forum.
It’s counter-intuitive and looks funny, but the Y coordinate should be (g.height + g.textascent)/2
seems the question is “do you want the text baseline centered vertically or the text actually centered vertically” as those are two slightly different things and they will draw ever so slightly differently
I give you thanks!