Help: Why does assigning StyledText to a TextArea so slow?

Blog post? PLEEEEEEEASE …

2 Likes

For sure @Norman_Palardy. I’ve hit a painful wall here today.

Appending styleruns does seem much faster but I don’t want to add content to the text area (which is what happens when you append style runs) - I just want to colourise the text that’s already there.

TIC would be several

I see that as a PLUS … :wink:

there’s just a lot of mundane grunt work to get to the point you can do much useful with TIC

And then you have to decide on how you want to represent the “contents”
Do you want an array of lines each of which has some sections that each of attributes like font size color etc ? That might be useful for something like the styled text @GarryPettet is dealing with.
Or a giant block of raw text ?
Or something else ?

In all seriousness, I just don’t have the time to reinvent the wheel for what was to be a free project for the community.

It’s a shame because I had a nice idea for a project that required the user to write into markdown into a TextArea that would colourise Markdown. I figured that since I had already done the hard job of writing a fast Markdown parser (that can literally pass 10,000s characters per keystroke) that Xojo would be able to change the bold status and colour of characters in a TextArea to keep up.

Alas I was wrong. So disappointing.

1 Like

If you are macOS only and have the MBS plugins you might be able to use NSTextFieldMBS / NSTextMBS instead.

I am macOS only. I don’t have an active plugin license but would be willing to get one. Is it much faster for styling? My only worry is that I didn’t see a speed improvement manipulating the StyledText with the declares @Markus_Winter suggested.

.selTextBold, .selTextItalic, selTextColor…

Edit: Sorry forgot to say, to it directly in the textarea rather than trying to edit a styledText object and then apply it to the TextArea.

1 Like

I must admit I haven’t used it myself and it was only a suggestion.

However, I just performed a quick test putting a NSTextFieldControlMBS onto a window and running some code to populate and style the field. The result seemed to be pretty instant.

It might be worthwhile downloading the MBS demo version and having a play.

Dim s As String
Dim i As Int32
Dim nsTextObj As NSTextMBS

nsTextObj = NSTextFieldControlMBS1.View.currentEditor
nsTextObj.isRichText = True

s = “1234567890”

For i = 1 To 17
s = s + s
Next

nsTextObj.text = s
nsTextObj.setTextColorForRange(New NSColorMBS(1.0, 0, 0), 0, Len(s))

Holy smokes Batman - I think we’ve cracked it!!

Using a combination of @Markus_Winter suggestion of fast editing using declares and @Sam_Rowlands suggestion of using .SelelectionTextColor, .SelectionBold, etc seems to be working.

I don’t know why adjusting via the TextArea’s properties is faster with the declares compared to re-assigning the StyledText but I will continue to investigate. So far it’s handling a 30,000 character document.

1 Like

RTFData is know to be slow…

Is it the same bug ?

Experimenting a little more, it looks like you don’t need the extension methods with the declares either. Awesome.

1 Like

We don’t have a StyledText ↔ NSAttributedString.
I could make that someday of course. But for now you can maybe just use the edit functions via TextArea1.NSTextViewMBS.textStorage directly.

1 Like

e.g.

TextArea1.Text = “Hello World”

Dim n As NSTextStorageMBS = TextArea1.NSTextViewMBS.textStorage

Dim r As New NSRangeMBS(0, n.Text.Len)
n.addAttribute(n.NSForegroundColorAttributeName, NSColorMBS.redColor, r)

with using NSTextStorageMBS and NSColorMBS class.

1 Like