2016-06-24 4 views
0

У меня есть два шрифта, и я бы хотел использовать Typeface1 для названий и Typeface2 для тела.Различные шрифты для частей текста

Есть ли способ сделать это? Я могу добавить метки вокруг заголовка, если это поможет, как <b>Title</b>

Typeface typeface1 = Typeface.createFromAsset(getAssets(), "fonts/Typeface1.ttf"); 
Typeface typeface2 = Typeface.createFromAsset(getAssets(), "fonts/Typeface2.ttf"); 
textView.setTypeface(typeface1); 

textView.setText("<b>Title 1</b>\n" + 
       "Body 1\n" + 
       "<b>Title 2</b>\n" + 
       "Body 2\n" + 
       "<b>Title 3</b>\n" + 
       "Body 3"); 

ответ

2

Вы должны использовать Html.fromHtml() использовать HTML в ваших XML-строк. Просто ссылаясь на String с HTML в вашем макете, XML не будет работать.

Пример:

textView.setText(Html.fromHtml("<b>Title</b><br>Body1<br><b>Title2</b><br>Body2")); 

Это может работать

1

попробуйте использовать SpannableStringBuilder, как предусмотрено в this answer

TextView txt = (TextView) findViewById(R.id.custom_fonts); 
Typeface font = Typeface.createFromAsset(getAssets(), "font1.ttf"); 
Typeface font2 = Typeface.createFromAsset(getAssets(), "font2.ttf"); 
SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder("TextTextText"); 
spannableStringBuilder.setSpan (new CustomTypefaceSpan("", font2), start, end, Spanned.SPAN_EXCLUSIVE_INCLUSIVE); 
spannableStringBuilder.setSpan (new CustomTypefaceSpan("", font), start, end, Spanned.SPAN_EXCLUSIVE_INCLUSIVE); 
txt.setText(SS); 

CustomTypefaceSpan.java

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); 
} 
} 
0

Не рекомендуется смешивать шрифты и теги html.
Чтобы использовать теги html в текстовом виде, см. Ответ (Satish Kumar's).

textView.setText("<b>Title 1</b>\n" + 
       "Body 1\n" + 
       "<b>Title 2</b>\n" + 
       "Body 2\n" + 
       "<b>Title 3</b>\n" + 
       "Body 3"); 

Чтобы использовать несколько шрифтов в одном текстовом виде,

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

CustomTypefaceSpan Класс:

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); 
     } 
    } 


Ссылки:
* Multiple TypeFaces
Span styles and custom typefaces

Смежные вопросы