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

Post

Posted
Rating:
#1 (In Topic #657)
Regular
Doctor Watson is in the usergroup ‘Regular’
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 :

Code (gambas)

  1. ' Gambas class file
  2.  
  3. 'Form1
  4.  
  5. Public Sub Form_Open()
  6.   With TextLabel1
  7.     .width = 300
  8.     .height = 200
  9.  
  10.     .text = Module1.data()
  11.    
  12.     '.RichText = Module1.data() '????

Code (gambas)

  1. ' Gambas module file
  2.  
  3. 'Module1
  4. Public Edit$ As New String[10]
  5.  
  6.  
  7. Edit$[0] = "< p > < b > Welcome < / b > < / p >"
  8. Edit$[1] = "< p > There should be a way to display this Rich Text.</p>"
  9. Edit$[2] = "< p > But the .RichText option doesn't work any longer.< / p >"
  10. Edit$[3] = "< p > < b > Anyone know a way to do this? < / b > < / p >"
  11.  
  12. Text$ = Edit$[0]
  13. For x = 1 To 3
  14. Text$ = Text$ & Edit$[x]
  15.  
  16. Return Text$

The text gets returned, but TextLabel refuses to execute the HTML markup.

 Old african saying:
You eat an elephant one small bite at a time.
Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Regular
stevedee is in the usergroup ‘Regular’

Doctor Watson said

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:-

Code (gambas)

  1. Public Sub Form_Open()
  2.   With TextLabel1
  3.     .width = 300
  4.     .height = 200
  5.    
  6.     .text = "<p> <b> Welcome </b> </p> <p> There should be a way to display this Rich Text.</p>"
  7.      
  8.     '.RichText = Module1.data() '????

Try removing any spaces within the HTML, e.g. < / b > must be </b>
Online now: No Back to the top

Post

Posted
Rating:
#3
Regular
Doctor Watson is in the usergroup ‘Regular’
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.
Online now: No Back to the top

Post

Posted
Rating:
#4
Avatar
Guru
cogier is in the usergroup ‘Guru’
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.

<IMG src="https://www.cogier.com/gambas/HTML.png"> </IMG>

Code (gambas)

  1. ' Gambas class file
  2.  
  3. ''FMain
  4.  
  5. Public Sub Form_Open()
  6.  
  7.   With Me
  8.     .H = 500
  9.     .W = 600
  10.     .Arrangement = Arrange.Vertical
  11.     .Padding = 5
  12.  
  13.   With TextLabel1
  14.     .Expand = True
  15.     .text = GetText()
  16.  
  17.  
  18. Public Sub GetText() As String
  19.  
  20.   Dim sText As String
  21.  
  22.   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>"
  23.   sText &= "<p>You don't use<b> '.RichText' </b> just use HTML."
  24.   sText &= "<h3>Rich Text Syntax</h3><p>"
  25.   sText &= "A rich text is a string using a subset of the HTML format.<br>"
  26.   sText &= "The following HMTL markups are allowed:<p>"
  27.   sText &= "&lt;p&gt; &lt;br&gt; &lt;a&gt; &lt;font&gt;<br>"
  28.   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>"
  29.   sText &= "&lt;h1&gt; &lt;h2&gt; &lt;h3&gt; &lt;h4&gt; &lt;h5&gt; &lt;h6&gt;<p>"
  30.   sText &= "The &lt;font&gt; markup understands the following attributes: face, color, and size.<p>"
  31.   sText &= "The size attribute can be:<br>"
  32.   sText &= "A value between 1 and 7 for an absolute font size.<p>"
  33.   sText &= "-1 for a smaller font size.<br>"
  34.   sText &= "+1 for a larger font size.<br>"
  35.   sText &= "Any HTML entity can be used.<br>"
  36.  
  37.   Return sText
  38.  
Online now: No Back to the top

Post

Posted
Rating:
#5
Avatar
Regular
stevedee is in the usergroup ‘Regular’

Doctor Watson said

…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;
Image

(Click to enlarge)


…which is a bit of unexpected excitement for a Friday afternoon!
Anyway, no time to worry about it today.
Online now: No Back to the top

Post

Posted
Rating:
#6
Guru
BruceSteers is in the usergroup ‘Guru’

Doctor Watson said

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.
Online now: No Back to the top

Post

Posted
Rating:
#7
Regular
Doctor Watson is in the usergroup ‘Regular’
 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.
Online now: No Back to the top

Post

Posted
Rating:
#8
Guru
BruceSteers is in the usergroup ‘Guru’

Doctor Watson said

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
Online now: No Back to the top

Post

Posted
Rating:
#9
Regular
Doctor Watson is in the usergroup ‘Regular’
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:

Code (gambas)

  1. 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:

Code (gambas)

  1. GridView1[0,0].RichText = "<b> Welcome </b>"
  2. 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.
Online now: No Back to the top
1 guest and 0 members have just viewed this.