Setting the text style of certain words in a 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
LouisThompson
Posts: 4
Joined: Mon Oct 03, 2016 8:11 pm

Setting the text style of certain words in a field

Post by LouisThompson » Thu Jul 13, 2017 1:03 pm

Hi,

I've played around, searched, but not found anything...
(Sounds like the first line of a Country & Western song :-)

Anyway, I have a field which contains a Python Script.

I'd like to set the text style of certain words...

eg
def, return, import, for : bold, blue
numbers should all be red
comments should be green

I'm happy to parse the field to find the relevant sections and words, but how do I apply the text style atrributes.

Thanks

Louis

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

Re: Setting the text style of certain words in a field

Post by bogs » Thu Jul 13, 2017 1:16 pm

I believe the htmlText property in the dictionary may be what you want. You can set many aspects of text styling with it, including colors. Alternately, textStyle / textColor could be used. Just type "text" in the dictionary, there is a lot there :)
Image

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9582
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Setting the text style of certain words in a field

Post by dunbarx » Thu Jul 13, 2017 1:50 pm

What Bogs said.

The HTML text will allow you to attach style tags to text within, say, a variable. You need to research this as per his suggestion, since this is a powerful tool indeed.

But just to get back to basics, put some numbers and ordinary words in a field. Put this in a button script somewhere:

Code: Select all

on mouseUp
   repeat with y = 1 to the number of words of fld 1
      if word y of fld 1 is a number then set the textColor of word y of fld 1 to "yellow"
      else set the textColor of word y of fld 1 to "blue"
   end repeat
end mouseUp
This will require forethought to parse text , as you say, into several different categories. The "offset" function might come in handy...

Craig Newman

MaxV
Posts: 1579
Joined: Tue May 28, 2013 2:20 pm
Location: Italy
Contact:

Re: Setting the text style of certain words in a field

Post by MaxV » Mon Jul 17, 2017 12:46 pm

Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

marksmithhfx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 931
Joined: Thu Nov 13, 2008 6:48 am
Location: London, UK

Re: Setting the text style of certain words in a field

Post by marksmithhfx » Sun May 02, 2021 3:39 pm

dunbarx wrote:
Thu Jul 13, 2017 1:50 pm
But just to get back to basics, put some numbers and ordinary words in a field. Put this in a button script somewhere:

Code: Select all

on mouseUp
   repeat with y = 1 to the number of words of fld 1
      if word y of fld 1 is a number then set the textColor of word y of fld 1 to "red"
      else set the textColor of word y of fld 1 to "blue"
   end repeat
end mouseUp
Hi Craig, one could quibble, but when I put the following text into a field and ran the code above...
How do you style some text in a field? One, two, 3, 4, who are we for? 100 times 100 is 10000.
I expected all the numbers to turn red. Only 100 and 10000 did. Note 10000 is also followed by punctuation, as is 3 and 4, but 3 and 4 were ignored. I am sure some complicated logic but a numbers a number. If it captures 10000 why not 3 and 4?

Mark
macOS 12.6.5 (Monterey), Xcode 14.2, LC 10.0.0, iOS 15.6.1
Targets: Mac, iOS

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9802
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Setting the text style of certain words in a field

Post by FourthWorld » Sun May 02, 2021 4:00 pm

You'll want to use the trueWord chunk type rather than word. TrueWord is both Unicode-aware and uses natural-language rules to account for punctuation and other details, where word (for compatibility with older dialects) only uses simple space boundaries.

I've been looking for some Python examples I might use to run comparative benchmarks. This task may be a good fit. How would you do this in Python?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”