How can I get a string with a line break to be centered in a uicontrol?
18 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Josh G.
el 2 de Dic. de 2020
Comentada: Josh G.
el 2 de Dic. de 2020
I'm trying to center text with a line break in it in a figure, but the second line is never centered correctly. Here's an example:
f = figure('Position',[300,300,600,600]);
uicontrol('Parent',f,...
'Style','Text',...
'HorizontalAlignment','center',...
'Position',[20,300,560,280],...
'String',sprintf('This is a string that should be centered. It''s more apparent with longer lines.\nThis line definitely isn''t centered.'));
The second line is indented, but not centered. This is in R2020b if it matters. How can I get it to center correctly?
2 comentarios
Rik
el 2 de Dic. de 2020
Sometimes assigning a cellstr works better. I don't remember if this is such a case.
Respuesta aceptada
Fangjun Jiang
el 2 de Dic. de 2020
I don't know why, but try and error and then got lucky.
%%
f = figure('Position',[300,300,600,600]);
uicontrol('Parent',f,...
'Style','Text',...
'HorizontalAlignment','center',...
'Position',[20,300,560,80],'backgroundcolor','r',...
'String',["This is a string that should be centered. It''s more apparent with longer lines.","Short line centered."]);
Comparing to
%%
f = figure('Position',[300,300,600,600]);
uicontrol('Parent',f,...
'Style','Text',...
'HorizontalAlignment','center',...
'Position',[20,300,560,80],'backgroundcolor','r',...
'String',['This is a string that should be centered. It''s more apparent with longer lines.',newline,'Short line not centered.']);
0 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Characters and Strings en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!