当我进行放大或缩小时,如何在此自动缩放中添加文本?
放大或缩小时,FontUnits
个选项,normalized
和pixels
都会不重新缩放。
figure(1);
text(0.5,0.5,'test',...
'FontUnits','Normalized',...
'FontSize',0.25,...
'HorizontalAlignment','center',...
'Color',color...
);
figure(2);
set(gcf,'Position',[935 428 672 504])
text(50,50,'test',...
'FontUnits','pixels',...
'FontSize',100,...
'HorizontalAlignment','center',...
'Color',color...
);
axis([0 100 0 100])
此功能,包括对zoomCallBack功能的进一步修改:
function drawtext(p,s,f,color)
% get axes size
ax = axis;
% add some text
ax0=ax(4)-ax(3);
txt = text(p(1),p(2),s,...
'FontSize',f,...
'HorizontalAlignment','center',...
'Color',color);
h = zoom; % get handle to zoom utility
set(h,'ActionPostCallback',@zoomCallBack);
set(h,'Enable','on');
% everytime you zoom in, this function is executed
function zoomCallBack(obj, evd)
% Since i expect to zoom in ax(4)-ax(3) gets smaller, so fontsize
% gets bigger.
ax = axis(evd.Axes); % get axis size
% get all texts in figure
htxt = findobj(gcf,'Type','text');
axi=ax(4)-ax(3);
for i=1:length(htxt)
% change font size accordingly
set(htxt(i),'FontSize',str2num(get(htxt(i),'Tag'))*ax0/axi);
end
end
end
这是一个有效的解决方案,但是,非常棘手并且有时会失败。欢迎任何更好的解决方案。
答案 0 :(得分:2)
text 对象具有属性 FontUnits 。如果设置为标准化,则文本将随轴重新缩放。
text(0.4, 0.5, 'test', 'FontUnits', 'Normalized', 'FontSize', 0.2);
然后尝试更改轴尺寸。