2013-06-26 4 views
1

사용자 정의 글꼴을 설정하고 전체 애플리케이션의 html 텍스트를 설정하기 위해 사용자 정의 텍스트 뷰를 만들었습니다. 내 애플 리케이션은 여러 장소에서 두 가지 기능을 모두 가지고 있기 때문에. 이 경우, 나는 전체 안드로이드 응용 프로그램에 사용자 정의 글꼴을 설정할 수 있습니다. 맞춤 텍스트 뷰를위한 html 텍스트를 적용하지 못했습니다.Html 텍스트 및 사용자 정의 글꼴이있는 사용자 정의 텍스트 뷰 전체 Android 애플리케이션

제한 내가 가지고

  1. 나는 텍스트 뷰를 얻을 setText(Html.fromHtml("Some text"))를 사용하지 않습니다. 그것은 내가 코드에서 이것을 사용하지만 작동하지 않을 때 strings.xml에있는 텍스트를 사용하려고합니다. <string name="my_app"><![CDATA[My Whole <b>app</b>lication]]></string>
  2. 마지막 메소드이므로 사용자 정의 클래스 함수에서 settext를 재정의 할 수 없습니다.
  3. 저는 HTML 텍스트가 거의 100입니다. 코드에서이 문자열을 하드 코딩하고 싶지는 않습니다. 도와주세요.

내 사용자 정의 클래스 :

public class CustomTextView extends TextView { 

    public CustomTextView(Context context) { 
     super(context); 
     // TODO Auto-generated constructor stub 
    } 

    public CustomTextView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     // TODO Auto-generated constructor stub 
    } 

    public CustomTextView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     // TODO Auto-generated constructor stub 
    } 

    @Override 
    public void setTypeface(Typeface tf) { 
     // TODO Auto-generated method stub 
     super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/gothic.ttf")); 
    } 
} 

내 레이아웃 파일 :

내가 출력으로 원하는 내 응용 프로그램 : : 나는 같은 출력을 얻을

<com.views.CustomTextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/my_app" /> 

응용 프로그램 연화

굵게 글씨로 표시된 글

이 방법이 있습니까?

답변

1

SpannbleString을 사용 하시겠습니까? How can I use TypefaceSpan or StyleSpan with a custom Typeface?

Here은 SpannableString에 적용 할 수있는 다른 기간입니다.

public void setSpan(){ 
    Spannable spantext = new SpannableString("My application"); 


    spantext.setSpan(new StyleSpan(Typeface.BOLD), 3//Start of span, 6//End of span, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
    Typeface HelveticaNeueBoldItalic = Typeface.createFromAsset(getActivity().getAssets(), "fonts/HelveticaNeueBoldItalic.ttf"); 
    spantext.setSpan(new CustomTypefaceSpan("", HelveticaNeueBoldItalic), 0, text.lenght(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 



    TextView text; 

    text.setText(spantext); 
} 






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

회신에 감사드립니다 user1888162. 하지만 어떻게 이것을 사용자 정의 텍스트보기에서 사용할 수 있습니까? 그래서 내가 전체 응용 프로그램에서 이것을 사용할 수 있습니다. 고맙습니다. – fargath

+0

감사. 커스텀 뷰 생성자에서이 spannable 문자열을 사용했습니다. 그것은 위대한 작품. – fargath

관련 문제