InflationX / Calligraphy

Custom fonts in Android the easy way...
Apache License 2.0
1.09k stars 83 forks source link

Any way to have multiple fonts for multiple textStyles? #15

Open MHKalantarian opened 5 years ago

MHKalantarian commented 5 years ago

I want to use separate fonts for BOLD & Italic textStyle. Is there any way to do this with the library? or any kind of workaround? I've tried writing custom classes for some views but Calligraphy overrides them :(

npag commented 5 years ago

I'd be also interested in this.

jeffypooo commented 5 years ago

We did something similar in our app using custom text appearance styles. Not exactly what you want, but works well enough.

MHKalantarian commented 5 years ago

We did something similar in our app using custom text appearance styles. Not exactly what you want, but works well enough.

@masterjefferson May I have the custom view/class?

basmaessen commented 5 years ago

@MHKalantarian How did you solved this issue?

MHKalantarian commented 5 years ago

@basmaessen Custom class for every single view and not binding calligraphy on some pages!

basmaessen commented 5 years ago

@MHKalantarian And what if you have multiple fonts for 1 view

MHKalantarian commented 5 years ago

@basmaessen If you mean multiple fonts for 1 type of view you'll have to use a custom attribute or get the font in constructor but if you mean multiple fonts for 1 textfield

Multiple Typeface's per TextView / Spannables It is possible to use multiple Typefaces inside a TextView, this isn't new concept to Android.

This could be achieved using something like the following code.

SpannableStringBuilder sBuilder = new SpannableStringBuilder(); sBuilder.append("Hello!") // Bold this .append("I use Calligraphy"); // Default TextView font. // Create the Typeface you want to apply to certain text CalligraphyTypefaceSpan typefaceSpan = new CalligraphyTypefaceSpan(TypefaceUtils.load(getAssets(), "fonts/Roboto-Bold.ttf")); // Apply typeface to the Spannable 0 - 6 "Hello!" This can of course by dynamic. sBuilder.setSpan(typefaceSpan, 0, 6, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); setText(sBuilder, TextView.BufferType.SPANNABLE);

basmaessen commented 5 years ago

@MHKalantarian Do you have an example of a custom view for a TextView or Button or something?

MHKalantarian commented 5 years ago

@basmaessen a simple example for a textview

public class TextViewWithFont extends TextView {

    public TextViewWithFont(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.setTypeface(exampleTypeFace);
    }

    public TextViewWithFont(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.setTypeface(exampleTypeFace);
    }

    public TextViewWithFont(Context context) {
        super(context);
        this.setTypeface(exampleTypeFace);
    }

}