Posted By
Yair Altman
On
In
GUI,Low risk of breaking in future versions,Stock Matlab function,Undocumented feature |
5 Comments
I often use tooltips in my Matlab GUIs. They are a fantastically intuitive and unobtrusive visual helper for users to understand the GUI. In this blog, I have already posted several articles about how to tweak tooltip contents (here[1], here[2], and here[3]). Today I would like to expand on one of the aspects that were already covered, namely that of multi-line tooltips.
Basic multi-line tooltips
The basic multi-line tooltip consists of a simple string that includes the newline character. for example:
set(hControl, 'TooltipString', ['this is line 1' 10 'and this is line 2'])
Or better (platform independent):
set(hControl, 'TooltipString', sprintf('this is line 1\nand this is line 2'))
A simple multi-line tooltip string
As can be seen, this is very simple to set up. Unfortunately, the font cannot be customized. Which leads us to:
HTML-rendered multi-line tooltips
Tooltips, like most other Matlab controls, supports HTML rendering[4]. This is a natural corollary of the fact that Java Swing, on which all Matlab controls are based, automatically supports HTML[5]. All we need to do is to add ‘<HTML>‘ at the very beginning of our tooltip string, and then we can embed HTML tags within the rest of the string:
set(hControl, 'tooltipString', 'line #1 line#2');
Multi-line HTML'ed tooltip
And a more sophisticated example, used within my createTable utility[6] on the File Exchange:
A more complex multi-line HTML-based tooltip
Note that there is no need to close the final HTML tags in the tooltip string, although it’s not an error to do so (as I have done above).
The problem with formatted multi-line tooltip
Unfortunately, none of these two methods work when we wish to display formatted multi-line tooltips. For example, suppose that we wish to display struct data in a tooltip:
If we try to use HTML, the result looks even worse, because if the HTML-renderer detects a newline character embedded in the string, it simply uses the regular (non-HTML) renderer:
set(hControl, 'TooltipString', ['' myDataStr ''])
Failure to parse a string using HTML
Even if we fix this by replacing all line-separators with ‘<br/>‘, we still get a badly-formatted tooltip, because HTML ignores all white spaces:
(you can see that it’s HTML-formatted by the fact that the tooltip contents do not have internal margins like in the non-HTML tooltip above)
Fixed-width font multi-line tooltips
We now recall the HTML <pre> tag, which tells HTML not to ignore white-spaces. In most web browsers, it also defaults to using a fixed-width font. Unfortunately, this is not the case here:
set(hControl, 'TooltipString', ['
' myDataStr2])
Still not quite right...
Which brings us to the final customization of the day, namely explicitly forcing the tooltip to use a fixed-width font:
set(hControl, 'TooltipString', ['
' myDataStr2 ''])
Finally - a well-formatted multi-line tooltip
Even more powerful customizations can be achieved using CSS rather than pure HTML. This will be discussed a future article.
Related posts:
Multi-line uitable column headers [7] – Matlab uitables can present long column headers in multiple lines, for improved readability. ...
Spicing up Matlab uicontrol tooltips [8] – Matlab uicontrol tooltips can be spiced-up using HTML and CSS, including fonts, colors, tables and images...
Inactive Control Tooltips & Event Chaining [9] – Inactive Matlab uicontrols cannot normally display their tooltips. This article shows how to do this with a combination of undocumented Matlab and Java hacks....
#1 Pingback By Undocumented Profiler options part 2 | Undocumented Matlab On September 24, 2012 @ 03:24
[…] recall my related article last year, where I showed how to convert a proportional-font multi-line tooltip […]
#2 Comment By Simon On April 29, 2019 @ 17:25
Does anyone know if multi line tooltips are possible for Digraph plots?
This strategy doesn’t appear to work…
#3 Comment By Yair Altman On June 20, 2019 @ 14:28
@Simon – I assume that you mean the data-tip that is displayed next to the nodes in a digraph plot – this is not a tooltip but a data-tip (also called a datacursor): tooltips (which are the topic of this article) are Java components that are displayed next to Java GUI controls (buttons, checkboxes, editboxes etc.). In contrast, data-tips are non-Java graphic areas that are only displayed within graphic axes and use a completely different technology. Datacursor popups are not tooltips and they do not support HTML, although they do support Tex/Latex (like other graphics/axes text objects).
Basic customizations of the data-tip text is supported and [13]; I described [14] in other articles.
#4 Comment By Ozgu On June 18, 2019 @ 22:14
Hi Yair
Can we adjust the position of the TooltipString?
#5 Comment By Yair Altman On June 20, 2019 @ 14:09
@Ozgu – you cannot control tooltip position for Matlab controls. You can only do this with custom Java components in which you overload the getToolTipLocation() method.
5 Comments To "Multi-line tooltips"
#1 Pingback By Undocumented Profiler options part 2 | Undocumented Matlab On September 24, 2012 @ 03:24
[…] recall my related article last year, where I showed how to convert a proportional-font multi-line tooltip […]
#2 Comment By Simon On April 29, 2019 @ 17:25
Does anyone know if multi line tooltips are possible for Digraph plots?
This strategy doesn’t appear to work…
#3 Comment By Yair Altman On June 20, 2019 @ 14:28
@Simon – I assume that you mean the data-tip that is displayed next to the nodes in a digraph plot – this is not a tooltip but a data-tip (also called a datacursor): tooltips (which are the topic of this article) are Java components that are displayed next to Java GUI controls (buttons, checkboxes, editboxes etc.). In contrast, data-tips are non-Java graphic areas that are only displayed within graphic axes and use a completely different technology. Datacursor popups are not tooltips and they do not support HTML, although they do support Tex/Latex (like other graphics/axes text objects).
Basic customizations of the data-tip text is supported and [13]; I described [14] in other articles.
#4 Comment By Ozgu On June 18, 2019 @ 22:14
Hi Yair
Can we adjust the position of the TooltipString?
#5 Comment By Yair Altman On June 20, 2019 @ 14:09
@Ozgu – you cannot control tooltip position for Matlab controls. You can only do this with custom Java components in which you overload the getToolTipLocation() method.