I am having a TextView that has a huge text in between i have a tamil word and i know how to embedd the tamil font in seperate textview .but i need the tamil word between english word please help thanks in advance
my part of text in textview :
Seasonal messages like welcome (நல்வரவு) is used in Kolam. Volunteering to draw kolam at temple is sometimes done when a devotee's
Try setting this Akshar.ttf font to your TextView through setTypeface, it suuports both English and Tamil.
Here is the outcome:
Or, look for a similar font which supports both language.
your second solution is to use image for this small Tamil text portion using SpannableStringBuilder.
Code for setting custom font to TextView:
Assuming you have the Akshar.ttf font in fonts folder under assets folder:
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/Akshar.ttf");
TextView tv = (TextView) findViewById(R.id.CustomFontText);
tv.setTypeface(tf);
tv.setText("Seasonal messages like welcome (நல்வரவு) is used in Kolam. Volunteering to draw kolam at temple is sometimes done when a devotee's");
There is another solution if you want to support multiple fonts within a TextView but they are not supported within a single ttf:
Use the following code:(I'm using Bangla and Tamil font)
TextView txt = (TextView) findViewById(R.id.custom_fonts);
txt.setTextSize(30);
Typeface font = Typeface.createFromAsset(getAssets(), "Akshar.ttf");
Typeface font2 = Typeface.createFromAsset(getAssets(), "bangla.ttf");
SpannableStringBuilder SS = new SpannableStringBuilder("আমারநல்வரவு");
SS.setSpan (new CustomTypefaceSpan("", font2), 0, 4,Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
SS.setSpan (new CustomTypefaceSpan("", font), 4, 11,Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
txt.setText(SS);
The outcome is:
CustomTypefaceSpan Class:
package my.app;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.text.TextPaint;
import android.text.style.TypefaceSpan;
public class CustomTypefaceSpan extends TypefaceSpan {
private final Typeface newType;
public CustomTypefaceSpan(String family, Typeface type) {
super(family);
newType = type;
}
@Override
public void updateDrawState(TextPaint ds) {
applyCustomTypeFace(ds, newType);
}
@Override
public void updateMeasureState(TextPaint paint) {
applyCustomTypeFace(paint, newType);
}
private static void applyCustomTypeFace(Paint paint, Typeface tf) {
int oldStyle;
Typeface old = paint.getTypeface();
if (old == null) {
oldStyle = 0;
} else {
oldStyle = old.getStyle();
}
int fake = oldStyle & ~tf.getStyle();
if ((fake & Typeface.BOLD) != 0) {
paint.setFakeBoldText(true);
}
if ((fake & Typeface.ITALIC) != 0) {
paint.setTextSkewX(-0.25f);
}
paint.setTypeface(tf);
}
}
I had this problem in Android 4.0. I found that this is due to csc(Country Code). If it is a India then Tamil working correctly else not working. I can confirm this.
Till ICS (4.0) there were no Tamil font available in Android Systems. Even then it had bugs and Google fixed it all with Jelly Bean (4.2).
Now if you wanna show Tamil in your application you need to use TAB, TAM, BAMINI or TSCII encodings.
I wrote a simple library that does the conversion for you dynamically. All you have to write is simple code as below.
// Initialise the Typeface (assumes TSCII, Bamini, Anjal, TAB or TAM font located inside assets/fonts folder)
Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/mylai.ttf");
// Initialises the TextView
TextView tv = (TextView)findViewById(R.id.textView1);
//Setting the Typeface
tv.setTypeface(tf);
//Magic happens here ;) encoding conversion
String TSCIIString = TamilUtil.convertToTamil(TamilUtil.TSCII, "வணக்கம் அன்ரொயிட்");
//Setting the new string to TextView
tv.setText(TSCIIString);
I have written more on this topic in this answer. Please check it out too.
来源:https://stackoverflow.com/questions/10635516/android-tamil-font-between-english-word