Adding Bold or Italic Text to Plain Text?

Is it possible to add bold or italic text to the plain text within the code?

Example: The quick brown fox jumped over the lazy dog. [import]uid: 162639 topic_id: 29744 reply_id: 329744[/import]

You can not change font ‘inline’ in the text objects, unfortunately. [import]uid: 147305 topic_id: 29744 reply_id: 119399[/import]

Can you give some advice, please? [import]uid: 162639 topic_id: 29744 reply_id: 119405[/import]

Hi there,

It’s possible in theory, but not easy. You’d basically need to parse a block of text out into separate text objects, using string search routines to locate certain “format codes” within your overall sentence. For example, maybe your string would look like this in code:

“The quick brown (B)fox(/B) jumped over the lazy dog.”

You would scan the string looking for the routine “(B)” (this could be something else, just an example I use here). Then you’d display one text object as "The quick brown " (trailing space included!). Following that, you’d display the part between the “bold tags” using your bold font… and using Corona to locate the X position of where the previous words ended, you’d position that bold object there. And so forth, scanning the entire sentence until the end.

It really depends on how crucial this is to your app. Is it easy? No, but if you really need it, a few functions could probably be written to handle this process, within a few hours if you’re comfortable with string searching in Lua. :slight_smile:

Brent

[import]uid: 9747 topic_id: 29744 reply_id: 119408[/import]

Brent’s suggestion is how I managed it in an app.

I had to use a monospace font though as it made life a lot easier knowing that each letter would have a set width. I used string.find, string.len and string.gsub all together to get it working, so it’s a little bit of a pain :slight_smile:

If there’s an effective way of doing this without a monospaced font, I’m all ears! [import]uid: 69826 topic_id: 29744 reply_id: 119424[/import]

@TandG,

Could you use the object.contentBounds call to retrieve the xMax (right side) coordinate of the previous bit of text, including the space, and then start the next “formatted” bit of text at that X position? That’s how I was envisioning it, but I obviously haven’t tested this theory.

http://docs.coronalabs.com/api/type/DisplayObject/contentBounds.html

Brent [import]uid: 9747 topic_id: 29744 reply_id: 119446[/import]

Let’s say that I want to make the words bowling and ball bold in the sentence. What suggestion would I have in there?

[lua]local lotsOfText12 = “This is how a bowling ball rolls.”

local lotsOfText12Object = util.wrappedText( lotsOfText12, 45.5, 14, “Times New Roman”, {0,0,0} )
scrollView:insert(lotsOfText12Object)
lotsOfText12Object.x = 24
lotsOfText12Object.y = math.floor(lotsOfText11Object.y + lotsOfText11Object.height)[/lua] [import]uid: 162639 topic_id: 29744 reply_id: 119463[/import]