Losing Styled text after replaceall

Hi all,

I have a Styled text area that have multiple paragraphs, each paragraph has it’s own style like: font, size and color.

When i do the code to replace some letters, after replace is done, all the paragraphs text are loosing it’s styles and becomes in one style as the first paragraph style!.

I use the following code:

Dim s As String s = TextArea.text s = Replaceall (s,"A","a") TextArea.text = s

Any help please!

Did you try the replacements on the StyledText.Text property instead?

Thank you Ulrich,

Yes i tried StyledText.Text property, but after the replacement done, all the text area has no style at all, a Black color text with no style and the default font size.

The TextArea.Text property is not styled. To get and set styled text with a TextArea you need to use the StyledText property.
This is covered in the documentation: http://documentation.xojo.com/api/deprecated/textarea.html

Another attempt then:
Copy the replacement text to a new Styledtext, append each style run from the original to the copy and then assign this to the StyledText property of the TextArea.

The reason why you loose text is because you programmed it to do that. The offending line is:

s = TextArea.text // No style copied in s // then later: TextArea.text = s // No style to set back !

Because s do not hold any style.

You have to change the whole Replace code.

Thank you all Tim, Ulrich and Emile.

I will be very appreciate if you can provide a sample code.

The following code will work if:

  • you use a mac
  • you copy/paste the paragraphs from Notes with color/font/style into the TextArea1
    so this may not be what you want.

Dim s As String s = TextArea1.StyledText.RTFData s = ReplaceAll (s, "A", "a") TextArea1.StyledText.RTFData = s

Are you setting the Style within Xojo?
Are you bringing the style/color/font from another app?

Thank you very much Alberto for your help. It works now just fine for all letters except for “Carriage Return”, I tried to replace all extra carriage returns in the text but no luck!, it does not work. I tried to replace: Chr(13), Chr(10), EndofLine.Windows, EndofLine.Macintosh and EndofLine.Unix… but neither of them works. I don’t know why all letters are replaceable but “Carriage Return” does not !!.

I’m using Mac OSX, and the text is generated on the Mac.

Very appreciate your concern.

Try replaceLineEndings().

Mujahed, can you post your code? You said “replace all extra carriage returns” but English is not my first language, so I can interpret that as “have 2 or more” (next to each other) and want to replace all those to only 1 line end, but I’m not sure if this is what you want.

EDIT: if you are using RTFData, then you may not have “Carriage Return” but code for end of Paragraph \\par

Yes Alberto, I have 2 or more" (next to each other) and want to replace all those to only 1 line end after each paragraph. The above code you provided works very good for all letters, But it is not replace Carriage return. It seems it does not recognize it!!

As i said, the text is generated on mac, i used a normal Mac OS carriage return.

Regards

Mujahed, I edited my post, maybe you didn’t see the edit. I think you will need to use \par\par to \par as the ReplaceAll.

Note: some of my tests there is a space between the first \par and the second. So I did:

s = ReplaceAll (s, "\\par ", "\\par") s = ReplaceAll (s, "\\par\\par", "\\par")
you need to change it to take care of 3 carriage returns and maybe other cases.

You can do something like:

Dim s As String s = TextArea1.StyledText.RTFData Break
and see the Text and Binary contents of ‘s’ to find if your string has End of Lines or \par

[quote=420531:@Alberto De Poo]The following code will work

Dim s As String s = TextArea1.StyledText.RTFData s = ReplaceAll (s, "A", "a") TextArea1.StyledText.RTFData = s[/quote]

This only works in VERY restricted circumstances. Try replacing all “a” with “b” or all “a” with “something” and see what happens.

Thank you so much Alberto for your help, I’m really very appreciate your concern. It works fine now.

Thank you also Tim for your help.

Have a great Day and a happy new Year :slight_smile: