Jump to content

[Question] Text length


XML

Recommended Posts

I mean automatic 'WordWrap' cause i want to make like a chat system using the grid list,

so i want to check if the sender's text reached the end of the line, to put it in a new line under the text's line.

Link to comment
  • Moderators

 

@XML

There is a dx function for it, you will need to check if the DX text length is the same as the GUI. (make sure you set the same font) If true, then it might work for you.

https://wiki.multitheftauto.com/wiki/DxGetTextWidth

If false, then you will need to adjust it with a scale factor.

 

 

10 words per line reads best! (at least that is for the Dutch language)

Edited by IIYAMA
Link to comment

if u mean u have long text and u want to insert it as multi line in the list depending in the text length

try my function :

function multiLineForText(txt,maxForEachLine)
	if not( txt and maxForEachLine ) then
		return
	end
	array = {}
	_txt = ''
	_count = 1
	if ( #txt >= maxForEachLine ) then
		for i=1,#txt do
			_txt = _txt..''..string.sub(txt,i,i)
			if ( i == maxForEachLine ) then
				array[#array+1] = _txt
				_txt = ''
				_count = _count+1
			elseif ( i == maxForEachLine*_count ) then
				array[#array+1] = _txt
				_txt = ''
				_count = _count+1
			else
				if ( i == #txt and _txt ~= '' ) then
				array[#array+1] = _txt
				end
			end
		end
	else
		array[#array+1] = txt
	end
	return array
end

use it as a table , for example :
 

function multiLineForText(txt,maxForEachLine)
	if not( txt and maxForEachLine ) then
		return
	end
	array = {}
	_txt = ''
	_count = 1
	if ( #txt >= maxForEachLine ) then
		for i=1,#txt do
			_txt = _txt..''..string.sub(txt,i,i)
			if ( i == maxForEachLine ) then
				array[#array+1] = _txt
				_txt = ''
				_count = _count+1
			elseif ( i == maxForEachLine*_count ) then
				array[#array+1] = _txt
				_txt = ''
				_count = _count+1
			else
				if ( i == #txt and _txt ~= '' ) then
				array[#array+1] = _txt
				end
			end
		end
	else
		array[#array+1] = txt
	end
	return array
end

local list = guiCreateGridList ( 0, 0, 350,350,false )
local column = guiGridListAddColumn( list, 'Player', 1 )
txt_ = 'test1test1test1test1test1test1test1test2test2test2test2test2test2test2test3test3test3test3test3test3test34fewwwwosk'
for i,v in ipairs(multiLineForText(txt_,35)) do -- 35 change it to an appropriate max for text per line 
	local row = guiGridListAddRow ( list )
	guiGridListSetItemText ( list, row, column,v, false, false )
end

 

Edited by MTASAPLAYER
Link to comment
  • Moderators
5 hours ago, XML said:

Do you mean like using:


 guiGridListGetColumnWidth

Because the text will be inside the grid list.

No, that is the width you set when creating the column. You need to check the length based on each word or character depending how you want to break it. You can probably break it with a new line, if it supports that.

@MTASAPLAYER

Not every character has the same width. 1x m = 1 em. It will never look nicely wrapped if you use that method.

 

 

Edited by IIYAMA
Link to comment

@MTASAPLAYER

Thank you for this function, But as @IIYAMA said, It's not gonna look nice,

Cause it will split the characters to a new line like for example if this word reached the end of the line: Hello

it will be splitted to for example like: He

llo

Edited by XML
Link to comment
6 hours ago, XML said:

@MTASAPLAYER

Thank you for this function, But as @IIYAMA said, It's not gonna look nice,

Cause it will split the characters to a new line like for example if this word reached the end of the line: Hello

it will be splitted to for example like: He

llo

I didn't get ur full point yet,

do u want multi lines in the list for the same length 100%

or just it should be about the length not exactly the same length but with full word?

if u need same length 100% and full word , that's in my opinion impossible .

Link to comment

I'll try to give you an example:

let's say that this line below is the column line:

__________________________________________
This is my text in the row that has reached
the end of the line and this is the second
line

 

so when this part: 'the end of the line and this is the second' reaches the end of the line, And there is no more enough space,

it will move it to a new line below the same text. Just like the example above, I hope you got my point now.

Edited by XML
Link to comment
function wordWrap(text, maxwidth, scale, font, colorcoded)
  local lines = {}
  local words = split(text, " ")
  local line = 1
  local word = 1
  while (words[word]) do -- while there are still words to read
    repeat
      lines[line] = (lines[line] and (lines[line] .. " ") or "") .. words[word]
      word = word + 1
    until ((not words[word]) or dxGetTextWidth(lines[line] .. " " .. words[word], scale, font, colorcoded) >= maxwidth) -- stop if next word doesn't exist to text width would exceed maxwidth if next word was added
    line = line + 1
  end -- jumps back to while(words[word])
  return lines
end

You can use this function similarly to multiLineForText from MTASAPLAYER, or use table.concat(wordWrap(...), "\n") to concatenate the lines into one string separated with newline characters.

Note: This function removes consecutive space characters, so four spaces will be shortened to one space due to how split words.

Edited by MrTasty
Link to comment
On 7/19/2018 at 14:24, IIYAMA said:

No, that is the width you set when creating the column. You need to check the length based on each word or character depending how you want to break it. You can probably break it with a new line, if it supports that.

@MTASAPLAYER

Not every character has the same width. 1x m = 1 em. It will never look nicely wrapped if you use that method.

 

 

i don’t think he’s really care about the width as much as viewing the full word  , therefore i  think @MrTasty’s is useful for what @XML asked for

Edited by MTASAPLAYER
Link to comment
  • Moderators
1 hour ago, MTASAPLAYER said:

i don’t think he’s really care about the width as much as viewing the full word  , therefore i  think @MrTasty’s is useful for what @XML asked for

If you look closely, MrTasty came with the same solution as I did. Just he had already written the function for it.

And yes indeed, it is the solution XML asked for.

 


 

My solution

https://wiki.multitheftauto.com/wiki/DxGetTextWidth

 

Mr Tasty his solution + sample / implementation

 until ((not words[word]) or dxGetTextWidth(lines[line] .. " " .. words[word], scale, font, colorcoded) >= maxwidth) -- stop if next word doesn't exist to text width would exceed maxwidth if next word was added

 

 

 

 

 

 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...