如何在GWT标签中插入<b>元素</b>

时间:2015-02-27 08:48:09

标签: java html gwt

我正在使用GWT UiBinder,我想创建以下标签

--------------------------------------------

您只能上传 .jpg 图片

--------------------------------------------

<g:Label ui:field="imgInfo">You can upload only <b>.jpg</b> images</g:Label>

但当然这是不正确的例子。我想使用Label,因为我想添加PopupPanel

 final PopupPanel popupImgInfo = new PopupInfo("Max size of the imagde:10Mb");

 @UiHandler("imgInfo")
    void doProtocol(MouseOverEvent event) {
        popupImgInfo
                .setPopupPositionAndShow(new PopupPanel.PositionCallback() {
                    @Override
                    public void setPosition(int offsetWidth, int offsetHeight) {
                        int left = imgInfo.getAbsoluteLeft();
                        int top = imgInfo.getAbsoluteTop() - 120;
                        popupImgInfo.setPopupPosition(left, top);
                        popupImgInfo.setWidth("400px");
                    }
                });
    }

    @UiHandler("imgInfo")
    void doProtocolHide(MouseOutEvent event) {
        popupImgInfo.hide();
    }

所以我的问题是如何在Label中插入html元素,或者你可以提供其他解决方案,使文本在GWT Label中变为粗体。

4 个答案:

答案 0 :(得分:2)

使用HTML小部件而不是标签1:

<g:HTML ui:field="imgInfo">You can upload only <b>.jpg</b> images</g:HTML>

答案 1 :(得分:1)

philfr49是绝对正确的。标签使用 createTextNode ,您不能使用HTML。如果您仍想这样做,您可以这样做:

DirectionalTextHelper directionalTextHelper = new DirectionalTextHelper(imgInfo.getElement(), true);
directionalTextHelper.setTextOrHtml("You can upload only <b>.jpg</b>; images", true);

更新: HTML(class =“gwt-HTML”)和Label(class =“gwt-Label”)都将DIV元素生成为包装元素。这两个类在standard.css中都是空的。所以,只需选择适合您的方法即可。

答案 2 :(得分:0)

您可以使用SafeHtmlBuilder类实现此功能。你告诉它你要逃脱的字符串以及你不想逃脱的字符串。它的作用类似于StringBuilder,因为您调用了追加方法。

请参阅类似答案的链接: GWT: Putting raw HTML inside a Label

答案 3 :(得分:0)

您可以使用DOM元素。

@UiField
Element myLabel;

public void setLabelText(String text)
{
    myLabel.setInnerHTML(new SafeHtmlBuilder().appendEscaped(text).toSafeHtml().asString());
}