Removing a particular text style from a field

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

kaveh1000
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 508
Joined: Sun Dec 18, 2011 7:23 pm
Location: London
Contact:

Removing a particular text style from a field

Post by kaveh1000 » Sat Feb 08, 2020 12:24 pm

I have a field with around 2000 lines of text. it is styled using bold, italic, backgroundcolor, etc.

I have one small piece of the text styled with "box", by using

Code: Select all

set the style of char x to y of fld "name" to "box"
What I need to do is to remove the box quickly, e.g. when user clicks outside the field or when the cursor moves, but keep all other styles. So looking for something like

Code: Select all

set the style of char 1 to -1 of fld "name" to not "box"
What is the right way to do this?
Kaveh

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9359
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Removing a particular text style from a field

Post by richmond62 » Sat Feb 08, 2020 2:26 pm

Beats me how you are setting the style to box in the first place:
-
boxx.png

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Removing a particular text style from a field

Post by [-hh] » Sat Feb 08, 2020 2:43 pm

Code: Select all

-- ON
set the textstyle of char x to y of fld "name" to "box"
-- OFF 
set the textstyle of char x to y of fld "name" to empty
To preserve other textstyles when removing, you could also put the effective textstyle of <chunk> into <variable>. Then filter items of <variable> without "box" and then set the textstyle of <chunk> to <variable>.

[Edit. Corrected "empty" to empty (without quotes).]
Last edited by [-hh] on Sat Feb 08, 2020 2:50 pm, edited 1 time in total.
shiftLock happens

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

Re: Removing a particular text style from a field

Post by Klaus » Sat Feb 08, 2020 2:45 pm

1. You mean TEXTSTYLE!
2. Set this back to "plain".
:D

kaveh1000
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 508
Joined: Sun Dec 18, 2011 7:23 pm
Location: London
Contact:

Re: Removing a particular text style from a field

Post by kaveh1000 » Sat Feb 08, 2020 5:09 pm

Sorry guys, I meant the TextStyle, as Klaus rightly says!

Hermann and Klaus — when I want to reset, I do not know the offset of the characters. But I know I want to remove the "box" style from all chars. I want to keep other text styles as they are. So looking to remove all box styles but not touch anything else. is this possible?
Kaveh

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

Re: Removing a particular text style from a field

Post by dunbarx » Sat Feb 08, 2020 5:13 pm

Beats me how you are setting the style to box in the first place:
Why, Richmond? Box is as good as any other native textStyle.

Craig

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

Re: Removing a particular text style from a field

Post by dunbarx » Sat Feb 08, 2020 5:16 pm

Kaveh.

What almost everyone said.

Know that there is a difference between setting the textStyle of a chunk to "empty" as opposed to "plain". Both sort of "look" the same if you do not do much to the rest of your stack. From the dictionary:
----------
Setting the textStyle to "plain" turns off all styles. (Setting the textStyle to "plain" plus one or more additional styles may result in anomalous behavior.)

Setting the textStyle of an object to empty allows the textStyle of the object's owner to show through. Use the effective keyword to find out what style is used for the object, even if its own textStyle is empty. Similarly, use the effective keyword to find out what style is used for a chunk of text, even if the chunk's textStyle is empty.
----------

Craig

kaveh1000
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 508
Joined: Sun Dec 18, 2011 7:23 pm
Location: London
Contact:

Re: Removing a particular text style from a field

Post by kaveh1000 » Sat Feb 08, 2020 5:45 pm

Thanks Craig.

By the way Richmond's comment was just the confusion I caused by writing "style" rather than "textstyle"!

OK, I never used "effective" and thanks for the explanation. I have some more thinking to do on that.

Let me know if I have still missed something but my problem is:
  • I have several text styles, e.g. bold, italic, already interspersed in the text. I do not want to remove those
  • I do not know which chars have the box textstyle. so just want to remove any box style but leave other styles as they are
I have actually now solved my problem by using backgroundcolor rather than bold, italic but the fundamental question remains as to how I can "wipe off" any box styling but leave everything else as it is.

