StyledText for in Paragraph text!

Ok, I use the provided XOJO code to change the Style of a text in a TextArea:

[code]Dim text As String // text to be displayed in TextArea
Dim st, ln As Integer // start and length values of a paragraph
//define four paragraphs in Text
text = "This is the text that we are going to save " _

  • “into our file from the TextArea.” + EndOfLine _
  • “Isn’t that interesting?” + EndOfLine _
  • “Man, I sure do love using Xojo to take care of my projects.”

TextArea1.StyledText.Text = text // four paragraphs in Text
TextArea1.StyledText.Bold(5, 2) = True
TextArea1.StyledText.TextColor(5, 2) = &cFF0000 // bold and red
TextArea1.StyledText.Size(7, 10) = 16
TextArea1.StyledText.Underline(12, 4) = True // 16 pt underline
TextArea1.StyledText.Size(100, 4) = 18
TextArea1.StyledText.TextColor(100, 4) = &cFF00FF // 18 pt and Magenta
TextArea1.StyledText.Font(0, Len(text) ) = “Comic Sans MS”

// center align second paragraph
TextArea1.StyledText.ParagraphAlignment(1) = Paragraph.AlignCenter
// set this paragraph in Helvetica, 18 pt bold, red
// first get the start and length values for this paragraph…
st = TextArea1.StyledText.Paragraph(1).StartPos
ln = TextArea1.StyledText.Paragraph(1).Length + 1

// next apply attributes…
TextArea1.StyledText.Bold(st, ln) = True
TextArea1.StyledText.Font(st, ln) = “Helvetica”
TextArea1.StyledText.Size(st, ln) = 18
TextArea1.StyledText.TextColor(st, ln) = &cFF0000[/code]


How can I change the Font and Style for a “Quoted” words or names inside the text! My TextArea have many “Quoted” texts and the Paragraph method (Dim st, ln As Integer ) does not work for this matter because it’s depends on know the start and the end point for each word!

So I need a code that only make my "Quoted" text to be Bold and has Other font.

Any help please!

My first (probably naive – I am not a pro) attempt would be something like this:

[code]Public Sub changeQuotes()
Dim s() As String = ReplaceLineEndings(TArea1.Text, EndOfLine).Split("")
Dim ubd As Integer = s.Ubound
Dim starts() As Integer
Dim lengths() As Integer
Dim curLength As Integer

// search and save quote starts and lengths
For i As Integer = 0 To ubd
If s(i) = ““” Then
lengths.Append(curLength)
Elseif s(i) = “„” Then
starts.Append(i)
curLength = 0
Else
curLength = curLength + 1
End If
If i = ubd And starts.Ubound > lengths.Ubound Then
lengths.Append(curLength)
End If
Next

// save current “state” of TextArea
Dim cStart As Integer = TArea1.SelStart
Dim cLength As Integer = TArea1.SelLength
Dim cPos As Integer = TArea1.ScrollPosition

// alter quotes
ubd = starts.Ubound
For i As Integer = 0 To ubd
TArea1.SelStart = starts(i) + 1
TArea1.SelLength = lengths(i)
TArea1.SelBold = True
Next

// restore TextArea “state”
TArea1.SelStart = cStart
TArea1.SelLength = cLength
TArea1.ScrollPosition = cPos
End Sub
[/code]

The method saves starts and lenghts of quotes to integer arrays and in the second step highlights and alters the concerned quotes. (I split that up to two steps/loops, because in the past when I used similar approaches it seemed much faster to save the positions first than doing searching and altering text in one step.)
Please note, that my method looks for German curly quotes; you might need to replace them. (If you don’t have curly quotes – I don’t know the English word for not-curly quotes – you will need to track if you are currently inside or outside a quote while looping the text.)

I’m sure there is much room for improvement, but maybe it can be a starting point …

EDIT: I never used the StyledText methods before; maybe this is faster than my approach, but I didn’t test it.

Okay, I did a quick testing, and using the StyledText method doubles up processing speed:

[code]Public Sub changeQuotes()
Dim s() As String = ReplaceLineEndings(TArea1.Text, EndOfLine).Split("")
Dim ubd As Integer = s.Ubound
Dim starts() As Integer
Dim lengths() As Integer
Dim curLength As Integer

For i As Integer = 0 To ubd
If s(i) = ““” Then
lengths.Append(curLength)
Elseif s(i) = “„” Then
starts.Append(i)
curLength = 0
Else
curLength = curLength + 1
End If
If i = ubd And starts.Ubound > lengths.Ubound Then
lengths.Append(curLength)
End If
Next

ubd = starts.Ubound
For i As Integer = 0 To ubd
TArea1.StyledText.Bold(starts(i) + 1, lengths(i)) = True
Next
End Sub
[/code]

Thank You Jens, i will test it tomorrow as I’m now way to home. Then tell you the result.

I downsized the first loop just a little bit more – sorry for the many posts, but from the first idea I always need to reconsider an algorithm quite a while until I get to a reasonable result. (Fortunately I don’t have to earn money with my coding…)

[code]Public Sub changeQuotes()
Dim s() As String = ReplaceLineEndings(TArea1.Text, EndOfLine).Split("")
Dim ubd As Integer = s.Ubound
Dim starts() As Integer
Dim lengths() As Integer
Dim curLength As Integer

For i As Integer = 0 To ubd
If s(i) = ““” Or (i = ubd And starts.Ubound > lengths.Ubound) Then
lengths.Append(curLength)
Elseif s(i) = “„” Then
starts.Append(i)
curLength = 0
Else
curLength = curLength + 1
End If
Next

ubd = starts.Ubound
For i As Integer = 0 To ubd
TArea1.StyledText.Bold(starts(i) + 1, lengths(i)) = True
Next
End Sub
[/code]

Thank you very much Jens, it works just fine :slight_smile: this is what I’m looking for. Very appreciate your help. Have a great day.

I’m glad I could help!

Adding the line “#Pragma DisableBackgroundTasks” at the top of the method gains a little more processing speed. (I tested with a 5000 words document and the method needed 8 milliseconds with a few quotes, which is fast enough for my scenarios.)