<SOLVED> How can I display Rich Text in a TextLabel
Posted
#1
(In Topic #657)
Regular

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)
- ' Gambas module file
- 'Module1
- 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]
- Text$ = Text$ & Edit$[x]
- 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.
You eat an elephant one small bite at a time.
Posted
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:-
Try removing any spaces within the HTML, e.g. < / b > must be </b>
Posted
Regular

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.
You eat an elephant one small bite at a time.
Posted
Guru

<IMG src="https://www.cogier.com/gambas/HTML.png">
</IMG>Code (gambas)
- ' Gambas class file
- ''FMain
- .H = 500
- .W = 600
- .Arrangement = Arrange.Vertical
- .Padding = 5
- With TextLabel1
- .text = GetText()
- 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
Posted
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;
…which is a bit of unexpected excitement for a Friday afternoon!
Anyway, no time to worry about it today.
Posted
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
Posted
Regular

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.
You eat an elephant one small bite at a time.
Posted
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
Posted
Regular

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)
- TextLabel1.RichText = "<b> Welcome </b>"
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)
- GridView1[0,0].RichText = "<b> Welcome </b>"
- GridView1[1,0].RichText = "<b><i>Another text example</i></b><br>One more line</br>"
“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.
You eat an elephant one small bite at a time.
1 guest and 0 members have just viewed this.



