2012-05-05 4 views
2

Matlab GUI로 작업 할 때 텍스트 편집 상자에 "힌트"를 표시 할 수있는 방법이 있습니까? 즉, 사용자가 타이핑을 시작하면 사라지는 텍스트입니까? Android에서 비슷한 기능을 사용했지만 다른 GUI에 익숙하지 않아서이 기능이 얼마나 널리 퍼져 있는지 잘 모르겠습니다.Matlab 텍스트 상자 편집 - 힌트를 표시 하시겠습니까?

답변

3

이것은 Matlab에서 가능하지만 사용자 정의 MouseClickCallback을 정의해야합니다.이 파일은 Yair Altman이 findjobj을 사용하여 액세스 할 수 있습니다 (링크를 사용하여 Matlab File Exchange에서 다운로드하여 Matlab 경로의 어딘가에 저장하십시오).

저는이 아이디어가 마음에 듭니다.이 모든 것을 편리하게 처리하는 함수를 작성했습니다. 편집 상자를 클릭하면 사라지는 이탤릭 도움말 텍스트가 회색으로 표시됩니다.

enter image description here

function setInitialHelp(hEditbox,helpText) 
%SETINITIALHELP adds a help text to edit boxes that disappears when the box is clicked 
% 
% SYNOPSIS: setInitialHelp(hEditbox,helpText) 
% 
% INPUT hEditbox: handle to edit box. The parent figure cannot be docked, the edit box cannot be part of a panel. 
%  helpText: string that should initially appear as help. Optional. If empty, current string is considered the help. 
% 
% SEE ALSO uicontrol, findjobj 
% 
% EXAMPLE 
%   fh = figure; 
%   % define uicontrol. Set foregroundColor, fontAngle, before 
%   % calling setInitialHelp 
%   hEditbox = uicontrol('style','edit','parent',fh,... 
%    'units','normalized','position',[0.3 0.45 0.4 0.15],... 
%    'foregroundColor','r'); 
%   setInitialHelp(hEditbox,'click here to edit') 
% 

% check input 
if nargin < 1 || ~ishandle(hEditbox) || ~strcmp(get(hEditbox,'style'),'edit') 
    error('please supply a valid edit box handle to setInitialHelp') 
end 

if nargin < 2 || isempty(helpText) 
    helpText = get(hEditbox,'string'); 
end 

% try to get java handle 
jEditbox = findjobj(hEditbox,'nomenu'); 
if isempty(jEditbox) 
    error('unable to find java handle. Figure may be docked or edit box may part of panel') 
end 

% get current settings for everything we'll change 
color = get(hEditbox,'foregroundColor'); 
fontAngle = get(hEditbox,'fontangle'); 

% define new settings (can be made optional input in the future) 
newColor = [0.5 0.5 0.5]; 
newAngle = 'italic'; 

% set the help text in the new style 
set(hEditbox,'string',helpText,'foregroundColor',newColor,'fontAngle',newAngle) 

% add the mouse-click callback 
set(jEditbox,'MouseClickedCallback',@(u,v)clearBox()); 

% define the callback "clearBox" as nested function for convenience 
    function clearBox 
     %CLEARBOX clears the current edit box if it contains help text 

     currentText = get(hEditbox,'string'); 
     currentColor = get(hEditbox,'foregroundColor'); 

     if strcmp(currentText,helpText) && all(currentColor == newColor) 
      % delete text, reset color/angle 
      set(hEditbox,'string','','foregroundColor',color,'fontAngle',fontAngle) 
     else 
      % this is not help text anymore - don't do anything 
     end 

    end % nested function 

end % main fcn 
+0

최고, 감사 톤! – camdroid

관련 문제