Page 1 of 1
<SOLVED> How can I display Rich Text in a TextLabel
Posted: Friday 7th May 2021 1:47pm
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.
Re: How can I display Rich Text in a TextLabel
Posted: Friday 7th May 2021 2:06pm
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>
Re: How can I display Rich Text in a TextLabel
Posted: Friday 7th May 2021 2:56pm
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
Re: How can I display Rich Text in a TextLabel
Posted: Friday 7th May 2021 2:59pm
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.
' 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 &= "<p> <br> <a> <font><br>"
sText &= "<b> <i> <s> <sub> <sup> <small> <tt> <u><br>"
sText &= "<h1> <h2> <h3> <h4> <h5> <h6><p>"
sText &= "The <font> 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
Re: How can I display Rich Text in a TextLabel
Posted: Friday 7th May 2021 3:29pm
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 (46.41 KiB) Viewed 5393 times
...which is a bit of unexpected excitement for a Friday afternoon!
Anyway, no time to worry about it today.
Re: How can I display Rich Text in a TextLabel
Posted: Friday 7th May 2021 5:12pm
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.
Re: <SOLVED> How can I display Rich Text in a TextLabel
Posted: Saturday 8th May 2021 4:30am
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.
Re: <SOLVED> How can I display Rich Text in a TextLabel
Posted: Saturday 8th May 2021 5:29am
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
Re: <SOLVED> How can I display Rich Text in a TextLabel
Posted: Saturday 8th May 2021 8:13am
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.