2016-06-24 7 views
0

Ich habe zwei Schriftarten und ich möchte Typeface1 für die Titel und Typeface2 für den Körper verwenden.Unterschiedliche Schrift für Teile des Textes

Gibt es eine Möglichkeit, das zu tun? Ich kann Tags um den Titel hinzufügen, wenn das helfen würde, wie <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"); 

Antwort

2

Sie müssen Html.fromHtml() verwenden, um HTML in XML-Strings zu verwenden. Wenn Sie einfach einen String mit HTML in Ihrem Layout referenzieren, funktioniert XML nicht.

Beispiel:

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

Dies könnte funktionieren

1

versuchen SpannableStringBuilder mit wie in this answer

bereitgestellt
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

Es wird nicht empfohlen, Schriftarten und HTML-Tags zu mischen.
Um HTML-Tags in der Textansicht zu verwenden, siehe (Satish Kumar) Antwort.

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

Um mehrere Schriftarten in der gleichen Textansicht

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 Klasse zu verwenden:

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


Referenzen:
* Multiple TypeFaces
* Span styles and custom typefaces

Verwandte Themen