Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
377 views
in Technique[技术] by (71.8m points)

android - Samsung devices supporting setTypeface(Typeface.Italic)?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...