First, the method of judging the string containing Chinese characters
Iterate over the array, use String.byte () for each byte, and find that there are more than 127, that is, Chinese characters, you can refer to the following code.
Second, the calculation string width function
Copy Code code as follows:
--Calculate string width
Local str = "Jimmy: Hello, world!"
Local fontsize = 20
Local Leninbyte = #str
Local width = 0
For I=1,leninbyte do
Local curbyte = String.byte (str, i)
Local byteCount = 1;
If Curbyte>0 and curbyte<=127 then
ByteCount = 1
ElseIf curbyte>=192 and curbyte<223 Then
ByteCount = 2
ElseIf curbyte>=224 and curbyte<239 Then
ByteCount = 3
ElseIf curbyte>=240 and curbyte<=247 Then
ByteCount = 4
End
Local char = string.sub (str, I, i+bytecount-1)
i = i + byteCount-1
If ByteCount = 1 Then
width = width + fontsize * 0.5
Else
width = width + fontsize
Print (char)
End
End
Print (total width:). Width