Android Paint.setTypeface 不适用于斜体

发布于 2024-11-09 12:08:00 字数 845 浏览 6 评论 0原文

Paint.setTypeface 不适用于斜体,或者我做错了事情。我可以创建普通、粗体、等宽和衬线文本,但无法创建斜体文本。它看起来总是正常的(或者在粗斜体的情况下,它看起来是粗体)。

    //This will appear monospace
    paint.setTypeface(Typeface.MONOSPACE);
    canvas.drawText("foo", 10, 10, paint);

    //This will appear serif
    paint.setTypeface(Typeface.SERIF);
    canvas.drawText("foo", 10, 10, paint);

    //This will appear bold
    paint.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
    canvas.drawText("foo", 10, 10, paint);

    //This will NOT appear italic <===  PROBLEM
    paint.setTypeface(Typeface.defaultFromStyle(Typeface.ITALIC));
    canvas.drawText("foo", 10, 10, paint);

    // This isn't working either <===  PROBLEM
    paint.setTypeface(Typeface.create(Typeface.SANS_SERIF, Typeface.ITALIC));

现在的问题是:是否有已知的解决方法?我的简单目标是用斜体风格绘制一些单词......

The Paint.setTypeface is not working for italic or I'm doing something the wrong way. I can create normal, bold, monospace, and serif text, but I can't create italic text. It always looks normal (or in the case of bold-italic, it looks bold).

    //This will appear monospace
    paint.setTypeface(Typeface.MONOSPACE);
    canvas.drawText("foo", 10, 10, paint);

    //This will appear serif
    paint.setTypeface(Typeface.SERIF);
    canvas.drawText("foo", 10, 10, paint);

    //This will appear bold
    paint.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
    canvas.drawText("foo", 10, 10, paint);

    //This will NOT appear italic <===  PROBLEM
    paint.setTypeface(Typeface.defaultFromStyle(Typeface.ITALIC));
    canvas.drawText("foo", 10, 10, paint);

    // This isn't working either <===  PROBLEM
    paint.setTypeface(Typeface.create(Typeface.SANS_SERIF, Typeface.ITALIC));

So now the question: is there a known workaround for this? My simple goal is to draw some words with italic style...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

抹茶夏天i‖ 2024-11-16 12:08:00

遇到同样的困难后,我在TextView源代码中摸索找到了解决方案。试试这个:

paint.setTextSkewX(-0.25f);

After experiencing the same difficulty, I found the solution by fishing around in theTextViewsource code. Try this:

paint.setTextSkewX(-0.25f);
与风相奔跑 2024-11-16 12:08:00

我有同样的问题。看起来并不是所有的 android 字体都支持 ITALIC 风格。尝试以下操作,这对我有用:

paint.setTypeface(Typeface.create(Typeface.SERIF,Typeface.ITALIC));

仅与 SERIF 配合使用即可正常工作。 DEFAULT、MONOSPACE、SANS_SERIF 忽略此样式。

PS 我说的是 API 10。

I have the same problem. looks like not all android typefaces supports ITALIC style. Try following, i'ts worked for me:

paint.setTypeface(Typeface.create(Typeface.SERIF,Typeface.ITALIC));

Works fine just with SERIF. DEFAULT, MONOSPACE, SANS_SERIF ingnores this style.

P.S. I'm talking about API 10.

下雨或天晴 2024-11-16 12:08:00

为了为不支持默认字体的设备获取斜体模式,我们应该使用 setTextSkewX 方法。然而,在应用它之前,我们必须确保不支持斜体模式。我们通过创建一个临时 TextView 对象并在两种模式(正常和斜体)下测量其宽度来实现它。如果它们的宽度相同,则表示不支持斜体模式。

请查看另一个问题中提出的解决方案:三星设备支持 setTypeface(Typeface.Italic)?

In order to get an italic mode for devices which don't support it for a default font, we should use setTextSkewX method. However, before applying it we have to be sure that an italic mode is not supported. We achieve it by creating a temporal TextView object and measuring its width in both modes (NORMAL and ITALIC). If their widths are same, then it means an ITALIC mode is NOT supported.

Please, take a look at a solution presented in another question: Samsung devices supporting setTypeface(Typeface.Italic)?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文