My android application will use Chinese. The regular font is OK, but the italic font and bold font does not work.
So which font files should I use for Chinese italic and bold font?
I assume you are using TextView to show Chinese words.
If you want the whatever words in TextView to be bold or italic, it would be easy.
Just use
testView.getPaint().setFakeBoldText(true);
to make all words bold.
For italic, use:
testView.getPaint().setTextSkewX(-0.25f);
However, if you only want some words to be bold or italic. Normally you can set StyleSpan on specific range of your Spannablebut it is not work on Chinese word.
Therefore, I suggest you create a class extends StyleSpan
public class ChineseStyleSpan extends StyleSpan{
public ChineseStyleSpan(int src) {
super(src);
}
public ChineseStyleSpan(Parcel src) {
super(src);
}
#Override
public void updateDrawState(TextPaint ds) {
newApply(ds, this.getStyle());
}
#Override
public void updateMeasureState(TextPaint paint) {
newApply(paint, this.getStyle());
}
private static void newApply(Paint paint, int style){
int oldStyle;
Typeface old = paint.getTypeface();
if(old == null)oldStyle =0;
else oldStyle = old.getStyle();
int want = oldStyle | style;
Typeface tf;
if(old == null)tf = Typeface.defaultFromStyle(want);
else tf = Typeface.create(old, want);
int fake = want & ~tf.getStyle();
if ((want & Typeface.BOLD) != 0)paint.setFakeBoldText(true);
if ((want & Typeface.ITALIC) != 0)paint.setTextSkewX(-0.25f);
//The only two lines to be changed, the normal StyleSpan will set you paint to use FakeBold when you want Bold Style but the Typeface return say it don't support it.
//However, Chinese words in Android are not bold EVEN THOUGH the typeface return it can bold, so the Chinese with StyleSpan(Bold Style) do not bold at all.
//This Custom Class therefore set the paint FakeBold no matter typeface return it can support bold or not.
//Italic words would be the same
paint.setTypeface(tf);
}
}
Set this span to your Chinese words and I should be work.
Be aware to check it is only set on Chinese words only. I have not test on it but I can imagine that set fakebold on a bold English characters would be very ugly.
I'd suggest you don't use bold and italic fonts when displaying chinese text.
Bold is likely to distort the text and italic will only artificially skew the text.
Related
A TextView is displayed in one activity and the user goes to another activity to edit the properties of the text such as color, size, bold, italics, and underlined. When the user chooses to make the text bold, italic, or bold_italic it works. If the user unselects the checkbox the TextView does not return to normal. It can shift between the styles (ie if I check bold, then uncheck bold and check italic the TextView will be italic, not bold) but it cannot have a normal TypeFace.
I've searched around and everything I've found says that using the setTypeface method with Typeface.NORMAL should work but it is not working.
I would appreciate some help in solving this. The relevant code is below. Thank you!
TextView mDisplayMessage = (TextView) findViewById(R.id.message);
mDisplayMessage.setTypeface(mDisplayMessage.getTypeface(), Typeface.NORMAL);
//TODO: Cannot return all the way back to normal. Remains bold/italic/bold_italic
if (mIsBold && mIsItalic) mDisplayMessage.setTypeface(mDisplayMessage.getTypeface(), Typeface.BOLD_ITALIC);
else if (mIsBold) mDisplayMessage.setTypeface(mDisplayMessage.getTypeface(), Typeface.BOLD);
else if (mIsItalic) mDisplayMessage.setTypeface(mDisplayMessage.getTypeface(), Typeface.ITALIC);
else mDisplayMessage.setTypeface(mDisplayMessage.getTypeface(), Typeface.NORMAL);
if (mIsUnderlined) {
mDisplayMessage.setPaintFlags(mDisplayMessage.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
} else {
mDisplayMessage.setPaintFlags(0);
}
If you didn't care about font family you can use
mDisplayMessage.setTypeface(Typeface.DEFAULT);
Because Typeface.DEFAULT change the font family(serif, sans-serif, monospace), but the below can keep the same font family with style changed.
Typeface typeface = mDisplayMessage.getTypeface();
int style = Typeface.NORMAL;
if(mIsBold && mIsItalic) {
style = Typeface.BOLD_ITALIC;
} else if(mIsBold) {
style = Typeface.BOLD;
} else if(mIsItalic) {
style = Typeface.ITALIC;
}
Typeface newTypeface = Typeface.create(typeface, style);
mDisplayMessage.setTypeface(newTypeface);
Try this,
mDisplayMessage.setTypeface(Typeface.DEFAULT);
OR
mDisplayMessage.setTypeface(null, Typeface.NORMAL);
I can't change color of the u23F8 (pause symbol).
play symbol looks ok and I can change its color
playPauseButton = new Button(mContext);
playPauseButton.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 72);
playPauseButton.setTextColor(Color.WHITE);
...
if (mPlayer.isPlaying()) {
playPauseButton.setText("\u23F8");
} else {
playPauseButton.setText("\u25B6")
}
The TextView font renders \u23F8 and \u25B6 as an emoji, which means it basically uses a predefined image, so font colors are ignored on these.
I know we can change edit text font by using Typeface. But what about errors we set for edit text?
Look at codes below:
Typeface font = Typeface.createFromAsset(getAssets(), "fonts/ATaha.ttf");
private EditText mPasswordView;
mPasswordView = (EditText) findViewById(R.id.password);
mPasswordView.setTypeface(font);
With this code I could only change edit text font but when I set error like this:
mPasswordView.setError(getString(R.string.error_field_required));
The error notification font is android default font and didn't change by using type face. How can I change that?
You can use a SpannableString to set the font:
SpannableString s = new SpannableString(errorString);
s.setSpan(new TypefaceSpan(font), 0, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
mPasswordView.setError(s);
A custom Span class that has a specific Typeface set:
public class TypefaceSpan extends MetricAffectingSpan {
private Typeface mTypeface;
public TypefaceSpan(Typeface typeface) {
mTypeface = typeface;
}
#Override
public void updateMeasureState(TextPaint p) {
p.setTypeface(mTypeface);
p.setFlags(p.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
}
#Override
public void updateDrawState(TextPaint tp) {
tp.setTypeface(mTypeface);
tp.setFlags(tp.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
}
}
Since you can't directly set a Typeface for error text, you can achieve it by setting an HTML string as a text inside it.
You can see HTML Tags supported by a TextView in The CommonsBlog
We have face attribute for font, which means you can change the font-family.
mPasswordView.setError(Html.fromHtml("<font face='MONOSPACE'>Error font is MONOSPACE</font>"));
By setting spannable string in error message or extend EditText and overrite your own error draw mechanism.
Do anyone know how to highlight a certain word in a TextView?
With highligt I mean to use an italic typeface for the word "Hua Hin" in the following string.
The sandy beaches of Hua Hin, popular resort town in Thailand.
Must I use a textView in another textView to acomplish this?
I set the text in the onPostExecute-method of AsyncTask:
protected void onPostExecute(Bitmap[] bitmap) {
for (int i = 0; i < nmbrOfImages; i++) {
imageView[i].setImageBitmap(bitmap[i]);
textView[i].setText(scrollText[i]);
textView[i].setVisibility(TextView.VISIBLE);
}
loadAlertDialog();
}
I have all my text in an xml-file
If only italic you want then you can go for
textView[i].setText(Html.fromHtml(<i>some part</i> some other text);
else you can go for Spnnable
Here is an example in this answer.
How to change the color of a paragraph word by word with timer. like at first second i want to change color of The in the below paragraph, after 5th second change color of text, after 8th second change color of will be and so on....
The text will be wrapped in tags, and displayed in a monospaced font.
just use Timer and change the font color of your edit text accordingly and stop timer in focus lost.
i think you can do something like this :
Split the paragraph to words by using the method :
split(String separator);// it will return an array of Strings
//in your case you will do somthing like this
myWords = paragraph.split(" ");// the separator is the space
And then , you can use the method to colorate what ever you want of those words by using the method :
myNewParagraph.append(HTML.fromHtml("<font color...>... Your HTML Code to colorate your Text"));
and when you finish coloring each word , you update your textView to display the new text colored
Hope it helps
You can use spans to control the appearance of text. Take a look at Spannable and CharacterStyle.
Here is an example. Of course you would need to put this in some sort of timer.
Spannable spannableText = getSpannableText(yourTextView);
spannableText.setSpan(new TextAppearanceSpan(...), wordStart, wordEnd)
yourTextView.setText(spannableText);
private Spannable getSpannableText(TextView tv) {
CharSequence cs = tv.getText();
if (cs instanceof Spannable) {
return (Spannable)cs;
} else {
return SpannableString.valueOf(cs);
}
}