Page 1 of 2
Removing a particular text style from a field
Posted: Sat Feb 08, 2020 12:24 pm
by kaveh1000
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?
Re: Removing a particular text style from a field
Posted: Sat Feb 08, 2020 2:26 pm
by richmond62
Beats me how you are setting the style to box in the first place:
-
Re: Removing a particular text style from a field
Posted: Sat Feb 08, 2020 2:43 pm
by [-hh]
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).]
Re: Removing a particular text style from a field
Posted: Sat Feb 08, 2020 2:45 pm
by Klaus
1. You mean TEXTSTYLE!
2. Set this back to "plain".

Re: Removing a particular text style from a field
Posted: Sat Feb 08, 2020 5:09 pm
by kaveh1000
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?
Re: Removing a particular text style from a field
Posted: Sat Feb 08, 2020 5:13 pm
by dunbarx
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
Re: Removing a particular text style from a field
Posted: Sat Feb 08, 2020 5:16 pm
by dunbarx
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
Re: Removing a particular text style from a field
Posted: Sat Feb 08, 2020 5:45 pm
by kaveh1000
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.

Re: Removing a particular text style from a field
Posted: Sat Feb 08, 2020 5:54 pm
by [-hh]
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>","")
Re: Removing a particular text style from a field
Posted: Sat Feb 08, 2020 6:30 pm
by kaveh1000
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.

Re: Removing a particular text style from a field
Posted: Sat Feb 08, 2020 7:27 pm
by FourthWorld
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
Re: Removing a particular text style from a field
Posted: Sat Feb 08, 2020 7:37 pm
by kaveh1000
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?
Re: Removing a particular text style from a field
Posted: Sat Feb 08, 2020 10:37 pm
by [-hh]
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...
Re: Removing a particular text style from a field
Posted: Sat Feb 08, 2020 10:47 pm
by kaveh1000
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.
Re: Removing a particular text style from a field
Posted: Sat Feb 08, 2020 11:38 pm
by dunbarx
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.
Craig