<SOLVED> How can I display Rich Text in a TextLabel

Post your Gambas programming questions here.
Post Reply
Doctor Watson
Posts: 84
Joined: Wednesday 22nd August 2018 7:55am

<SOLVED> How can I display Rich Text in a TextLabel

Post by Doctor Watson »

Hi.

I was planning to use a TextLabel control to display information in Rich Text format.
When I put a TextLabel on a form and enter some Rich Text using the little text editor, it works fine. But I want to import some external text (a user manual) to be displayed, stored in a seperate module or from an external file (.txt or .svg),
I did find a topic from Seany about ‘Rich Text Editor’ where Cogier suggests some solutions, but I cant’ get them to work. Error messages galore. Something to do with gb.qt4, gb.qt5 and such. I wanted to activate those but my Gambas version (3.14.3) didn’t like them.
This is what I’ve tried with a Form1 with a TextLabel1 on it and a Module1 :
' Gambas class file

'Form1

Public Sub Form_Open()
  With TextLabel1
    .width = 300
    .height = 200
  
    .text = Module1.data()
    
    '.RichText = Module1.data() '????
  End With
End
' Gambas module file

'Module1
Public Text$ As String
Public Edit$ As New String[10]

Public Sub data() As String
Dim x As Integer
  
Edit$[0] = "< p > < b > Welcome < / b > < / p >"
Edit$[1] = "< p > There should be a way to display this Rich Text.</p>"
Edit$[2] = "< p > But the .RichText option doesn't work any longer.< / p >"
Edit$[3] = "< p > < b > Anyone know a way to do this? < / b > < / p >"

Text$ = Edit$[0]
For x = 1 To 3
Text$ = Text$ & Edit$[x]
Next

Return Text$
End
The text gets returned, but TextLabel refuses to execute the HTML markup.
Last edited by Doctor Watson on Friday 7th May 2021 2:57pm, edited 1 time in total.
Old african saying:
You eat an elephant one small bite at a time.
User avatar
stevedee
Posts: 518
Joined: Monday 20th March 2017 6:06pm

Re: How can I display Rich Text in a TextLabel

Post by stevedee »

Doctor Watson wrote: Friday 7th May 2021 1:47pm Hi.

I was planning to use a TextLabel control to display information in Rich Text format...
Not sure which bit you are having a problem with, but this works:-
Public Sub Form_Open()
  With TextLabel1
    .width = 300
    .height = 200
   
    .text = "<p> <b> Welcome </b> </p> <p> There should be a way to display this Rich Text.</p>"
     
    '.RichText = Module1.data() '????
  End With
End
Try removing any spaces within the HTML, e.g. < / b > must be </b>
Doctor Watson
Posts: 84
Joined: Wednesday 22nd August 2018 7:55am

Re: How can I display Rich Text in a TextLabel

Post by Doctor Watson »

Oh dear !
I just didn’t notice and consider those spaces.
And I just found out how they got there in the first place.
Copy <p><b> Welcome </b></p> , without putting it between “ “ to a blank line in the Gambas editor and see what happens.
I did and added the Edit$[0] = " and then the ending “ afterwards … :?
Thanks
Old african saying:
You eat an elephant one small bite at a time.
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: How can I display Rich Text in a TextLabel

Post by cogier »

You can't use '.RichText'. Just use HTML. I also agree with Steve, you don't need all those spaces. A <p> does not require a </p>. Try this code that adds the Gambas help page that you can see here.

Image
' Gambas class file

''FMain

Public Sub Form_Open()

  With Me
    .H = 500
    .W = 600
    .Arrangement = Arrange.Vertical
    .Padding = 5
  End With

  With TextLabel1
    .Expand = True
    .text = GetText()
  End With

End

Public Sub GetText() As String

  Dim sText As String

  sText = "<p><b>Welcome</b><p>There should be a way to display this Rich Text.<p>But the .RichText option doesn't work any longer.<p><b>Anyone know a way to do this?</b><p><h1><u><i>This is the way to do it!</h1></u></i>"
  sText &= "<p>You don't use<b> '.RichText' </b> just use HTML."
  sText &= "<h3>Rich Text Syntax</h3><p>"
  sText &= "A rich text is a string using a subset of the HTML format.<br>"
  sText &= "The following HMTL markups are allowed:<p>"
  sText &= "&lt;p&gt; &lt;br&gt; &lt;a&gt; &lt;font&gt;<br>"
  sText &= "&lt;b&gt; &lt;i&gt; &lt;s&gt; &lt;sub&gt; &lt;sup&gt; &lt;small&gt; &lt;tt&gt; &lt;u&gt;<br>"
  sText &= "&lt;h1&gt; &lt;h2&gt; &lt;h3&gt; &lt;h4&gt; &lt;h5&gt; &lt;h6&gt;<p>"
  sText &= "The &lt;font&gt; markup understands the following attributes: face, color, and size.<p>"
  sText &= "The size attribute can be:<br>"
  sText &= "A value between 1 and 7 for an absolute font size.<p>"
  sText &= "-1 for a smaller font size.<br>"
  sText &= "+1 for a larger font size.<br>"
  sText &= "Any HTML entity can be used.<br>"

  Return sText

