问题
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.
回答1:
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 Widget
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" );
Styled Text
StyledText text = new StyledText( parent, SWT.NONE );
text.setText( "If you restore data the changes will be lost, So recheck once" );
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
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.
来源:https://stackoverflow.com/questions/28874579/display-some-part-of-text-in-bold-and-some-part-in-italic-in-swt-label