GVKun编程网logo

Java摆动HTML drawString(java 移动)

23

如果您想了解Java摆动HTMLdrawString的相关知识,那么本文是一篇不可错过的文章,我们将对java移动进行全面详尽的解释,并且为您提供关于AndroidHtml.fromHtml(Stri

如果您想了解Java摆动HTML drawString的相关知识,那么本文是一篇不可错过的文章,我们将对java 移动进行全面详尽的解释,并且为您提供关于Android Html.fromHtml(String)不适用于文本、android – 如何用双引号将HTML数据添加到java中的String?、asp.net – MvcHtmlString.ToHtmlString()不编码HTML?、asp.net-mvc – MvcHtmlString.Create()和Html.Raw()之间的区别的有价值的信息。

本文目录一览:

Java摆动HTML drawString(java 移动)

Java摆动HTML drawString(java 移动)

我正在尝试创建一些用于特定目的的特殊组件,在该组件上我需要绘制HTML字符串,这是示例代码:

 public class MyComponent extends JComponent{     public MyComponent(){        super();     }     protected void paintComponent(Graphics g){        //some drawing operations...        g.drawString("<html><u>text to render</u></html>",10,10);     } }

不幸的是,drawString方法似乎无法识别HTML格式,它愚蠢地按原样绘制字符串。

有什么办法可以使这项工作吗?

答案1

小编典典

我找到了一种简洁的模拟方法paintHtmlString; 这是代码:

public class MyComponent extends JComponent {    private JLabel label = null;    public MyComponent() {        super();    }    private JLabel getLabel() {        if (label == null) {            label = new JLabel();        }        return label;    }    /**     * x and y stand for the upper left corner of the label     * and not for the baseline coordinates ...     */    private void paintHtmlString(Graphics g, String html, int x, int y) {        g.translate(x, y);        getLabel().setText(html);        //the fontMetrics stringWidth and height can be replaced by        //getLabel().getPreferredSize() if needed        getLabel().paint(g);        g.translate(-x, -y);    }    protected void paintComponent(Graphics g) {        //some drawing operations...        paintHtmlString(g, "<html><u>some text</u></html>", 10, 10);    }}

Android Html.fromHtml(String)不适用于文本

Android Html.fromHtml(String)不适用于文本

我正在尝试使用html标签向我的TextView添加不同颜色的线条.

不管出于什么原因

    Html.fromHtml("<font color='#145A14'>text</font>");

将不会在TextView中显示彩色.

解决方法:

Html.fromHtml("<font color='#145A14'>text</font>");

而不是上面请使用以下

Html.fromHtml("<![CDATA[<font color='#145A14'>text</font>]]>");

这对我有用,我相信它也适合你.

如有任何问题,请告诉我.

android – 如何用双引号将HTML数据添加到java中的String?

android – 如何用双引号将HTML数据添加到java中的String?

String s=
    "<html>
    <head>
    <title>app</title>
    </head>
    <body bgcolor="pink">
    <div align="center"><img src="" width="900" height="100"/>
    </div>
    <h2 align="center">Welcome to your rewards program</h2>
    <table>
    <tr>
    <td><p>&nbsp;&nbsp;&nbsp;&nbsp;You Now  have ongoing uninterrupted access to thousands of discounts all on your phone .Accessing great  savings just got so easier . You can search by category , by keyword and also search local with what around me. Select a merchant offer you like and present the coupon on your phone  at the time of settling your bill .  Some merchants only offer their services online and you will also be able  to click through to their online offerings . 
    GIN image</p></td>
    </tr>

    </table>
    </body>
    </html>";

我想将html数据添加到字符串中,但它给了我错误.我怎么能解决这个问题?
我使用了escapse序列,但它没有用.

谢谢

解决方法:

试试这个

String s=
            "<html>"+
            "<head>"+
            "<title>app</title>"+
            "</head>"+
            "<body bgcolor=\"pink\">"+
            "<div align=\"center\"><img src=\"\" width=\"900\" height=\"100\"/>"+
            "</div>"+
            "<h2 align=\"center\">Welcome to your rewards program</h2>"+
            "<table>"+
            "<tr>"+
            "<td><p>&nbsp;&nbsp;&nbsp;&nbsp;You Now  have ongoing uninterrupted access to thousands of discounts all on your phone .Accessing great  savings just got so easier . You can search by category , by keyword and also search local with what around me. Select a merchant offer you like and present the coupon on your phone  at the time of settling your bill .  Some merchants only offer their services online and you will also be able  to click through to their online offerings ."+ 
            "GIN image</p></td>"+
            "</tr>"+
            "</table>"+
            "</body>"+
            "</html>";

asp.net – MvcHtmlString.ToHtmlString()不编码HTML?

asp.net – MvcHtmlString.ToHtmlString()不编码HTML?

与此 question相关,我在ASP.NET MVC项目中使用XSS问题,并且与MvcHtmlSTring.ToHtmlString()方法混淆.从 documentation“返回一个代表当前对象的HTML编码字符串.”,但它在我的情况下不起作用:
var mvcHtmlString = MvcHtmlString.Create("<SCRIPT/XSS SRC=\"htpp://ha.ckers.org/css.js\">").ToHtmlString();

    var encoded = HttpUtility.HtmlEncode("<SCRIPT/XSS SRC=\"htpp://ha.ckers.org/css.js\">");

输出mvcHtmlString

<SCRIPT/XSS SRC="htpp://ha.ckers.org/css.js">

输出编码< - 这是我怀疑的行为!

&lt;SCRIPT/XSS SRC=&quot;htpp://ha.ckers.org/css.js&quot;&gt;

我错过了什么?

解决方法

MvcHtmlString(或 HtmlString,或实现 IHtmlString的任何内容)用于字符串,应该逐字地发出HTML – 即通过使它成为MvcHtmlString,你告诉它你真的想要那些HTML标签.

不同之处在于使用<%:..%>将字符串发送到ASP.NET页面时(ASP.NET 4或更高版本中的新增功能).在这种情况下,ASP.NET引擎将为您自动HtmlEncode常规字符串(或任何不实现IHtmlString的字符串),而MvcHtmlString将以逐字/未编码的形式发送到页面中.

即我认为文件是错误的.有一个connect ticket在HtmlString构造函数文档中有相同的错误,他们确实修复了它. (我以为我提出了这样的说法: – /或许我的关闭是别人的副本?)我没有注意到MvcHtmlString文档也是错误的.

asp.net-mvc – MvcHtmlString.Create()和Html.Raw()之间的区别

asp.net-mvc – MvcHtmlString.Create()和Html.Raw()之间的区别

我正在创建一个MVC项目。使用MVC 4和剃刀。在构建一些页面后,我在想:有什么区别?
MvcHtmlString.Create()

Html.Raw()

如果你能帮我在这里了解,那会很好的

提前致谢!

解决方法

这是一个很好的机会来看看ASP.NET( http://aspnetwebstack.codeplex.com)可用的​​源代码。

看看HtmlHelper.cs,这是Html.Raw()的代码:

public IHtmlString Raw(string value)
{
    return new HtmlString(value);
}

public IHtmlString Raw(object value)
{
    return new HtmlString(value == null ? null : value.ToString());
}

这是MvcHtmlString类的代码:

namespace System.Web.Mvc
{
    public sealed class MvcHtmlString : HtmlString
    {
        [SuppressMessage("Microsoft.Security","CA2104:DoNotDeclareReadOnlyMutableReferenceTypes",Justification = "MvcHtmlString is immutable")]
        public static readonly MvcHtmlString Empty = Create(String.Empty);

        private readonly string _value;

        public MvcHtmlString(string value)
            : base(value ?? String.Empty)
        {
            _value = value ?? String.Empty;
        }

        public static MvcHtmlString Create(string value)
        {
            return new MvcHtmlString(value);
        }

        public static bool IsNullOrEmpty(MvcHtmlString value)
        {
            return (value == null || value._value.Length == 0);
        }
    }
}

最显着的区别是Html.Raw()接受任何对象,而MvcHtmlString.Create()只接受字符串。此外,Html.Raw()返回一个接口,而Create方法返回一个MvcHtmlString对象。最后,创建处理为null。

今天关于Java摆动HTML drawStringjava 移动的分享就到这里,希望大家有所收获,若想了解更多关于Android Html.fromHtml(String)不适用于文本、android – 如何用双引号将HTML数据添加到java中的String?、asp.net – MvcHtmlString.ToHtmlString()不编码HTML?、asp.net-mvc – MvcHtmlString.Create()和Html.Raw()之间的区别等相关知识,可以在本站进行查询。

本文标签: