Formatting text saved in an array

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
glenn9
Posts: 223
Joined: Wed Jan 15, 2020 10:45 pm
Location: Europe

Formatting text saved in an array

Post by glenn9 » Fri Nov 20, 2020 10:00 pm

Hi everyone,

I just wanted to check if there was a way of 'formatting' text saved in an array?

At present I've got text displayed in a field which is then saved as part of an array and the plain text saves as expected as an array.

However, if I apply say underline or bold to the text in the field, these changes are then lost when I retrieve the text from the array.

Is there a way of maintaining the formatting of the text on saving to an array.

Kind regards,

Glenn

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9647
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Formatting text saved in an array

Post by dunbarx » Fri Nov 20, 2020 10:24 pm

Sure, something like?:

Code: Select all

on mouseUp
   put the htmlText of fld 1 into myArray["x"]
   set the htmlText fld 2 to  myArray["x"]
end mouseUp
Arrays are just variables, after all. Read up on the "htmlText" in the dictionary.

Craig

glenn9
Posts: 223
Joined: Wed Jan 15, 2020 10:45 pm
Location: Europe

Re: Formatting text saved in an array

Post by glenn9 » Sat Nov 21, 2020 8:59 am

Thanks Craig,

Have now checked the dictionary but not sure I'm understanding it completely in relationship to what I want to achieve!

I guess if I wanted the whole field have a particular formatting, eg bold, applied to it then I'm assuming the htmlText is what's needed.

However, I'm wanting each array element to have its own individual formatting with parts bold and underlined etc.

What I was envisaging was to edit the formatting of the element in a field, and then to have the formatting saved when the element is saved in the array.

Not sure how to achieve this, or even if its possible?

Kind regards,

Glenn

SparkOut
Posts: 2852
Joined: Sun Sep 23, 2007 4:58 pm

Re: Formatting text saved in an array

Post by SparkOut » Sat Nov 21, 2020 11:21 am

The htmlText of the field is a property that includes all the (LC-version) styled html text of the field. If you set the text formatting of a field how you want, the put the htmlText of that field into a variable/custom property/write it out to a text file to save, then clear the field, you will be able to restore it by setting the htmlText of the field to the value you saved. This will include all the formatting you applied before.
For more granularity you can also delve into the "styledText" property and see if that is more applicable to your needs, but the htmlText is not limited to one style run only.

Klaus
Posts: 13821
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Formatting text saved in an array

Post by Klaus » Sat Nov 21, 2020 11:28 am

Hi Glenn,

in case you didn't know:
Every element of a field (char, word, item, line etc.) has a HTMLTEXT property, so you can do:

Code: Select all

...
put the htmltext of word 2 of fld 1 into tArray[1]
put the htmltext of line 1 of fld 1 into tArray[2]
## etc.
...
Maybe that helps?
Or do you want to format the contents of the array inside of the array?

Best

Klaus

glenn9
Posts: 223
Joined: Wed Jan 15, 2020 10:45 pm
Location: Europe

Re: Formatting text saved in an array

Post by glenn9 » Sat Nov 21, 2020 2:08 pm

Thanks Klaus, I hadn't appreciated this!

I guess I'm struggling to implement the suggestions from Craig and yourself as the text of my field is an array element, ie field 1 is the keyword, and field 2 is the element (ie the text that I'm trying to format).

I guess its like a dictionary app, the keyword is the header with the associated definition text as the associated element?

Not sure if this makes what I'm trying to achieve any clearer!

Regards,

Glenn

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9823
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Formatting text saved in an array

Post by FourthWorld » Sat Nov 21, 2020 5:40 pm

If field 1 contains the label and field 2 contains the value to be associated with that label...

Code: Select all

put the htmltext of fld 2 into tArray[fld 1]
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9647
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Formatting text saved in an array

Post by dunbarx » Sun Nov 22, 2020 12:55 am

There is nothing special about an array variable as opposed to an ordinary one, in terms of storing "htmlStyled" text.

If field 1 contains, say "ABC", where "A" is bold, "B" is underlined and "C" is italic, the htmlText of that string is:
<p><b>A</b><u>B</u><i>C</i></p>
the htmlText will add tags identifying each char. That new string is what is stored in your array. Nothing special, really. When you pull that string out, and set the htmlText of a field to it, that property "resets" all the styles, and you get your original back.

Craig

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9823
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Formatting text saved in an array

Post by FourthWorld » Sun Nov 22, 2020 3:20 am

And if you want to get obsessive about performance, arrays can contain arrays, so you could use the styledText of the field instead of the htmlText:

Code: Select all

put the styledText of fld 2 into tArray[fld 1]
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

glenn9
Posts: 223
Joined: Wed Jan 15, 2020 10:45 pm
Location: Europe

Re: Formatting text saved in an array

Post by glenn9 » Sun Nov 22, 2020 10:32 am

Thank you all for the helpful comments, I think I'm starting to get it now!

I now just need to experiment with the syntax to see how I fit it into my code.

Kind regards,

Glenn

glenn9
Posts: 223
Joined: Wed Jan 15, 2020 10:45 pm
Location: Europe

Re: Formatting text saved in an array

Post by glenn9 » Sun Nov 22, 2020 10:36 pm

Thanks again for all the tips - now able to successfully implement html editing - I think I was previously 'overthinking' the htmlText function and assuming it was more complicated than it actually was!

Am I correct in assuming that the htmlText function simply allow a field to recognise and react to html tags?

Regards,

Glenn

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9823
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Formatting text saved in an array

Post by FourthWorld » Sun Nov 22, 2020 10:59 pm

glenn9 wrote:
Sun Nov 22, 2020 10:36 pm
Am I correct in assuming that the htmlText function simply allow a field to recognise and react to html tags?
Almost.

Htmltext was added as a field property to provide a textual (as opposed to binary) format that retains the contents of the field with complete fidelity. StyledText was added more recently and is more efficient to work with, but sometimes the tagged-text form is useful so htmltext isn't going away.

Where attributes happen to match HTML conventions at the time that feature was added, LC can render that subset of HTML well.

Though htmltext bears some superficial semblance to HTML, they serve fundamentally different purposes. There are tags in each not supported by the other, font sizes are relative in HTML but fixed point sizes in htmltext, many paragraph-level features are implemented differently, etc. And LC has no facility for handling CSS at all, which is where most HTML styling takes place these days.

So if you happen to find HTML that works well imported into a field, consider yourself lucky. ;)

If you need to render modern HTML inside LC use the browser widget.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

glenn9
Posts: 223
Joined: Wed Jan 15, 2020 10:45 pm
Location: Europe

Re: Formatting text saved in an array

Post by glenn9 » Mon Nov 23, 2020 8:44 am

Richard,

Many thanks for the interesting and useful background.

Kind regards,

Glenn

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”