Changing Colors Of Text In Field

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
warrenk
Posts: 110
Joined: Sun Sep 21, 2008 5:39 am

Changing Colors Of Text In Field

Post by warrenk » Wed Jan 05, 2011 8:33 pm

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

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2718
Joined: Sat Dec 22, 2007 5:35 pm
Location: Genève
Contact:

Re: Changing Colors Of Text In Field

Post by jmburnod » Wed Jan 05, 2011 9:57 pm

Hi Warren,

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

Jean-Marc
https://alternatic.ch

jsburnett
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 110
Joined: Fri Mar 09, 2007 9:47 pm

Re: Changing Colors Of Text In Field

Post by jsburnett » Thu Jan 20, 2011 5:05 am

Try This:

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

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

Re: Changing Colors Of Text In Field

Post by Regulae » Thu Jan 20, 2011 11:03 am

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

jsburnett
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 110
Joined: Fri Mar 09, 2007 9:47 pm

Re: Changing Colors Of Text In Field

Post by jsburnett » Thu Jan 20, 2011 3:16 pm

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

jsburnett
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 110
Joined: Fri Mar 09, 2007 9:47 pm

Re: Changing Colors Of Text In Field

Post by jsburnett » Thu Jan 20, 2011 3:18 pm

I spoke too soon.
I see your point.
When selecting text, the first char is not changed color.
Thanks!

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

Re: Changing Colors Of Text In Field

Post by Regulae » Thu Jan 20, 2011 5:46 pm

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

jsburnett
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 110
Joined: Fri Mar 09, 2007 9:47 pm

Re: Changing Colors Of Text In Field

Post by jsburnett » Sat Jan 22, 2011 8:24 pm

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

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”