Color

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
p4tr1ck
Posts: 36
Joined: Mon Jun 14, 2010 4:58 pm

Color

Post by p4tr1ck » Wed Oct 27, 2010 11:32 pm

How would I go about changing the color of particular text in a field? (not all of it)

I looked through the dictionary and it all seemed to reference objects, not chunks.
Thanks.

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4163
Joined: Sun Jan 07, 2007 9:12 pm

Re: Color

Post by bn » Thu Oct 28, 2010 1:07 am

patrick,

would the textcolor do?

Code: Select all

set the textcolor of word 5 to 9 of field "myField" to red
regards
Bernd

p4tr1ck
Posts: 36
Joined: Mon Jun 14, 2010 4:58 pm

Re: Color

Post by p4tr1ck » Thu Oct 28, 2010 2:38 am

Thanks, sorry for asking such a simple question. :)
I can't believe I didn't find that on the dictionary... :roll:

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10043
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Color

Post by FourthWorld » Thu Oct 28, 2010 3:22 am

p4tr1ck wrote:I can't believe I didn't find that on the dictionary... :roll:
Happens to all of us. We think it should be something really cryptic, and then we find out it's almost English.

Almost. Then later we start looking for things expecting them to be really simple, and find some of them to be somewhat cryptic. ;)
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

p4tr1ck
Posts: 36
Joined: Mon Jun 14, 2010 4:58 pm

Re: Color

Post by p4tr1ck » Thu Oct 28, 2010 10:08 pm

I'm having some trouble using the textcolor property. How would I, say, set the selected text of a field to the color red?
It seems that every time I use your code exactly it works fine, but when I try it breaks :?

Code: Select all

set the textcolor of the selectedText to red
I've tried selectedChunk as well. What am I doing wrong?
Thanks for your help. :)

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4163
Joined: Sun Jan 07, 2007 9:12 pm

Re: Color

Post by bn » Thu Oct 28, 2010 10:20 pm

patrick,
the selectedText gives you the actual text.
You are looking for a chunk expression like "char 30 to 50 of field MyField"
The selectedChunk is what you are looking for.

Code: Select all

on mouseUp
   put the selectedChunk of field 1 into tChunk
   set the textColor of tChunk to red
end mouseUp
(tested)
If you are unshure what a particular keyword does I often code
put the selectedChunk of field 1
which puts the value of the expression into the message box.

regards
Bernd

p4tr1ck
Posts: 36
Joined: Mon Jun 14, 2010 4:58 pm

Re: Color

Post by p4tr1ck » Thu Oct 28, 2010 10:30 pm

Thanks, I'm a little rusty on text and chunks, I'll have to read its user guide part again.
Thanks for all the help. :)

p4tr1ck
Posts: 36
Joined: Mon Jun 14, 2010 4:58 pm

Re: Color

Post by p4tr1ck » Thu Oct 28, 2010 10:33 pm

Sorry for the double post, but I'm getting some errors.

Code: Select all

on mouseUp
   local tChunk
   put the selectedChunk of field "userCode" into tChunk
   set the textColor of tChunk to red
end mouseUp
That brings up this error:
"button "Button": execution error at line 4 (Chunk: error in object expression), char 22"
I am using the correct field name...
Thanks,
Patrick

Regulae
Posts: 136
Joined: Tue Oct 20, 2009 6:05 am

Re: Color

Post by Regulae » Fri Oct 29, 2010 2:48 am

Hi Patrick,

Something to remember when working with “selectedText” and “selectedChunk” is that clicking the mouse clears the selection, i.e. if I have a button containing the script:

Code: Select all

on mouseUp
   local tChunk
   put the selectedChunk of field "userCode" into tChunk
   set the textColor of tChunk to red
end mouseUp
... as in your example, it runs into the problem that, if I select some text, then click the button, I will by clicking the button clear the selection. If instead you have the script:

Code: Select all

on mouseEnter
   local tChunk
   if the selection is not empty then
      put the selectedChunk into tChunk
      set the textColor of tChunk to red
   end if
end mouseEnter
... select some text, then move the mouse over the button, the selectedChunk will be made red. Three things are worth noticing about the above script:
1. By using “mouseEnter”, the text we selected is still selected when the script runs, changing the textColor as desired.
2. “if the selection is not empty”... we need to put this condition in simply because, as this button uses “mouseEnter” if we happen to move the mouse into it without having selected some text, we would get an error. You could try “commenting out” the condition to see the problem, thus:

Code: Select all

on mouseEnter
   local tChunk
   --   if the selection is not empty then
   put the selectedChunk into tChunk
   set the textColor of tChunk to red
   --end if
end mouseEnter
... and move the mouse into the button, without having selected any text. The problem would be the line:

Code: Select all

set the textColor of tChunk to red
... because, with no text selected, the selectedChunk is empty, thus tChunk is empty, and so Rev is now trying to set the textColor of an unspecified chunk of text to red, which generates an error. This was the problem with your script as well- clicking the button cleared the selection so nothing was selected when the script actually ran.
3. If, for example, there is only one field on the card (hence it is “field 1”) and it contains the single sentence “This is an example”- if you then select the first word:
the selectedText ... is “This”
the selectedChunk ... is “char 1 to 4 of field 1”
so, because the selectedChunk already specifies the field involved (field 1) it is not really necessary, as I understand it, to specify the field explicitly as in:

Code: Select all

the selectedChunk of fld "usercode"
I should also mention that a simplified script would also work:

Code: Select all

on mouseEnter
   if the selection is not empty then
      set the textColor of the selectedChunk to red
   end if
end mouseEnter
I hope this is of some help.

Regards,
Michael

p4tr1ck
Posts: 36
Joined: Mon Jun 14, 2010 4:58 pm

Re: Color

Post by p4tr1ck » Fri Oct 29, 2010 3:21 am

It never occurred to me that the selection cleared...Thanks a ton! :)

Regulae
Posts: 136
Joined: Tue Oct 20, 2009 6:05 am

Re: Color

Post by Regulae » Fri Oct 29, 2010 4:07 am

Hi Patrick,

Glad to hear things are working out. Something I should clarify:

Code: Select all

the selectedChunk of field “usercode”
... is perfectly valid, just more specific than:

Code: Select all

the selectedChunk
... on its own. If you have several fields on a card, if you select text in any of them, the selectedChunk will be of the form “char(a) to (b) of field (whichever numbered field text was selected in)”, whereas:

Code: Select all

the selectedChunk of field “usercode”
... will be empty, unless the current selection is specifically in the field “usercode”. In some cases, you may want a script to operate on the selectedChunk regardless of which field the user is working on, so just “the selectedChunk” is very useful. On the other hand, there may well be design situations where as the developer you want things only applicable to a specific field. To be honest, I hadn’t realised you could specify the selectedChunk in this way, but I can well see how it could be very useful. Once again, I learn from my esteemed German colleague.

Regards,
Michael

Post Reply