问题描述
我必须向用户显示一些警告消息,例如
I have to show some warning message to the user like
如果您还原数据,则更新的更改将丢失,因此重新检查一次""
"If you restore data the updated changes will be lost, So recheck once"
在德语中,我也必须使用粗体和斜体来表示相同的字符串.
In german language also I have to same string with bold and italic.
在两种语言中,对话框的高度和宽度都应相同
In both languages the dialog height and width should be same
public class BoldTextMessageDialog extends ZedisTrayDialog {
private Composite container;
private String firstSring;
private String secondString;
private String boldString;
private Button restoreButton;
private Button cancelButton;
public BoldTextMessageDialog(Shell shell, String firstSring, String secondString, String boldString) {
super(shell);
this.firstSring = firstSring;
this.secondString = secondString;
this.boldString = boldString;
}
@Override
protected Control createDialogArea(Composite parent) {
container = new Composite(parent, SWT.NONE);
container.setLayout(new FormLayout());
GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
gd.heightHint = 300;
gd.widthHint = 500;
container.setLayoutData(gd);
Label warningLabel = new Label(container, SWT.NONE);
warningLabel.setImage(parent.getDisplay().getSystemImage(SWT.ICON_WARNING));
FormData fd = new FormData();
fd.left = new FormAttachment(0, 5);
fd.top = new FormAttachment(0, 5);
warningLabel.setLayoutData(fd);
Label firstLabel = new Label(container, SWT.WRAP);
firstLabel.setText(firstSring);
fd = new FormData();
fd.left = new FormAttachment(warningLabel, 10);
fd.right = new FormAttachment(100, 0);
fd.top = new FormAttachment(0, 10);
firstLabel.setLayoutData(fd);
Label secLabel = new Label(container, SWT.WRAP);
secLabel.setText(boldString);
secLabel.setFont(FontCache.getBoldFont());
fd = new FormData();
fd.top = new FormAttachment(0, 25);
fd.left = new FormAttachment(0, 48);
secLabel.setLayoutData(fd);
Label thirdLabel = new Label(container, SWT.WRAP);
thirdLabel.setText(secondString);
fd = new FormData();
fd.top = new FormAttachment(0, 25);
fd.left = new FormAttachment(secLabel, 3);
fd.right = new FormAttachment(100, -5);
thirdLabel.setLayoutData(fd);
return parent;
}
}
这是我尝试过的方法,但是问题是德语和英语的斜体和粗体文本都出现在不同的位置,因此对于相同的大小,它们不合适.如果我使用其他尺寸,还可以.
This is what I tried, but the problem is for german and english both italic and bold text are coming at different places, so for same size they are not suitable. If I use different sizes its ok.
推荐答案
要在SWT中显示样式化的文本,可以使用Browser
小部件或StyledText
小部件.在这两种情况下,您可能都需要将默认外观和行为更改为类似标签(即背景色,只读)
To display styled text in SWT you can use the Browser
widget or StyledText
widget. In both cases you likely need to change the default appearance and behavior to be label-like (i.e. background color, read-only)
浏览器窗口小部件
Browser browser = new Browser( parent, SWT.NONE );
browser.setText( "If you restore data the changes will be <b>lost</b>, So <i>recheck</i> once" );
样式文本
StyledText text = new StyledText( parent, SWT.NONE );
text.setText( "If you restore data the changes will be lost, So recheck once" );
通过StyleRange
,您可以定义文本部分的格式.样式范围具有start
和length
(用于指定要应用于文本的一部分)和TextStyle
(用于控制要应用的样式属性).要使第一个字符显示为粗体,代码应如下所示:
Through StyleRange
s you can define how portions of the text should be formatted. A style range has a start
and a length
that specifies the part of the text it is applied to and a TextStyle
to control the style attributes to be applied. To let the first char appear bold the code would look like this:
StyleRange styleRange = new StyleRange( 0, 1, null, null, SWT.BOLD );
text.setStyleRanges( new StyleRange[]{ styleRange } );
FormText
如果您已经依赖于org.eclipse.ui.forms
,另一种选择是使用FormText
小部件.它支持HTML之类的文本内的标记,类似于浏览器小部件.这可能是最类似于标签的小部件,但会增加其他依赖性.
A further option, if you have a dependency on org.eclipse.ui.forms
already, is to use the FormText
widget. It supports HTML like markup within the text, similar to the browser widget. This is probably the most label-like widget but drags in an additional dependency.
这篇关于在SWT标签中以粗体显示文本的一部分,以斜体显示文本的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!