Main Content

removeStyle

从 UI 组件中删除样式

自 R2019b 起

说明

示例

removeStyle(comp) 从指定的表、树、列表框或下拉 UI 组件中删除使用 uistyle 函数创建的所有样式。要确定哪些样式位于 comp 上并可删除,请查询 comp.StyleConfigurations 的值。

示例

removeStyle(comp,ordernum) 指定要删除的样式。根据添加样式的顺序指定一个样式。属性 comp.StyleConfigurations 按照添加样式的顺序列出各样式。

示例

全部折叠

首先,向一个树添加两个样式。

fig = uifigure;
fig.Position = [100 100 250 350];
t = uitree(fig);
n1 = uitreenode(t,'Text','Fruits');
n11 = uitreenode(n1,'Text','Banana');
n12 = uitreenode(n1,'Text','Cherry');
n2 = uitreenode(t,'Text','Vegetables');
n21 = uitreenode(n2,'Text','Broccoli');
n22 = uitreenode(n2,'Text','Lettuce');
expand(t)

s1 = uistyle('FontColor',[0 0.4 0.7]);
s2 = uistyle('FontColor',[0.1 0.5 0.1]);

addStyle(t,s1,'level',2);
addStyle(t,s2,'node',[n2 n21 n22]);

A tree with nodes listing fruits and vegetables. The Banana and Cherry nodes are blue, and the Vegetables, Broccoli, and Lettuce nodes are green.

然后,删除这两个样式以将树还原为其默认外观。

removeStyle(t)

A tree with nodes listing fruits and vegetables. All node font is black.

向表 UI 组件添加多个样式,然后删除其中一些样式。

首先,创建一个表 UI 组件,并向它的不同部分添加样式。

fig = uifigure; 
fig.Position = [500 500 720 230]; 
uit = uitable(fig); 
uit.Data = randi([-20,20],7); 
uit.Position = [20 30 680 185]; 
 
[row,col] = find(uit.Data<0);

s1 = uistyle;
s1.BackgroundColor = 'cyan';
addStyle(uit,s1,'column',[1 3 5]) 

s2 = uistyle;
s2.FontColor = 'red';
s2.FontWeight = 'bold';
addStyle(uit,s2,'cell',[row,col])

s3 = uistyle('BackgroundColor','green');
addStyle(uit,s3,'row',[3 4])

addStyle(uit,s1,'column',7)

Table UI component with 7 columns and 7 rows. The negative-valued data is displayed in bold red text. Cells in rows 3 and 4 and between columns 1 and 6 are green. The remaining cells in columns 1, 3, and 5 are cyan. All of the cells in column 7 are cyan.

现在,删除行和列样式。首先,查询该表的 StyleConfigurations 属性的值。

uit.StyleConfigurations
ans=4×3 table
         Target     TargetIndex                Style           
         ______    _____________    ___________________________

    1    column    { 1x3 double}    [1x1 matlab.ui.style.Style]
    2    cell      {20x2 double}    [1x1 matlab.ui.style.Style]
    3    row       { 1x2 double}    [1x1 matlab.ui.style.Style]
    4    column    {[        7]}    [1x1 matlab.ui.style.Style]

StyleConfigurations 属性值显示顺序号为 14 的样式影响列的样式,行样式是添加到表中的第三个样式。通过指定样式顺序号 134 删除样式。

removeStyle(uit,[1 3 4])

Table UI component. The negative-valued data is displayed in bold red text. All cells have the default background color.

输入参数

全部折叠

UI 组件,指定为以下 UI 组件之一:

  • 使用 uitable 函数创建的 Table 对象

  • 使用 uitree 函数创建的 Tree 对象

  • 使用 uilistbox 函数创建的 ListBox 对象

  • 使用 uidropdown 函数创建的 DropDown 对象

该组件对象必须以使用 uifigure 函数创建的图窗或其子容器之一为父级。

样式顺序号,指定为正整数或正整数向量。要确定当前应用于表的样式及其添加顺序,请查询 StyleConfigurations 属性的值。

当您删除上次添加的样式之外的样式时,其余样式会向上移动以填补空缺。如果未指定样式顺序号,将从 UI 组件中删除所有样式。

示例: removeStyle(comp,2) 删除 comp.StyleConfigurations 返回的列表中的第二个样式。

示例: removeStyle(comp,[1 3 5]) 删除 comp.StyleConfigurations 返回的列表中的第一个、第三个和第五个样式。

示例: removeStyle(comp) 从 UI 组件中删除所有样式。

版本历史记录

在 R2019b 中推出

全部展开