- Undocumented Matlab - https://undocumentedmatlab.com -
Multi-line tooltips
Posted By Yair Altman On November 2, 2011 | 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.
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'))
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');
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:
>> myData = struct('a',pi, 'b',-4, 'very_long_field_name','short txt')
myData =
a: 3.14159265358979
b: -4
very_long_field_name: 'short txt'
>> myDataStr = evalc('disp(myData)');
>> set(hControl, 'TooltipString', myDataStr);
set(hControl, 'TooltipString', ['' myDataStr ''])
<br/>
‘, we still get a badly-formatted tooltip, because HTML ignores all white spaces:
>> myDataStr2 = strrep(myDataStr, sprintf('\n'), '
');
>> set(hControl, 'TooltipString', ['' myDataStr2 ''])
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])
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 ''])
Even more powerful customizations can be achieved using CSS rather than pure HTML. This will be discussed a future article.
Categories: GUI, Low risk of breaking in future versions, Stock Matlab function, Undocumented feature
Article printed from Undocumented Matlab: https://undocumentedmatlab.com
URL to article: https://undocumentedmatlab.com/articles/multi-line-tooltips
URLs in this post:
[1] here: http://undocumentedmatlab.com/blog/spicing-up-matlab-uicontrol-tooltips/
[2] here: http://undocumentedmatlab.com/blog/additional-uicontrol-tooltip-hacks/
[3] here: http://undocumentedmatlab.com/blog/inactive-control-tooltips-event-chaining/
[4] supports HTML rendering: http://undocumentedmatlab.com/blog/html-support-in-matlab-uicomponents/
[5] automatically supports HTML: http://download.oracle.com/javase/tutorial/uiswing/components/html.html
[6] createTable utility: http://www.mathworks.com/matlabcentral/fileexchange/14225-java-based-data-table
[7] Multi-line uitable column headers : https://undocumentedmatlab.com/articles/multi-line-uitable-column-headers
[8] Spicing up Matlab uicontrol tooltips : https://undocumentedmatlab.com/articles/spicing-up-matlab-uicontrol-tooltips
[9] Inactive Control Tooltips & Event Chaining : https://undocumentedmatlab.com/articles/inactive-control-tooltips-event-chaining
[10] Explicit multi-threading in Matlab part 2 : https://undocumentedmatlab.com/articles/explicit-multi-threading-in-matlab-part2
[11] Multi-column (grid) legend : https://undocumentedmatlab.com/articles/multi-column-grid-legend
[12] Explicit multi-threading in Matlab part 4 : https://undocumentedmatlab.com/articles/explicit-multi-threading-in-matlab-part4
[13] : https://www.mathworks.com/help/matlab/creating_plots/create-custom-data-tips.html
[14] : https://undocumentedmatlab.com/?s=datacursormode
Click here to print.
Copyright © Yair Altman - Undocumented Matlab. All rights reserved.
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.