Boechat Posted April 10, 2021 Posted April 10, 2021 (edited) Eai rapaziada, eu estava precisando de um código de wordWrap e o que está disponível na wiki não funcionou direito pro que eu precisei, ficou muito bugado não sei porque. Achei melhor criar um código com o mesmo retorno e vou disponibiliza-lo aqui, talvez possa ser útil pra mais alguém... A diferença é que não está removendo a cor do texto, caso queiram modifiquem aí e adicionem isso ao código: string.gsub(text, "#%x%x%x%x%x%x", "") Sei que ainda tem alguns detalhes que podem ser melhorados, mas está funcionando bem. Até então não encontrei nenhum bug, se alguém encontrar comente aí Spoiler String text é o texto que você deseja quebrar linha Float limite é o limite em pixel até que o texto quebre de linha Fonte font é a fonte do texto, pode ser uma fonte criada com dxCreateFont ou uma fonte padrão do GTA: "default" "default-bold" "clear" "arial" "sans" "pricedown" "bankgothic" "diploma" "beckett" Int scale tamanho da fonte O retorno é uma tabela de strings, cada posição da tabela contém uma linha do texto no tamanho passado por parâmetro. --Função function wordWrap( text, limite, font, scale ) local frase = {} if dxGetTextWidth ( text, scale, font) > limite then local cont = 1 local aux = "" local cache = "" for i = 1, string.len(text) do cache = aux aux = aux..string.sub(text, i, i) if dxGetTextWidth ( aux, scale, font) > limite then frase[cont] = cache cont = cont + 1 aux = "" end end if string.len(aux) then frase[cont] = aux end return frase else frase[1] = text return frase end end -- Exemplo de uso local screenW, screenH = guiGetScreenSize() function exemplo () local text = "Isso é um texto." local msg = wordWrap( text, screenW * 0.34, 'sans', 1) -- Tabela de string contendo cada linha do texto. outputChatBox ('O texto foi divido em '..#msg..' linhas.') end Edited April 10, 2021 by Boechat
Boechat Posted April 10, 2021 Author Posted April 10, 2021 (edited) Não consigo mais editar o tópico então vou deixar como resposta uma atualização do código, usem esse: Spoiler --Código function wordWrap( text, limite, font, scale) local frase = {} if dxGetTextWidth ( text, scale, font) > limite then local cont = 1 local aux = "" for i = 1, string.len(text) do if dxGetTextWidth ( aux..string.sub(text, i, i), scale, font) > limite then for i = 1, string.len(aux) do if string.sub(aux, 1, 1) == " " then aux = string.sub(aux, 2, #aux) else break end end frase[cont] = aux cont = cont + 1 aux = "" end aux = aux..string.sub(text, i, i) end if string.len(aux) then for i = 1, string.len(aux) do if string.sub(aux, 1, 1) == " " then aux = string.sub(aux, 2, #aux) else break end end frase[cont] = aux end return frase else frase[1] = text return frase end end Edited April 10, 2021 by Boechat
Other Languages Moderators Lord Henry Posted April 10, 2021 Other Languages Moderators Posted April 10, 2021 Isso é um tutorial? 1
Boechat Posted April 11, 2021 Author Posted April 11, 2021 23 hours ago, Lord Henry said: Isso é um tutorial? Então... Fiquei um pouco em dúvida em qual tópico postar mas não chega a ser um tutorial, é só um pedacinho de código pronto que quis disponibilizar então acho que se enquadra em "Programação Lua" né
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now