Search code examples
livecodelivecoding

Highlight Before and after the characters --


I want to highlight the strings before and after '--'. Example good -- bad here i want to highlight good and bad. when ever the -- is comes then before and after the strings are become highlight. Is it possible.


Solution

  • Let's assume you have the following text in a field "mytext":

    This text comes before -- this text comes after.
    

    LiveCode (as most applications) does not allow discontinuous selections, so the 'select' command only works on continuous runs of text.

    select word 1 to 3 of fld "mytext"
    

    But you can simulate selection highlighting by setting the backgroundColor property of separate text runs:

    put wordOffset("--",fld "mytext") into tWordIndex
    set the backgroundColor of word 1 to tWordIndex - 1 of fld "mytext" to the hiliteColor
    set the backgroundColor of word tWordIndex + 1 to -1 of fld "mytext" to the hiliteColor
    

    Of course you can use any valid text chunk expression in the two 'set' statements, depending on what part of the text preceding and following the " -- " you want to "highlight".

    To clear the backgroundColor from the field do this:

    set the backgroundColor of char 1 to -1 of fld "mytext" to empty