2012-11-27 8 views
7

ho provato questo:Imposta carattere e il colore diverso da parte di un TextView

String s = "Some big string" 
SpannableStringBuilder sb = new SpannableStringBuilder(s); 
//normal font for 1st 9 chars 
sb.setSpan(robotoRegular, 0,9,Spannable.SPAN_INCLUSIVE_INCLUSIVE); 
//bold font for rest of the chars 
sb.setSpan(robotoBold, 9,s.length(),Spannable.SPAN_INCLUSIVE_INCLUSIVE); 
//also change color for rest of the chars 
sb.setSpan(new ForegroundColorSpan(Color.BLACK), 9,s.length(),Spannable.SPAN_INCLUSIVE_INCLUSIVE); 
textView.setText(sb); 

Ma questo non ha funzionato.

Richiede solo l'ultimo setSpan, cioè .., il colore del testo viene modificato ma non il carattere.

+0

Come si fa a definire '' robotoRegular' e robotoBold'? –

+0

Class Variables: ** robotoRegular = Typeface.createFromAsset (getAssets(), "font/Roboto-Regular.ttf"); ** –

risposta

28

è necessario utilizzare un TypefaceSpan invece di un Typeface.

Ma poiché si utilizza un carattere personalizzato, è necessario estendere TypefaceSpan.

Verificare questo answer e creare CustomTypefaceSpan classe.

Ora effettuare le seguenti operazioni:

Typeface robotoRegular = Typeface.createFromAsset(getAssets(), "fonts/Roboto-Regular.ttf"); 
Typeface robotoBold = Typeface.createFromAsset(getAssets(), "fonts/Roboto-Bold.ttf"); 

TypefaceSpan robotoRegularSpan = new CustomTypefaceSpan("", robotoRegular); 
TypefaceSpan robotoBoldSpan = new CustomTypefaceSpan("", robotoBold); 

// normal font for 1st 9 chars 
sb.setSpan(robotoRegularSpan, 0, 9, Spannable.SPAN_INCLUSIVE_INCLUSIVE); 
// bold font for rest of the chars 
sb.setSpan(robotoBoldSpan, 9, s.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE); 
// also change color for rest of the chars 
sb.setSpan(new ForegroundColorSpan(Color.BLUE), 9, s.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE); 
textView.setText(sb); 
0

controllo di questo codice: -

protected void onDraw(Canvas canvas) { 

     Paint circlePaint = new Paint(); 
     Typeface mType = Typeface.create(Typeface.SANS_SERIF, Typeface.ITALIC); 
     circlePaint.setTextSize(25); 
     circlePaint.setColor(Color.RED); 
     circlePaint.setTypeface(mType); 
     canvas.drawText("Default Typeface", 50, 100, circlePaint); 
     // 
     circlePaint.setFlags(Paint.UNDERLINE_TEXT_FLAG); 
     circlePaint.setColor(Color.YELLOW); 
     canvas.drawText("Underline Text Flag", 50, 120, circlePaint); 
     // 
     circlePaint.setFlags(Paint.STRIKE_THRU_TEXT_FLAG); 
     circlePaint.setColor(Color.GREEN); 
     canvas.drawText("Strike Thru Text Flag", 50, 140, circlePaint); 
     // 
     circlePaint.setFlags(Paint.ANTI_ALIAS_FLAG); 
     circlePaint.setColor(Color.WHITE); 
     canvas.drawText("Anti Alias Flag", 50, 160, circlePaint); 
     // 
     circlePaint.setFlags(Paint.DEV_KERN_TEXT_FLAG); 
     circlePaint.setColor(Color.WHITE); 
     canvas.drawText("Dev Kern Text Flag", 50, 180, circlePaint); 
     // 
     circlePaint.setFlags(Paint.FAKE_BOLD_TEXT_FLAG); 
     circlePaint.setColor(Color.CYAN); 
     canvas.drawText("Fake Bold Text Flag", 50, 200, circlePaint); 
    } 
0

Per peaple che preferiscono fare le classi utils per questo:

public static SpannableStringBuilder makeTextBold (Activity activity, String string, int fromCharIndex, int toCharIndex) { 
    return makeTextBold(activity, new SpannableStringBuilder(string), fromCharIndex, toCharIndex); 
} 

public static SpannableStringBuilder makeTextBold (Activity activity, SpannableStringBuilder string, int fromCharIndex, int toCharIndex) { 
    SpannableStringBuilder sb = new SpannableStringBuilder(string); 
    Typeface bold = Typeface.createFromAsset(activity.getAssets(), "fonts/NexaBold.ttf"); 
    TypefaceSpan robotoBoldSpan = new CustomTypefaceSpan("", bold); 
    sb.setSpan(robotoBoldSpan, fromCharIndex, toCharIndex, Spannable.SPAN_INCLUSIVE_INCLUSIVE); 
    return sb; 
} 

public static SpannableStringBuilder colorText (int resourceId, String string, Activity activity, int fromCharIndex, int toCharIndex) { 
    return colorText(resourceId, new SpannableStringBuilder(string), activity, fromCharIndex, toCharIndex); 
} 

public static SpannableStringBuilder colorText (int resourceId, SpannableStringBuilder sb, Activity activity, int fromCharIndex, int toCharIndex) { 
    sb.setSpan(new ForegroundColorSpan(activity.getResources().getColor(resourceId)), fromCharIndex, toCharIndex, Spannable.SPAN_INCLUSIVE_INCLUSIVE); 
    return sb; 
}