Calculate width and Height of a Text

nazilabdulla

Member
Licensed User
Longtime User
Is it Possible to calculate width and Height of Text in Turtle,
If We have following parameters
1. TextSize
2. Pensize
3. String Length (Number Char in Text)
 

nazilabdulla

Member
Licensed User
Longtime User
Let you know what is my requirement,
I have one Text, That will draw in Turtle,
Next Step , I need to Highlight particular section of above Text..
Highlighting means ….
Under line particular area of text
Or
Circle on particular area of Text
Or
Put Arrow or some other Drawing on particular area of text

My Logic is to calculate text width and find the location of particular text.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Example of drawing text with underline:
B4X:
'globals:
Private cvs As B4XCanvas
    Private fnt As B4XFont

Sub Turtle_Start
    If cvs.TargetRect = Null Then
        Dim p As B4XView = xui.CreatePanel("")
        p.SetLayoutAnimated(0, 0, 0, 2dip, 2dip)
        cvs.Initialize(p)
        fnt = xui.CreateDefaultFont(20)
    End If
    Turtle.SetSpeedFactor(1).SetPenColor(xui.Color_Blue).SetPenSize(1)
    Turtle.SetFontAndAlignment(fnt, "LEFT")
   
   DrawUnderlineText("Asdasd asdasd")
   Turtle.SetAngle(90).PenUp.MoveForward(25).PenDown
    DrawUnderlineText("Second line")
End Sub

Sub DrawUnderlineText(s As String)
    Dim r As B4XRect = cvs.MeasureText(s, fnt)
    Turtle.PushState
    Turtle.DrawText(s)
    Turtle.PopState
    Turtle.PushState
    Turtle.PenUp.SetAngle(90).MoveForward(r.Height / 2 / xui.Scale + 3).PenDown
    Turtle.SetAngle(0).MoveForward(r.Width / xui.Scale)
    Turtle.PopState
End Sub

java_tJdOHGjbAB.png
 
Last edited:
Top