How to set a label text to a special character? - java

I need to set a label to have some special characters, I'm trying:
Label label = new Label();
label.setText("•");
label.setText("♦");
label.setText("★");
I'm not seeing the characters rendered though (firefox 17). The output html looks like this:
<div class="gwt-Label"></div>
Is there a different way we need to set the text to those characters?

I do no think it is possible with Label widget. You should be using HTML class which extends Label.
SafeHtmlBuilder builder = new SafeHtmlBuilder();
builder.appendEscaped("★");
HTML widget = new HTML();
widget.setHTML(builder.toSafeHtml());
RootPanel.get().add(widget);
Also ensure best practice of using SafeHtmlBuilder class.

You should make sure your source code uses UTF-8 encoding. If you are using Eclipse, you can set the default encoding in Windows -> Preferences.
The short version of this is: if you do absolutely everything in your GWT project and deployment using UTF-8 encoding, then all your special characters should work as expected.
If you have lots of existing files to convert, the JDK contains a little tool to convert your files for you.

You can use html codes: ★ for star(★), • for bullet (•) and so on.
http://www.quackit.com/html/html_special_characters.cfm

Related

Unicode character Issue - openhtmltopdf library

I use openhtmltopdf library (version: 0.0.1-RC15). I have a problem with unicode characters. In PDF file I see "#" symbols instead of "ă" and "ș".
How I can fix it? Thank you.
In case it helps, we're using com.openhtmltopdf:openhtmltopdf-pdfbox:1.0.10 and have found a way that works:
new PdfRendererBuilder()
.useFastMode()
.useFont(new File(main.class.getClassLoader().getResource("arial-unicode-ms.ttf").getFile()), "Arial Unicode MS")
.withW3cDocument(new W3CDom().fromJsoup(Jsoup.parse(html)), null)
.toStream(os)
.run();
In the html we have a <style> element that declares some css for the page, in particular:
font-family: "Arial Unicode MS";
Unicode characters then display as they should.
I am using openhtmltopdf library. I had character problem too. Turkish characters looks like (İÖĞ -> ###).
People already say its about font problem.
I add useFont method like this and download tff file in resource/fonts/
PdfRendererBuilder builder = new PdfRendererBuilder();
builder.useFont(new File(Main.class.getClassLoader().getResource("fonts/ARIAL.TTF").getFile()), "Arial");

JavaFx html formatted text in pdf using iText with formatation

Is it possible to set a formatted HTML-Text (Color, Alignment, ...) from a HTMLEditor to an "editable" PDF using iText.
I didn't find anything on the internet.
Thanks.
The easiest way of doing this is (as Amedee suggested) using pdfHTML.
It's an iText7 add-on that converts HTML5 (+CSS3) into pdf syntax.
The code is pretty straightforward:
HtmlConverter.convertToPdf(
"<b>This text should be written in bold.</b>", // html to be converted
new PdfWriter(
new File("C://users/user2002/output.pdf") // destination file
)
);
To learn more, go to https://itextpdf.com/itext7/pdfHTML
I found a Solution in this post using The Flying Saucer: this

Swing JLabel - strike through HTML text

I am having problems adding a HTML strike-through tag to a string of text in Java.
EDIT: I have posted the entire code and I am using Windows 7, Java 8 and Swing.
JLabel LocalSelectorLabel = new JLabel("<html><strike>Text Here</strike><html>");
LocalSelectorLabel.setBounds(12, 50, 55, 16);
LocalTabPanel.add(LocalSelectorLabel);
I have tried the tags: s, del and strike but all of them just give me underlined text.
I have looked around for other tags that might work but I haven't found anything.
EDIT 2: This works for me, thanks:
new JLabel("<html><body><span style='text-decoration: line-through;'>Text Here</span></body></html>");
EDIT 3: Now this is interesting. I ran the code on a different computer and that one displays as strike-through and as underline as expected while the computer I used to ask this question did not.
I see now why I didn't find anything when I searched for a solution.
"<html></strike>Text Here</strike><html>"
Should be:
"<html><strike>Text Here</strike><html>"
The 'opening' </strike> tag should be <strike>.
As an aside, it never hurts to check the HTML using an HTML validator. For 'Swing HTML' set it to HTML 3.2 (or 4.01 transitional, if they stop supporting 3.2).
As a continue of #Gaël answer, using css you can achive your desired result with the follwing code:
new JLabel("<html><body><span style='text-decoration: line-through;'>Text Here</span></body></html>");
Be aware that <strike> tag is not part of HTML5, you should use CSS to achieve this.
EDIT : you're using Swing, so HTML 3.2 and <strike> tag exists (but still not recommended for future updates).

Display HTML text in SWT Label

I would like to display the HTML text in a Java SWT Label.
Below is my code for creating a label.
Label theLabel = new Label(controls,SWT.WRAP);
theLabel.setSize(100,500);
theLable.setText("<html><ol><li>Hello</li><li>welcome</li></ol></html>");
When I run the application as Eclipse Application I get the output as:
<html><ol><li>Hello</li><li>welcome</li></ol></html>
What is the mistake? Why I am not getting the html formatted output in my label? I am using Eclipse plugin with a view.
To show HTML with SWT you will have to use the Browser widget instead.
Browser browser = new Browser( parent, SWT.NONE );
browser.setText( "<html><ol><li>Hello</li><li>welcome</li></ol></html>" );
If you don't mind the extra dependency on org.eclipse.ui.forms you can also use FormText. But be aware that the control does only understand a subset of HTML (<p>, <b>, <li>, <img>, <br>, <span>) to render simple formatted text.

Using HTML in Android Alert Dialog

I have some amount of informations to be displayed in Dialog Box. It comes like Title, then under it text; Title, then under it text. Like wise, there are 4 titles and 4 descriptions to be displayed. It should come like this
Title One
description;description;description;description;description;description;description;description;description;description;description;description;description;description;description
Title Two
description;description;description;description;description;description;description;description;description;description;description;description;description;description;description
As you can see, there are bold texts, underlined texts, line breaks etc. I want to add this kind of a text to the alert box, so below is what I tried.
TextView msg = new TextView(this);
msg.setText("<html><u>Message</u></html>")
AlertDialog.Builder ab = new AlertDialog.Builder(this);
ab.setTitle("Title");
ab.setView(msg);
ab.setCancelable(false);
//rest of the code
However this trick didn't work. What happened is, all the HTML tags showed up as they are! And the text is not clear! Seems like it mixed with the background of the default colour of AlertBox, black. How can I solve this issue? Please help!
PS: Or am I using the wrong method? Wrong dialog box?
You will need to use Html.fromHtml() to use HTML tags in TextView as:
msg.setText(Html.fromHtml("<u>Message</u>"))
And you also see all HTML tags supported by TextView.
As it turns out, you don't actually need any extra TextViews to do this. Simply include the HTML in your alert's "setMessage()" call (which replaces the "setView()" call in your question) and pass it the html-formatted string. Be sure to only use <b>, <u>, and <i> in your formatting, though because those are the only tags it supports. If you're using a String resource for the text in your alert, call getResources().getText(R.id.yourHtmlString) rather than getResources().getString(R.id.yourHtmlString), though, or the tags will be completely stripped from the String.
If you want to add a link and make it clickable,
msg.setMovementMethod(LinkMovementMethod.getInstance());
msg.setClickable(true);
If you need to add more complex HTML, with CSS and META, you can add a WebView to the dialog, like this:
String webViewString = yourMeta + yourCss + yourHtml;
yourCustomWebView.loadData(webViewString, "text/html; charset=UTF-8",
null);
yourAlertDialog.setView(yourCustomWebView);
This way, you can display fully formatted HTML pages in your dialog.
Try this,
Font color,
String source = "<b><font color=#ff0000> Loading. Please wait..."
+ "</font></b>";
Font underline,
String source = <u>Message</u>
msg.setText(Html.fromHtml(source));
In case if you need it.
Better to use HtmlCompat.fromHtml((htmlString, 0) for compatibility with older versions.

Categories

Resources