如何在vaadin标签上添加html格式? [英] How to add html format on a vaadin label?
问题描述
我想在vaadin标签上添加html格式的字符串.该字符串来自数据库,直接进入标签,必须使用加粗文本,换行符和其他一些花哨的文本格式设置其格式.我找到了几种解决方案,但这些解决方案是针对旧版本的vaadin的,有什么办法可以在vaadin flow上做到这一点?
I want to add an html formatted string on a vaadin label. The string comes from a database and goes straight to the label, it must be formatted with bold text, line breaks and some other fancy text things. I found a couple of solutions but those were for old version of vaadin, is there any way of doing this on vaadin flow?
推荐答案
您可以使用以下方法将HTML内容添加到标签:
You could add an HTML content to a label using:
Label label = new Label();
label.getElement().setProperty("innerHTML","A new line should be created after this <br /> <b>This text is bold</b> <br /> <i> This text is italic</i>");
add(label);
但是,如果您只想显示文本,则标签不是要使用的正确元素.如其 API :
BUT, if you want to display only text, then the label is not a correct element to use. As stated in its API :
请注意,Label组件并不意味着页面中的散文-它们应该通过使用setFor(Component)或通过使用HasComponents.add(Component ...)方法添加到另一个组件中来与之耦合.
Note that Label components are not meant for loose text in the page - they should be coupled with another component by using the setFor(Component) or by adding them to it with the HasComponents.add(Component...) method.
我会改用HTML
元素:
String yourContent ="A new line should be created after this <br /> <b>This text is bold</b> <br /> <i> This text is italic</i>";
Html html = new Html("<text>" + yourContent + "</text>");
add(html);
输出:
或者您可以封装功能并使用此处的示例创建用于显示HTML代码段的常规组件: https://vaadin.com/forum/thread/17072019
Or you could encapsulate the functionality and create a general component for displaying an HTML snippets based using an example here : https://vaadin.com/forum/thread/17072019
这篇关于如何在vaadin标签上添加html格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!