2012-03-08 33 views
25

我想將兩種不同的字體樣式應用於單個TextView中的文本。將兩種不同的字體樣式應用於TextView

我的情況與Android - two sentences, two styles, one TextView相同。唯一的區別是我想在整個文本上設置一個自定義字體。我已經將Helvetica字體作爲資源添加到了我的項目中,並且希望將該字體應用於TextView,而文本的第一部分將爲Helvetica BOLD,其餘部分爲Helvetica NORMAL。任何建議如何完成?

以下格式需要的文本。具有不同風格和單個文本視圖的自定義文本。

enter image description here

+0

,你爲什麼不能使用具有不同fontstyles兩種不同的TextView。 – 2012-03-08 14:17:49

+0

@SenthilMg你可以在問題的參考鏈接上看到原因。它不會格式化文本,因爲它是必需的 – rizzz86 2012-03-08 14:33:49

+0

,我猜想通過使用正則表達式將完整字符串拆分爲兩個:並設置在不同的文本視圖爲例如:String text = AAAA:BBBBB,String [] splitText = text.split 「:」); tv1.setText(splitText [0]); tv2.setText(splitText [1]); – 2012-03-08 14:39:15

回答

69

的方法之一是擴大TypefaceSpan

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

然後,當你想使用兩個不同的字體撥打:

String firstWord = "first "; 
String secondWord = "second"; 

// Create a new spannable with the two strings 
Spannable spannable = new SpannableString(firstWord+secondWord); 

// Set the custom typeface to span over a section of the spannable object 
spannable.setSpan(new CustomTypefaceSpan("sans-serif",CUSTOM_TYPEFACE), 0, firstWord.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
spannable.setSpan(new CustomTypefaceSpan("sans-serif",SECOND_CUSTOM_TYPEFACE), firstWord.length(), firstWord.length() + secondWord.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 

// Set the text of a textView with the spannable object 
textView.setText(spannable); 
+0

感謝勞倫斯的答案。我正在嘗試這一個,讓我們看看會有什麼結果。也讓我試着理解你所提供的代碼片段中發生了什麼)) – rizzz86 2012-03-08 16:03:29

+0

沒問題,我對第二塊代碼中的擴展TypefaceSpan的名稱做了小的改動。 在實現方面,CustomTypefaceSpan已準備就緒,您只需將其添加到您的項目即可。第二塊代碼就是你需要編輯的東西。 – Ljdawson 2012-03-08 17:11:33

+2

勞倫斯爲我工作。這個問題似乎很小,但解決方案有點複雜。感謝真正節省了我的時間的幫助。 – rizzz86 2012-03-09 07:13:18

-2

沒有嘗試申請spanable文本之前在TextView中設置自定義字體?

Typeface face = Typeface.createFromAsset(ctx.getAssets(), "fonts/boost.ttf") 
TextView tv = (TextView) ctx.findViewById(id); 
tv.setTypeface(face); 

,然後應用SpannableStringBuilder從鏈接的問題 - 我只是假設,如果你的TTF支持「正常」和「大膽」,將相應地呈現:)

+0

我也嘗試過這種方式。但它不適用。 – rizzz86 2012-03-08 14:30:25

0

您可以創建自定義視圖並呈現與兩文本使用塗料對象canvas.drawText方法來做到這一點

+0

我會嘗試canvas.drawText並檢查它是如何工作的 – rizzz86 2012-03-08 14:35:50

2

這可能工作 - 創建自己的自定義的TextView,然後在它的一個組成部分使用StyleSpan:

public class CustomTextView extends TextView { 

    public CustomTextView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    @Override 
    public void setTypeface(Typeface tf, int style) { 
     if (style == 1){ 
      //replace "HelveticaBOLD.otf" with the name of your bold font 
      tf = Typeface.createFromAsset(getContext().getApplicationContext().getAssets(), "HelveticaBOLD.otf"); 
     }else{ 
      //replace "HelveticaNORMAL.otf" with the name of your normal font 
      tf = Typeface.createFromAsset(getContext().getApplicationContext().getAssets(), "HelveticaNORMAL.otf"); 
     } 
     super.setTypeface(tf, 0); 
    } 
} 

而且那麼你可以做這樣的事情:

int index1 = 0; //wherever bold should begin 
int index2 = 5; //wherever bold should end 

Spannable span = new SpannableString("some string"); 
span.setSpan(new StyleSpan(Typeface.BOLD),index1, index2,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 

((CustomTextView)findViewById(R.id.yourTextView)).setText(span); 
+0

這一個沒有爲我工作。無論如何感謝您的答案。 – rizzz86 2012-03-08 15:58:05

+0

正是我在找的,謝謝 – Ash 2015-05-05 22:19:08

2

這是一個更直接的解決方案,你可以使用HTML在相同的TextView上設置不同的樣式。

例如:

// Styled label 
String styledText = "<big><b><font color='#333333'>title</font></b></big> <small><b><font color='#CC5490'>subtitle</font></b></small>"; 

// Apply the styled label on the TextView 
textView.setText(Html.fromHtml(styledText)); 

您需要以下導入:

import android.text.Html; 
0

我用這種方法

class FooClass { 
    void Foo() { 
     textViewSetText(R.id.photo_title, "Title: ", photo.getTitle()); 
     textViewSetText(R.id.photo_tags, "Tags: ", photo.getTags()); 
     textViewSetText(R.id.photo_author, "Author: ", photo.getAuthor()); 
    } 

    private void textViewSetText(int resourceId, String prefix, String text) { 
     TextView textView = (TextView) findViewById(resourceId); 
     SpannableString styledText = new SpannableString(prefix); 
     styledText.setSpan(new StyleSpan(Typeface.BOLD), 0, prefix.length(), 0); 
     textView.setText(null); 
     textView.append(styledText); 
     textView.append(text); 
    } 
} 

正如你可以看到 「作者」,「標題「和」標籤「設置爲BOLD

Screenshot http://image.prntscr.com/image/dd60bb4a335445c2adca8a4596d7fb32.png

相關問題