When using Graphics.DrawString is there any way to determine, before the render, the actual dimensional size that the text will take up when using WrapWidth and/or Condense?
Mainly this would be used for centering text in a button, if that text go too long then it would auto condense or wrap depending on user settings. If it wraps when I need to get the precise measurement so I can center is vertically.
I see there is StringWidth, but this doesnt cope with multiline, and there’s obviously StringHeight.
Do I need to throw out Graphics.DrawString and write my own or am I missing something?
There is a way around the lack of precision in Windows : measure stringwidth and stringheight for a font size 10 or even 100 times bigger, and divide by 10 or 100.
[code] dim theText as string = ReplaceAll(pText, “:”, ": ")
theText = ReplaceAll(theText, “: :”, “::”)
theText = ReplaceAll(theText, “/”, "/ ")
dim TextArray(-1) as string = Split(theText, " ")
dim FinalText(-1) as String
dim currentLine as String
for currentWord as Integer = 0 to UBound(TextArray)
if currentLine = “” and getStringWidth(TextArray(currentWord)) < me.Width - 20 then
'first word
currentLine = TextArray(currentWord)
elseif currentLine <> “” and getStringWidth(currentLine + " " + TextArray(currentWord)) < me.Width then
'need one more word
currentLine = currentLine + " " + TextArray(currentWord)
currentLine = ReplaceAll(currentLine, ": ", “:”)
else
'length reached
currentLine = ReplaceAll(currentLine, ": ", “:”)
FinalText.Append currentLine
currentLine = TextArray(currentWord)
end if
next
FinalText.Append currentLine
Return Join(FinalText, " ")
End Function[/code]
and
[code]Private Function getStringWidth(theString as String) as Integer
dim thePicture as new Picture(500, 100, 32)
thePicture.Graphics.TextFont = me.TextFont
thePicture.Graphics.TextSize = me.TextSize
Return thePicture.Graphics.StringWidth(theString)
End Function[/code]
As far as I can see the intent of my code is a different one. I have text with “bla:something:bla:else” that needs to be changed to “bla:something: bla:else” with a space in the middle so that the text breaks.
Dim s As String="Now is the time for all good men to come to the aid of their country"
Dim p As picture
Dim g As graphics
Dim whole As Double
Dim sum As Double
Dim i As Integer
Dim c As String
p=New picture(10,10)
g=p.Graphics
g.TextFont="Arial" // to insure the same font on both OSX and windows
g.textsize=48
whole=g.StringWidth(s)
For i=1 To Len(s)
c=Mid(s,i,1)
sum=sum+g.StringWidth(c)
Next i
MsgBox "Whole="+Str(whole)+" Sum="+Str(sum)
// Xojo 2016 r4.1
// OSX 10.11.6 returns 1411.359 for both values
// Win 10 returns
Unfortunatly my remote connection to my Win10 machine is not working, but I’ll bet dollars to doughnuts that the above code run on Win 10 will return something closer to 1400 (or less)
EDIT : Windows 10 return 1405.000 as opposed to OSX with 1411.359
Sure Windows uses integer values. Let us say Mac would report 10.7 using a double.
I measure stringwidth for fontsize*10. The result is 107 (Integer). Divided by 10 gives 10.7 (double). Do you want me to write the method for you that returns a double ?
Its ok guys, StringWidth isnt the issue I have, as I said in my OP but thanks anyway
Its to do with the varying space at the end of a wrapped/shortened line.
Thanks for all the source code etc. I can write it without a problem I just really needed to know if there was something obvious that I had overlooked, before I wrote my own.