How can I change the font size of MsgBox?

48 visualizzazioni (ultimi 30 giorni)
Muhendisleksi
Muhendisleksi il 16 Apr 2018
Commentato: Adam Danz il 17 Lug 2025
if f==0 || f <0
Message = sprintf('Serbestlik derecesi %d\n DENGELEME YOK!!! ',f);
h = msgbox(Message,...
'Dikkat', 'warn');
end

Risposte (2)

Anibal Siguenza Torres
Anibal Siguenza Torres il 20 Ago 2018
Modificato: Anibal Siguenza Torres il 20 Ago 2018
The easiest is to use the TeX format so the subset of markup supported by matlab can be used. To activate them you have to create an structure and send it as parameter as follows:
CreateStruct.Interpreter = 'tex';
CreateStruct.WindowStyle = 'modal';
msgbox('\fontsize{18} Now is big =)', CreateStruct)

Adam Danz
Adam Danz il 26 Giu 2018
Modificato: Adam Danz il 20 Ago 2018
Here's a function I wrote that does exactly this. Pass the handle of the msgbox() and the fontsize to the function.
If you don't want a function, here's the basics:
First get the handle to the text within the message box, then change the font size. Here's a demo.
mh = msgbox('I can barely read this.', 'test'); %create msgbox
th = findall(mh, 'Type', 'Text'); %get handle to text within msgbox
th.FontSize = 14; %Change the font size
If you increase the font size too much, you'll need to expand the message box to fit the extent of the text.
deltaWidth = sum(th.Extent([1,3]))-mh.Position(3) + th.Extent(1);
deltaHeight = sum(th.Extent([2,4]))-mh.Position(4) + 10;
mh.Position([3,4]) = mh.Position([3,4]) + [deltaWidth, deltaHeight];
Lastly, allow the user to resize the window if needed:
mh.Resize = 'on';
  2 Commenti
Julia
Julia il 17 Lug 2025
Modificato: Julia il 17 Lug 2025
This is an old thread, but I still had the same issue these days. Thank you, Adam, for your suggestion! Based on this, I have slightly extended the code so that also the 'Okay' button changes size and that the 'Okay' button is recentered in the message box and the message box on the screen.
This is the function (usage example underneath):
function [] = ResizeTextMessagebox(mb, fontsize)
% Determine relative font size change
defaultFontSize = get(0,'FactoryUicontrolFontSize');
diffSize = 1.5*(fontsize - defaultFontSize);
% Find text object
th = findall(mb, 'Type', 'Text'); % Get handle to text within msgbox
th.FontSize = fontsize; % Change the font size
% If you increase the font size too much, you'll need to expand the message box to fit the extent of the text.
deltaWidth = sum(th.Extent([1,3]))-mb.Position(3) + th.Extent(1);
deltaHeight = sum(th.Extent([2,4]))-mb.Position(4) + diffSize;
mb.Position([3,4]) = mb.Position([3,4]) + [deltaWidth, deltaHeight];
% Recenter the box left to right
mb.Position(1) = mb.Position(1) - deltaWidth/2;
% Identify the 'Okay' Button
button = findall(mb,'Type','UIControl');
% Adjust the font size of the 'Okay' Button accordingly
button.FontSize = fontsize;
% Resize the 'Okay' Button accordingly
mb.Position(4) = mb.Position(4) + diffSize;
% Increase the height of the 'Okay' button accordingly
button.Position(4) = button.Position(4) + min(diffSize,5);
button.Position(3) = button.Position(3) + min(diffSize,5);
% Reposition the 'Okay' Button
button.Position(1) = (mb.Position(3)-button.Position(3))/2;
% Move text slightly up
th.Position(2) = th.Position(2) + diffSize;
% Lastly, allow the user to resize the window if needed:
mb.Resize = 'on';
end
Usage example:
fontsize = 16;
mb = msgbox('Here is a bit of text for testing.');
ResizeTextMessagebox(mb, fontsize);
% Optional if you want to block execution until user clicks 'Okay':
uiwait(mb);
Adam Danz
Adam Danz il 17 Lug 2025
Thanks for sharing that @Julia! Nice improvement.

Accedi per commentare.

Categorie

Scopri di più su Characters and Strings in Help Center e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by