如何在 GWT 标签中插入 <b> 元素
How to insert <b> Element in GWT Label
我正在使用 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 中的文本加粗。
您可以使用 SafeHtmlBuilder
class 来实现。您告诉它您想要转义哪些字符串以及您不想转义哪些字符串。它的工作方式类似于 StringBuilder
,因为您调用了追加方法。
类似答案请参考link:
GWT: Putting raw HTML inside a Label
使用 HTML 小部件而不是标签小部件:
<g:HTML ui:field="imgInfo">You can upload only <b>.jpg</b> images</g:HTML>
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 中,两个 class 都是空的。因此,只需选择适合您的方法即可。
您可以改用 DOM 元素。
@UiField
Element myLabel;
public void setLabelText(String text)
{
myLabel.setInnerHTML(new SafeHtmlBuilder().appendEscaped(text).toSafeHtml().asString());
}
我正在使用 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 中的文本加粗。
您可以使用 SafeHtmlBuilder
class 来实现。您告诉它您想要转义哪些字符串以及您不想转义哪些字符串。它的工作方式类似于 StringBuilder
,因为您调用了追加方法。
类似答案请参考link: GWT: Putting raw HTML inside a Label
使用 HTML 小部件而不是标签小部件:
<g:HTML ui:field="imgInfo">You can upload only <b>.jpg</b> images</g:HTML>
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 中,两个 class 都是空的。因此,只需选择适合您的方法即可。
您可以改用 DOM 元素。
@UiField
Element myLabel;
public void setLabelText(String text)
{
myLabel.setInnerHTML(new SafeHtmlBuilder().appendEscaped(text).toSafeHtml().asString());
}