End
User avatar
stevedee
Posts: 518
Joined: Monday 20th March 2017 6:06pm

Re: How can I display Rich Text in a TextLabel

Post by stevedee »

Doctor Watson wrote: Friday 7th May 2021 2:56pm ...Copy <p><b> Welcome </b></p> , without putting it between “ “ to a blank line in the Gambas editor and see what happens...
I know, it annoys the hell out of me when the editor does that!

BTW your code without spaces crashes the Gambas Interpreter;
GambasIntError.png
GambasIntError.png (46.41 KiB) Viewed 5176 times
...which is a bit of unexpected excitement for a Friday afternoon!
Anyway, no time to worry about it today.
User avatar
BruceSteers
Posts: 1521
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: How can I display Rich Text in a TextLabel

Post by BruceSteers »

Doctor Watson wrote: Friday 7th May 2021 2:56pm Oh dear !
I just didn’t notice and consider those spaces.
And I just found out how they got there in the first place.
Copy <p><b> Welcome </b></p> , without putting it between “ “ to a blank line in the Gambas editor and see what happens.
I did and added the Edit$[0] = " and then the ending “ afterwards … :?
Thanks
You can press Ctrl-Shift-V to paste and the "paste Special" options pop up. (in the "Advanced" menu just below paste in the menu)
then select "paste as string" :)

or remember to put in quotes first :)
it's a bit annoying when it first gets ya ;) but when the text is not in a string the IDE assumes it's gambas code so it formats it.
If at first you don't succeed , try doing something differently.
BruceS
Doctor Watson
Posts: 84
Joined: Wednesday 22nd August 2018 7:55am

Re: <SOLVED> How can I display Rich Text in a TextLabel

Post by Doctor Watson »

OK guys. Thanks for the advice.
There’s only one drawback with the supported HTML markup : you can’t center the title line in a TextLabel and have the rest of the text left-alligned.
Apparently the <center> markup has become obsolete or is no longer supported.
But there are ways around that.
Old african saying:
You eat an elephant one small bite at a time.
User avatar
BruceSteers
Posts: 1521
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: <SOLVED> How can I display Rich Text in a TextLabel

Post by BruceSteers »

Doctor Watson wrote: Saturday 8th May 2021 4:30am OK guys. Thanks for the advice.
There’s only one drawback with the supported HTML markup : you can’t center the title line in a TextLabel and have the rest of the text left-alligned.
Apparently the <center> markup has become obsolete or is no longer supported.
But there are ways around that.
Yeah RichText is very limited
Probably better to use a webview to get full html support
If at first you don't succeed , try doing something differently.
BruceS
Doctor Watson
Posts: 84
Joined: Wednesday 22nd August 2018 7:55am

Re: <SOLVED> How can I display Rich Text in a TextLabel

Post by Doctor Watson »

Bruce : First time I hear about a Webview control. I’ll have a look at it.
Cogier : It seems that only TextLabel refuses .RichText, although when you’re typing in the editor and you arrive at TextLabel1.ric , the usual popup indicates the possibility of RichText and will accept it. Only – as we found out – when you try to run something like:
TextLabel1.RichText = "<b> Welcome </b>"
You get the error “unknown symbol ….”
BUT I just found out that with a GridView the .RichText option DOES work and for every single cell as well. Like this:
GridView1[0,0].RichText = "<b> Welcome </b>"
GridView1[1,0].RichText = "<b><i>Another text example</i></b><br>One more line</br>"
Strange, as the documentation on Rich Text Syntax states :
“Rich text is mainly used by TextLabel, GridView cells, the Paint.DrawRichText and other rich text methods and properties.”
Using a GridView with one single column, Grid set to False and manipulating the height of each row and the properties of each cell, I can make it look like an elaborate textbox indeed. Seems to be what I was looking for.
Old african saying:
You eat an elephant one small bite at a time.
Post Reply