CopyPastor

Detecting plagiarism made easy.

Score: 0.8160037994384766; Reported for: String similarity Open both answers

Possible Plagiarism

Plagiarized on 2014-06-04
by Farouk Touzi

Original Post

Original - Posted on 2013-03-03
by twaddington



            
Present in both answers; Present only in the new answer; Present only in the old answer;

We have to use a custom TypefaceSpan class.
SpannableString s = new SpannableString("My Title"); s.setSpan(new TypefaceSpan(this, "fonts/khmerbibleregular.ttf"), 0, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); // Update the action bar title with the TypefaceSpan instance ActionBar actionBar = getActionBar(); actionBar.setTitle(s);

The custom TypefaceSpan class is passed your Activity context and the name of a typeface in your Assets/fonts directory. It loads the file and caches a new Typeface instance in memory. The complete implementation of TypefaceSpan is surprisingly simple:
/** * Style a {@link Spannable} with a custom {@link Typeface}. * * @author Tristan Waddington */ public class TypefaceSpan extends MetricAffectingSpan { /** An <code>LruCache</code> for previously loaded typefaces. */ private static LruCache<String, Typeface> sTypefaceCache = new LruCache<String, Typeface>(12); private Typeface mTypeface; /** * Load the {@link Typeface} and apply to a {@link Spannable}. */ public TypefaceSpan(Context context, String typefaceName) { mTypeface = sTypefaceCache.get(typefaceName); if (mTypeface == null) { mTypeface = Typeface.createFromAsset(context.getApplicationContext() .getAssets(), String.format("fonts/%s", typefaceName)); // Cache the loaded Typeface sTypefaceCache.put(typefaceName, mTypeface); } } @Override public void updateMeasureState(TextPaint p) { p.setTypeface(mTypeface); // Note: This flag is required for proper typeface rendering p.setFlags(p.getFlags() | Paint.SUBPIXEL_TEXT_FLAG); } @Override public void updateDrawState(TextPaint tp) { tp.setTypeface(mTypeface); // Note: This flag is required for proper typeface rendering tp.setFlags(tp.getFlags() | Paint.SUBPIXEL_TEXT_FLAG); } }
Simply copy the above class into your project and implement it in your code.
You can do this using a custom `TypefaceSpan` class. It's superior to the `customView` approach indicated above because it doesn't break when using other Action Bar elements like expanding action views.
The use of such a class would look something like this:
SpannableString s = new SpannableString("My Title"); s.setSpan(new TypefaceSpan(this, "MyTypeface.otf"), 0, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); // Update the action bar title with the TypefaceSpan instance ActionBar actionBar = getActionBar(); actionBar.setTitle(s);
The custom `TypefaceSpan` class is passed your Activity context and the name of a typeface in your `assets/fonts` directory. It loads the file and caches a new `Typeface` instance in memory. The complete implementation of `TypefaceSpan` is surprisingly simple:
/** * Style a {@link Spannable} with a custom {@link Typeface}. * * @author Tristan Waddington */ public class TypefaceSpan extends MetricAffectingSpan { /** An <code>LruCache</code> for previously loaded typefaces. */ private static LruCache<String, Typeface> sTypefaceCache = new LruCache<String, Typeface>(12); private Typeface mTypeface; /** * Load the {@link Typeface} and apply to a {@link Spannable}. */ public TypefaceSpan(Context context, String typefaceName) { mTypeface = sTypefaceCache.get(typefaceName); if (mTypeface == null) { mTypeface = Typeface.createFromAsset(context.getApplicationContext() .getAssets(), String.format("fonts/%s", typefaceName)); // Cache the loaded Typeface sTypefaceCache.put(typefaceName, mTypeface); } } @Override public void updateMeasureState(TextPaint p) { p.setTypeface(mTypeface);
// Note: This flag is required for proper typeface rendering p.setFlags(p.getFlags() | Paint.SUBPIXEL_TEXT_FLAG); } @Override public void updateDrawState(TextPaint tp) { tp.setTypeface(mTypeface);
// Note: This flag is required for proper typeface rendering tp.setFlags(tp.getFlags() | Paint.SUBPIXEL_TEXT_FLAG); } }
Simply copy the above class into your project and implement it in your activity's `onCreate` method as shown above.

        
Present in both answers; Present only in the new answer; Present only in the old answer;