2016-11-28 18 views
1

제목에서 알 수 있듯이, Assets 내부에 사용자 정의 글꼴이있는 TypefaceSpan 객체를 사용해야하지만 올바른 방법을 찾을 수 없습니다. 글꼴의 파일이 "HelveticaNeueLTCom-BdCn.ttf"Xamarin 안드로이드 TypefaceSpan Assets 폴더에 사용자 정의 글꼴이 있습니다.

입니다 이들은 내 나를 위해 작동하지 않았다 두 시도 :

// first attempt 
var textViewTitle = new TextView(Context); 
var span = new SpannableString("MyLongTitle"); 
span.SetSpan(new TypefaceSpan("HelveticaNeueLTCom-BdCn.ttf"), 0, 5, SpanTypes.ExclusiveExclusive); 
textViewTitle.TextFormatted = span; 


// second attempt 
var textViewTitle = new TextView(Context); 
var span = new SpannableString("MyLongTitle"); 
span.SetSpan(new Typeface(Typeface.CreateFromAsset(Context.Assets, "fonts/HelveticaNeueLTCom-BdCn.ttf")), 0, 5, SpanTypes.ExclusiveExclusive); 
textViewTitle.TextFormatted = span; 

누구나 몇 가지 힌트이나 조언이있다?

답변

1

거의 1 년 후 같은 문제에 직면했으며이를 달성하는 방법을 발견했습니다.

나는 Xamarin 포럼 here에 같은 질문을 게시했지만 귀하가 답변에 기입 한 링크가 깨졌습니다. 그러나 그것이 나를 도왔을 때 당신을 도운 것은 this post라고 생각합니다. 그래서 여기에가는 길.

먼저 SpannableString

var spannableString = new SpannableString("Anything to write with a special font"); 
spannableString.SetSpan(new CustomTypefaceSpan(Typeface.CreateFromAsset(Assets, "HelveticaNeueLTCom-BdCn.ttf"), 0, 7, SpanTypes.InclusiveInclusive); 
textView.TextFormatted = spannableString; 
에 범위를 추가하려면이 CustomTypefaceSpan을 사용하여 다음이

using System; 
using Android.OS; 
using Android.Runtime; 
using Android.Text.Style; 
using Android.Graphics; 
using Android.Text; 

namespace Droid.Spans 
{ 
    public class CustomTypefaceSpan : TypefaceSpan 
    { 
    private readonly Typeface _typeface; 

    public CustomTypefaceSpan(Typeface typeface) 
    : base(string.Empty) 
    { 
     _typeface = typeface; 
    } 
    public CustomTypefaceSpan(IntPtr javaReference, JniHandleOwnership transfer) 
     : base(javaReference, transfer) 
    { 
    } 
    public CustomTypefaceSpan(Parcel src) 
    : base(src) 
    { 
    } 
    public CustomTypefaceSpan(string family) 
    : base(family) 
    { 
    } 
    public override void UpdateDrawState(TextPaint ds) 
    { 
     ApplyTypeface(ds, _typeface); 
    } 
    public override void UpdateMeasureState(TextPaint paint) 
    { 
     ApplyTypeface(paint, _typeface); 
    } 
    private static void ApplyTypeface(Paint paint, Typeface tf) 
    { 
     paint.SetTypeface(tf); 
    } 
    } 
} 

같은 사용자 정의 TypefaceSpan을 만들