highlight text

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
micro04
Posts: 11
Joined: Sat Mar 30, 2019 12:48 am

highlight text

Post by micro04 » Sat Mar 30, 2019 3:03 pm

I need to highlight individual words in a field where the word placement is not always the same. The field is populated with a text file that can be changed so the word placement is not always the same.
I have tried :

Code: Select all

set the textColor of fld "Field1"  to "green"
which sets all the text to green, I just want a certain words in the field.

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

Re: highlight text

Post by Klaus » Sat Mar 30, 2019 3:29 pm

Hi micro04,

welcome to the forum!

You already said it, since LC is very like the english language, you can actually use all kind of "chunks". That means any part of text like: character, word, trueword, line

Code: Select all

...
set the textColor of word 2 fld "Field1" to "green"
...
set the textColor of word 22 of line 2 fld "Field1" to "red"
...
set the textColor of char 1 of word 22 of line 2 fld "Field1" to "red"
## char = abbreviation for character
...
set the textColor of word 2 to 4 of fld "Field1" to "green"
...
etc.


Best

Klaus

micro04
Posts: 11
Joined: Sat Mar 30, 2019 12:48 am

Re: highlight text

Post by micro04 » Sat Mar 30, 2019 4:37 pm

So how do I highlight the word "test" if I do not know what the location is?

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

Re: highlight text

Post by Klaus » Sat Mar 30, 2019 4:51 pm

Hi micro04,

there are many ways in LC! :D

I would probably use "wordoffset" first and then use the result to colorize the found word:

Code: Select all

...
put wordoffset("test",fld "Field1") into tFoundWordNumber

## ALWAYS check everything!
## 0 means the string is NOT in that field!
if tFoundWordNumber <> 0 then
  set the textColor of word tFoundWordNumber fld "Field1" to "green"
end if
...
This will however only find the first occurrence of "test" in that field.
Read up "wordoffset" (and "offset" and "lineoffset" etc...) in the dicitonary.


Best

Klaus

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: highlight text

Post by bogs » Sat Mar 30, 2019 4:57 pm

Hi micro04,
You can use 'find' (in the dictionary) to find words in the field. I am assuming that although the position of the word may change, the words themselves are in a list of some kind that can be referenced, so if you were going for a one color hilited solution it might look something like (untested) -

Code: Select all

// your list of words in a variable or file or whatever, which we'll call 'yourWords'....
repeat with x=1 to the number of yourWords
  find yourword x of field "theFieldYourTextIsIn"
  set the textColor of word (yourWord x) of field "theFieldYourTextIsIn" to "green"
end repeat


Make sure you read up on 'find ' and 'repeat' in the dictionary for more accurate examples.

** I see Klaus got back to you, but I'll leave this here as well.
Image

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”