If I am being dumb, it is not the first time. ;-)
Kaveh

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Removing a particular text style from a field

Post by [-hh] » Sat Feb 08, 2020 5:54 pm

Now it is clear (for me) what you really want.
The following could be done also with other tags as <b> or <i> instead of <box>.

Code: Select all

-- removes all box-textstyle (and only that) from a field
set htmltext of fld X to replaceText(the htmltext of fld X,"<\/?box>","")
shiftLock happens

kaveh1000
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 508
Joined: Sun Dec 18, 2011 7:23 pm
Location: London
Contact:

Re: Removing a particular text style from a field

Post by kaveh1000 » Sat Feb 08, 2020 6:30 pm

Fantastic! Perfect in this case. I have been doing some things in html and others directly in the field, but this is a very easy and fast. Thanks all. I have learnt a lot from this thread. :-)
Kaveh

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

Re: Removing a particular text style from a field

Post by FourthWorld » Sat Feb 08, 2020 7:27 pm

kaveh1000 wrote:
Sat Feb 08, 2020 12:24 pm

Code: Select all

set the style of char 1 to -1 of fld "name" to not "box"
Very close. In v5.5 (or thereabouts) textStyles became manipulatable independently of one another as Booleans, using array syntax.

So to turn off the box style while leaving all other styles in place, you can use:

Code: Select all

set the textStyle["box"] of char 1 to -1 of fld "name" to false
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

kaveh1000
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 508
Joined: Sun Dec 18, 2011 7:23 pm
Location: London
Contact:

Re: Removing a particular text style from a field

Post by kaveh1000 » Sat Feb 08, 2020 7:37 pm

Oh, man. I've hit the jackpot today! Another great reply. :-) this is what I was looking for, but of course Hermann's solution has its merits.

So Richard, is this the alternative way of controlling text styles to using htmlText? I have been doing all my manipulation using htmlText, with all the headaches, like dealing with split lines and nested colors and styles. I think that the better way to go is to use Textstyles, but because of my fear of multidimensional arrays I have stayed away from it. This might be the moment. Would you say in general it is better to use Textstyles than htmlText?
Kaveh

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Removing a particular text style from a field

Post by [-hh] » Sat Feb 08, 2020 10:37 pm

These are the results of tests with severals of my use cases:

If you use textstyle only then certainly the array method is faster than using the htmltext (or the ordinary styledText).

But if you do also other text styling that textstyle can't do (and have already the htmltext/styledText in a variable) or if you do several styling actions in 'one stroke' then switching also to the array method will slow down.
Of course depending on text length and complexity...
shiftLock happens

kaveh1000
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 508
Joined: Sun Dec 18, 2011 7:23 pm
Location: London
Contact:

Re: Removing a particular text style from a field

Post by kaveh1000 » Sat Feb 08, 2020 10:47 pm

I was just about to post that I was thinking of styledText when I referred to the arrays. I think, Hermann, that you commented a long time ago on a problem I had, after Trevor suggested styledText.

I don't quite understand what would be best for me, from your comments. In my case I want to have a range of styles available, as well as backgroundcolor, box around paras, etc. So quite a lot of formatting. Up to now I have mimicked html by first putting unique markers around formatted text, e.g. ••red text••, then changing these to the html tags at the end. This is so that when I search and replace the half formatted text, the html code does not confuse the search.

The kind of problems I face are:
  • Having to tag each para separately
  • Having to ensure that tags are correctly nested
Putting performance aside, I am wondering whether using styledText will be easier and more elegant. I have to say I am a bit frightened by the details:

https://livecode.fandom.com/wiki/StyledText

I don't quite understand "runs" for instance. Any advice or examples to refer to would be appreciated.
Kaveh

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

Re: Removing a particular text style from a field

Post by dunbarx » Sat Feb 08, 2020 11:38 pm

By the way Richmond's comment was just the confusion I caused by writing "style" rather than "textstyle"!
Aha. I glossed over that, and should have known to look deeper into any of Richmonds posts. :wink:

Craig

Post Reply

Return to “Talking LiveCode”