Page 1 of 1

Changing Colors Of Text In Field

Posted: Wed Jan 05, 2011 8:33 pm
by warrenk
I have a text field where the color of my text is blue. I want the ability to click anywhere in the text field (including between words) and when I type any new text...it is black. It seems like when I click in the middle of a word, I inherit the color of the word (which is currently blue).

Any suggestions?

Thanks for any help!
Warren

Re: Changing Colors Of Text In Field

Posted: Wed Jan 05, 2011 9:57 pm
by jmburnod
Hi Warren,

No problem for me with rev 4.0 and livecode 4.50 with OSX

Jean-Marc

Re: Changing Colors Of Text In Field

Posted: Thu Jan 20, 2011 5:05 am
by jsburnett
Try This:

on keyDown
set the textColor of (the selectedChunk) to black
pass keyDown
end keyDown

Re: Changing Colors Of Text In Field

Posted: Thu Jan 20, 2011 11:03 am
by Regulae
Hi all,

One scenario, admittedly not raised in the original post, is the case of selecting some text then typing new text over it. In case this, I found:

Code: Select all

on keyDown
   if the selection is not empty then
      delete the selectedChunk
   end if
   set the textColor of (the selectedChunk) to black
   pass keyDown
end keyDown
... is needed, otherwise the first character of the newly inserted text is blue, followed by black characters. This points to a subtle difference between the selection, and the selectedChunk- if there is no text selected (hence the selection is empty) yet the insertion point is in the field, the Dictionary entry for selectedChunk mentions:
If no text is selected but the text insertion point is in a field, the startChar is the character after the insertion point, and the endChar is the character before the insertion point. In this case, the endChar is one less than the startChar.
... which is handy to remember. All of this was prompted by jsburnett’s neat solution, which I was very keen to try. I’m running Rev Studio 4.0.0 under Windows XP, by the way.

Regards,
Michael

Re: Changing Colors Of Text In Field

Posted: Thu Jan 20, 2011 3:16 pm
by jsburnett
Michael,
While all that may be true....
if you put the script I posted in the field, it works with the first char changing colors.
Why, despite the documentation you quoted, ?
JSB

Re: Changing Colors Of Text In Field

Posted: Thu Jan 20, 2011 3:18 pm
by jsburnett
I spoke too soon.
I see your point.
When selecting text, the first char is not changed color.
Thanks!

Re: Changing Colors Of Text In Field

Posted: Thu Jan 20, 2011 5:46 pm
by Regulae
Hi JSB,

It was because your method was so neat and handy that I was prompted to play with it, during which I stumbled on the need for the minor “tweak”. Then I found myself wondering about the difference between the selection and the selectedChunk, so I thought I’d include a little discussion of what I learned for the benefit of future readers of this thread. I’ve added your solution to my collection of useful code snippets. It’s just the sort of thing I’m likely to need. Many thanks.

Regards,
Michael

Re: Changing Colors Of Text In Field

Posted: Sat Jan 22, 2011 8:24 pm
by jsburnett
Michael,
Thank you for your nice words.
I've become more interested in reexploring the syntax again.
I believe if the selection is empty, it will return that.
So, I tried the following and it seems to behave the way your script does too.

on keyDown theKey
delete the selection --if the selection is empty it deletes nothing
set the textColor of the selection to black
pass keyDown
end keyDown