Samsung devices supporting setTypeface(Typeface.Italic)?
Asked Answered
L

4

11

I have in application that makes use of a custom View component that drawas some text onto the screen via Paint/Canvas.

I am using the following code (before I call canvas.drawText()) to make my text Italic:

mPaintText.setTypeface(Typeface.defaultFromStyle(Typeface.ITALIC));

This works on Samsung Galaxy Nexus. But on Samsung Epic 4g (galaxy S), Samsung Epic Touch (Galaxy SII), and Samsung Transform ultra my text is still non-italic.

Does anyone know why some of these samsung devices wouldn't support setting italic text that way? I know the devices are capable of rendering the italic text because if I have a TextView I can use either

tv.setText(Html.fromHtml("<i>sometext</i>");

in java or

android:textStyle="italic"

in layout.xml and my text appears italic.

Does anyone know of another way that I can set the drawText() method of canvas to draw the text italicized that might work on these devices?

EDIT:

Here is a list of some ways I've tried it with their outcome in comments after. Turns out SERIF seems to be the only font that it works on.

mPaint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.ITALIC) //Nothing
mPaint.setTypeface(Typeface.create(Typeface.DEFAULT_BOLD, Typeface.ITALIC) //Nothing
mPaint.setTypeface(Typeface.create(Typeface.SERIF, Typeface.ITALIC) //omg it is italic...But serifs look gross.
mPaint.setTypeface(Typeface.create(Typeface.SANS_SERIF, Typeface.ITALIC) //Nothing
mPaint.setTypeface(Typeface.create(Typeface.MONOSPACE, Typeface.ITALIC) //Changes font, but still no italic.
mPaint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD_ITALIC) //Bold but no italic

EDIT AGAIN: To make this function I ended up adding the italic version of the roboto font to my assets folder and applied it as a font. I'd still be interested if anyone ever finds a way to get it working without adding it this way.

Levania answered 2/5, 2012 at 19:6 Comment(11)
What typeface are you using that is giving you this effect?Lylalyle
Whatever comes stock on the respective devices, I haven't changed the font.Levania
This thread could be helpful: https://mcmap.net/q/1021927/-android-italic-typefaceDashtikavir
Try passing direct values to the setTypeface method..Padding
Just for your info, my Galaxy SII has now been upgraded to 4.0.3 and paint.setTypeface(Typeface.defaultFromStyle(Typeface.ITALIC)) comes up OK in italic.Chinn
add a custom NICE font to ur assets and use setTypeface(Typeface.defaultFromStyle(Typeface.ITALIC), Typeface.ITALIC)! And remember! it should be a NICE LOOKING fontEdaphic
I ended up adding the italic version of the system font to my assets.Levania
On my Samsung Galaxy Tab 2, the setTypeface(Typeface.defaultFromStyle(Typeface.ITALIC)) solution doesn't work for me...Centonze
@Centonze right, that is the same issue I was facing. I never found a way to solve it really. I just ended up getting a copy of the italic system font and adding it to my application assets, and set my font to it.Levania
Thanks FoamyGuy, and would you mind providing me the way to do that (= copy the italic system font, adding it to the app assets and set the font to it)?Centonze
This thread coule be useful too -> developer.samsung.com/forum/board/thread/…Centonze
D
2

It may be that your Samsung device does not have a native italics version of the desired font installed. You may have to force the system to create the italics-style font synthetically. Try:

tv.setTypeface(Typeface.defaultFromStyle(Typeface.ITALIC), Typeface.ITALIC);

EDIT

Instead of defaultFromStyle, try to use Typeface.create (Typeface family, int style) (documented here).

Dashtikavir answered 2/5, 2012 at 19:41 Comment(4)
But I am trying to apply it to text drawn with Canvas / Paint objects. And Paint.setTypeface() only accepts one argument, not two...Levania
Just tested lots of the possible permutations of inputs to .create() results have been added to question.Levania
Could be a bug in Samsung's Android implementation... which OS version is this?Dashtikavir
Transform and Epic Touch(Galaxy S2) = 2.3.4, Regular Epic (Galaxy S) = 2.3.6Levania
P
1

Try passing direct values to the setTypeFace api till you find the right one. If italicizing is working through other methods then there could be some problem in constant definitions in TypeFace class (in those builds).

mPaintText.setTypeface(Typeface.defaultFromStyle(0)); // then 1, 2, 3
Padding answered 12/5, 2012 at 9:31 Comment(5)
If I start changing the values isn't that going to break it on the device that it does work on? (Galaxy Nexus)Levania
just for investigation sake try it and see.. atleast you'll know the problem root.. we can think of a solution then..Padding
Devices are at work, will try tomorrow. I found something that works but is not ideal. I found a copy of the italic system font and included it with my project, if I set the typeface to that file in my assets it appears correctly on the screen on the devices in question.Levania
Did you get the root of the problem..?Padding
nope, I tried subbing in several raw int values, but got nothing new.Levania
C
0

This is a bug from Samsung and the best solution is, as FomayGuy said, to add the italic version of the system font to the assets.

The official Roboto Android font is available here.

Centonze answered 19/2, 2013 at 20:14 Comment(0)
D
0

We need to check whether a default font supports an ITALIC mode. We do it by creating a temporal TextView object and measuring its width in both modes (NORMAL and ITALIC). If their widths are different, then it means an ITALIC mode is supported. Otherwise, a default font doesn't support it and we have to use setTextSkewX() method to skew a text.

    mPaint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.ITALIC));

    // check whether a font supports an italic mode, returns false if it does't
    if (!supportItalicMode(this, Typeface.DEFAULT))
    {
        paint.setTextSkewX(-0.25f);
    }



private boolean supportItalicMode(Context context, Typeface typeFace)
{
    Typeface tfNormal = Typeface.create(typeFace, Typeface.NORMAL);
    Typeface tfItalic = Typeface.create(typeFace, Typeface.ITALIC);

    TextView textView = new TextView(context);
    textView.setText("Some sample text to check whether a font supports an italic mode");

    textView.setTypeface(tfNormal);
    textView.measure(0, 0);
    int normalFontStyleSize = textView.getMeasuredWidth();

    textView.setTypeface(tfItalic);
    textView.measure(0, 0);
    int italicFontStyleSize = textView.getMeasuredWidth();

    return (normalFontStyleSize != italicFontStyleSize);
}
Decagram answered 6/10, 2015 at 11:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.