[Resolved] Using \n in displaytext

Hi,

I have a weird problem using \n with display.newText

Example:

message[i].Question = "From what movie is the following movie quote:\nDo you like dags?"  
  
local Question = display.newText( message[i].Question, 65, 50, "Century Gothic", 30 )  
  

Above code works correctly displaying a new line, like:

From what movie is the following movie quote:  
Do you like dags?  

Now if i read the question data from an XML file with the same contents, it doesn’t create a newline, just the original string:

From what movie is the following movie quote:\nDo you like dags?  

What am i doing wrong?

[import]uid: 50459 topic_id: 14093 reply_id: 314093[/import]

How are you reading in the data from the XML file?

PS : The answer to the question is “Snatch” :wink: [import]uid: 84637 topic_id: 14093 reply_id: 51887[/import]

Hi,

Im using this xml.lua mentioned here:

http://blog.anscamobile.com/2011/07/how-to-use-xml-files-in-corona/

Download: https://github.com/jonbeebe/Corona-XML-Module

To get the question i grab one of the tags with:

message[i].Question = inbox.child[i].properties[‘question’]

xml file sample:

[code]

<?xml version="1.0"?>



Amsterdam
London
Brussels
New York


Argentina
Mexico
Germany
France


Tomato Juice
Vodka
Tabasco
Ginger


364
365
366
367
368
369


Keanu Reeves
William Reeves
Melissa Reeves
Christopher Reeves


A Few Good Men
Lock Stock and Two Smoking Barrels
Snatch
Fight Club


1946
1947
1948
1949
1950
1951



[/code] [import]uid: 50459 topic_id: 14093 reply_id: 51888[/import]

The xml parser might be reading the \n. You might have to escape it by coding your question string like this; “…first line of question\nSecond line of question”

Have no idea if this will work but I’ve often had to do things like that. Worth a try. [import]uid: 81264 topic_id: 14093 reply_id: 51902[/import]

I can’t get a newline character to work at all…can anyone verify that this actually works, and if so, what build number are you on, and what platform? [import]uid: 5317 topic_id: 14093 reply_id: 51941[/import]

I can get it to work with latest build if I put it directly into a text object as art of a string, however when calling from xml it must get encoded somehow and the textfield no longer recognizes it as a newline character.

Anybody have any ideas? [import]uid: 50154 topic_id: 14093 reply_id: 52283[/import]

Oh and I forgot it does not work at all with the display.newText provided by ansca, but it does work using the crawlspace multi line textfield (but not when using xml) [import]uid: 50154 topic_id: 14093 reply_id: 52289[/import]

I’m solved it by using a wrapper instead, that adds linefeeds:

function wrap(str, limit, indent, indent1)  
 indent = indent or ""  
 indent1 = indent1 or indent  
 limit = limit or 72  
 local here = 1-#indent1  
 return indent1..str:gsub("(%s+)()(%S+)()",  
 function(sp, st, word, fi)  
 if fi-here \> limit then  
 here = st - #indent  
 return "\n"..indent..word  
 end  
 end)  
end  

and when printing the text:

wrap(message[i].Question,50)  

[import]uid: 50459 topic_id: 14093 reply_id: 52391[/import]

Glad to hear you got it sorted :slight_smile: [import]uid: 84637 topic_id: 14093 reply_id: 52399[/import]

For the record, Danny, I still think it stinks I can’t use \n to create multi-line text objects.

There I said it, and I feel better :slight_smile: [import]uid: 5317 topic_id: 14093 reply_id: 52435[/import]

nothing has been resolved…

message[i].Question = "From what movie is the following movie quote:\nDo you like dags?"  
   
local Question = display.newText( message[i].Question, 65, 50, "Century Gothic", 30 )  

returns this
[text]From what movie is the following movie quote: Do you like dags?[/text]

Seems like there is a bug in newText… [import]uid: 50154 topic_id: 14093 reply_id: 52509[/import]

Nor “\n”, nor “\r”, nor combination of the two work on device, but “\n” works in (Windows) simulator. Can we have “\n” supported on device, Ansca? [import]uid: 52103 topic_id: 14093 reply_id: 54043[/import]

I will pass a request for this onto the team [import]uid: 84637 topic_id: 14093 reply_id: 54055[/import]

+1 for the support of \n.

That would make dealing with text SO much easier

Thanks! [import]uid: 63276 topic_id: 14093 reply_id: 92370[/import]

This has been supported for a while now

You have to pass a width and height parameter to display.newText for it to work [import]uid: 84637 topic_id: 14093 reply_id: 92422[/import]

*bonk*, yes, this is an old thread. =)

thanks for the update. You just saved me a considerable amount of time.

Regards,
Scott D Brooks
[import]uid: 63276 topic_id: 14093 reply_id: 92423[/import]

No problem :slight_smile: [import]uid: 84637 topic_id: 14093 reply_id: 92425[/import]

Hey Danny,

One last question…

When I’m setting a width and height on my display.newText(), there’s no way to center that text is there…??

My use case is that I’m creating a reusable message box object with a title. That title must fit within the box width and the length of that title will change dynamically. For this title, I create the newText with a width and a height,specifying that the newText() object and position it to sit at the top of the box. If the text is larger than the space given, width wise, it wraps to the next line, which is great! For shorter strings, one line long, the text fits, but is left aligned. I want to center that text within the space. The “box” which the text lives in is centered, but the contents are not. They are left aligned.

Is there a way to center it? (nothing in the API on it)

Thanks!

Scott D Brooks

[import]uid: 63276 topic_id: 14093 reply_id: 92505[/import]