Android Question How many characters are in one line?

ArminKH

Well-Known Member
hi all
i have 2 question plz help me
question 1:
how i can return the lenght of characters in a line?
my string is multi line but i want know how many char is in any line
for example in this text:
Line 1-"This is test"
Line 2-"This is"

in line 1 i have 12 character and in line 2 i have 7 character
is there any way to calculate and return this numbers?

question 2:
i want return the width of any line of a multiline
i know this possible with canvas.measurestringwidth
but when we have for example 3 word in 3 line(with CRLF)
how we can know what is the width of line 2?
and may be need to understand how many line we have

plz help

tnx
 

Straker

Active Member
Licensed User
Longtime User
You know how to measure a single line.
Now you have a multiline, a string in which each line is separated from the others wit a crlf.

You just have to split the multiline. For example you can transform it in a list in which each entry is a single line.

You can easily do it with RegEx (regular expression)
Just search 'split regex' or 'split function' and you'll find some example.
 
Upvote 0

ArminKH

Well-Known Member
ok but if our text be in label and we dont use CRLF then can we split each line in separate line?
 
Upvote 0

Straker

Active Member
Licensed User
Longtime User
Mmmmmh... I don't think it's easy (but I'm a newbie with b4a so maybe the solution is something I don't know).

I too would like to know if there is a simple way to count the lines in word wrapped label.

Anyway, I suppose you need this information just to adapt the font size in relation with the label's size, just to have a better visualization on different display.
In this case you can use some libraries. See the following links:

http://www.b4x.com/android/forum/threads/label-utils.39338/#content
http://www.b4x.com/android/forum/threads/30642/#content
 
Upvote 0

ArminKH

Well-Known Member
Tnx i see
I have my own formul like that
Ok dear straker i hope erel see this question tnx again
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
you can use regex.split to split the text into lines based on the linefeed.

then you can loop throught the array and use variable length.


B4X:
Dim t As String
Dim lines() As String
t="This is test" & Chr(13) & Chr(10) & "This is"
lines=Regex.Split(Chr(13)&Chr(10),t)
For x=0 To lines.Length-1
Log(lines(x)&" : "&lines(x).Length)
Next

output:

** Activity (main) Create, isFirst = true **
This is test : 12
This is : 7
** Activity (main) Resume **
 
Upvote 0

ArminKH

Well-Known Member
Its true but we dont know what is the label text
I just tell that is an example and string can be different after user run program
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
the regex will remain the same if you want to split on linefeeds

B4X:
t=edittxt.text 'or label1.text whatever
lines=Regex.Split(Chr(13)&Chr(10),t)
 
Upvote 0
Top