Calligraphy icon indicating copy to clipboard operation
Calligraphy copied to clipboard

Support default font for bold/italic style?

Open akhy opened this issue 11 years ago • 20 comments

I don't know if this feature has been implemented or not. I just recently applying OpenSans-Regular.ttf as global default, then all bold TextView turned to normal (non-bold style). I wish there is a built-in and clean way to provide default bold font file (e.g. OpenSans-Bold.ttf) when used by TextView, perhaps by checking it in TextView#getTypeface().isBold().

I think this feature will come in handy because it will eliminate the need to apply custom style one by one to all bold/italic texts.

Thanks

akhy avatar Aug 08 '14 00:08 akhy

@akhyrul Yes I have been researching how to do this. I expect this will be a 2.x feature.

For example you would define:

fonts/
  OpenSans-Regular.ttf
  OpenSans-Bold.ttf
  OpenSans-Italic.ttf

Which would scope to something like:

<TextView
  fontPath="fonts/OpenSans-*" 
  textStyle="bold"/>

Which would inherently lookup OpenSans-Bold if it can't find it then apply fake bold etc.

This would also be applicable to strings <string>Hello <b>World</b></string> which would try to find the correct bold version instead of applying fake bold.

This is what you're thinking right?

chrisjenx avatar Aug 08 '14 11:08 chrisjenx

Yes, exactly!

Hopefully it will be implemented soon :+1:

akhy avatar Aug 10 '14 14:08 akhy

@chrisjenx interesting aproach. I suggest that you also make the textStyle a custom attribute (non android:), so we could use any name. Eg: MyFont-bold.ttf and MyFont-bolder.ttf

jonasfa avatar Aug 14 '14 22:08 jonasfa

Yeah I'm not sure yet I'm open to suggestions though. On 14 Aug 2014 19:37, "Jonas Alves" [email protected] wrote:

@chrisjenx https://github.com/chrisjenx interesting aproach. I suggest that you also make the textStyle a custom attribute (non android:), so we could use any name. Eg: MyFont-bold.ttf and MyFont-bold_er_.ttf

— Reply to this email directly or view it on GitHub https://github.com/chrisjenx/Calligraphy/issues/66#issuecomment-52251585 .

chrisjenx avatar Aug 14 '14 22:08 chrisjenx

I have a temporary fix for it.

Just integrate Calligraphy normally at first.

Then, do a search and replace on:

Android:textStyle="bold"

And replace/expand it into:

Android:textStyle="bold" fontPath="@string/font_path_bold"

Where font_path_bold is a string resource item referencing the path of your bold font. This way, there's a safe fallback in case you want to integrate Calligraphy and a normal styling if you disable it temporarily.

Thanks for the library!

OneManMobile avatar Sep 03 '14 08:09 OneManMobile

@OneManMobile rather than manual search-and-replace, currently I'm using this cleaner temporary workaround by using styles:

<!-- styles.xml -->
<style name="AppTheme.TextAppearance" parent="android:TextAppearance">
        <item name="fontPath">fonts/OpenSans-Regular.ttf</item>
</style>
<style name="AppTheme.TextAppearance.Bold" parent="AppTheme.TextAppearance">
        <item name="fontPath">fonts/OpenSans-Bold.ttf</item>
        <item name="android:textStyle">bold</item>
</style>

So I can use it in any layouts like this:

<TextView
    style="@style/WhateverStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/title"
    android:textAppearance="@style/AppTheme.TextAppearance.Bold"/>

I purposely put <item name="android:textStyle">bold</item> to make sure preview in Android Studio shows correct bold style.

Hopefully there will be a cleaner way in 2.x :thumbsup:

Thanks

akhy avatar Sep 04 '14 04:09 akhy

+1

makovkastar avatar Sep 09 '14 08:09 makovkastar

@akhyrul thanks! That's good solution.

samdark avatar Sep 17 '14 15:09 samdark

I'd like to propose the solution used by DTCoreText, a Cocoapod for iOS with custom font family ability. It allows defining any font family regardless of filename or internal font name.

For example, here is the Lato Light family using Lato Regular for bold.

         <dict>
            <key>Bold</key>
            <false/>
            <key>FontFamily</key>
            <string>Lato-Light</string>
            <key>Italic</key>
            <false/>
            <key>OverrideFontName</key>
            <string>Lato-Light</string>
        </dict>
        <dict>
            <key>Bold</key>
            <true/>
            <key>FontFamily</key>
            <string>Lato-Light</string>
            <key>Italic</key>
            <false/>
            <key>OverrideFontName</key>
            <string>Lato-Regular</string>
        </dict>
        <dict>
            <key>Bold</key>
            <true/>
            <key>FontFamily</key>
            <string>Lato-Light</string>
            <key>Italic</key>
            <true/>
            <key>OverrideFontName</key>
            <string>Lato-Italic</string>
        </dict>
        <dict>
            <key>Bold</key>
            <false/>
            <key>FontFamily</key>
            <string>Lato-Light</string>
            <key>Italic</key>
            <true/>
            <key>OverrideFontName</key>
            <string>Lato-LightItalic</string>
        </dict>

Then set the TextView custom font to Lato-Light and any bold is handled through the font-family map.

Complete DTCoreText demo source

doublerebel avatar Oct 19 '14 20:10 doublerebel

Any chance to see it soon? Title and buttons for material appcompat AlertDialog aren't bold anymore when I inject context with Calligraphy.

vyndor avatar Sep 26 '15 23:09 vyndor

Thanks because they will be inflated using your default font.

Soon? unlikely, this is something I don't want to hack together.

You can override default styles which would let you set the title correctly etc.

On Sun, 27 Sep 2015 00:56 vyndor [email protected] wrote:

Any chance to see it soon? Title and buttons for material appcompat AlertDialog aren't bold anymore when I inject context with Calligraphy.

— Reply to this email directly or view it on GitHub https://github.com/chrisjenx/Calligraphy/issues/66#issuecomment-143506071 .

chrisjenx avatar Sep 27 '15 10:09 chrisjenx

Any improvements on this thread?

I had same issue, and trying to find which default styles i need to override was kinda painful for AlertDialog... So here how you can do it:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
   ...
    <item name="alertDialogTheme">@style/AlertDialogStyle</item>
</style>

<style name="AlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:windowTitleStyle">@style/DialogWindowTitleStyle</item>
</style>

<style name="DialogWindowTitleStyle" parent="@android:style/TextAppearance.DialogWindowTitle">
    <item name="android:textAppearance">@style/CustomFontStyle.Bold</item>
</style>

 <style name="CustomFontStyle" parent="TextAppearance.AppCompat" />

 <style name="CustomFontStyle.Bold" parent="TextAppearance.AppCompat" >
     <item name="fontPath">fonts/bold_font.ttf</item>
 </style>

yayaa avatar Mar 31 '16 13:03 yayaa

Was added any solution to support bold style?

ramona-cristea avatar Aug 18 '16 22:08 ramona-cristea

I'm currently discussing this with some other engineers, no ETA yet. Sorry.

On Thu, 18 Aug 2016, 16:06 ramona-cristea, [email protected] wrote:

Was added any solution to support bold style?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/chrisjenx/Calligraphy/issues/66#issuecomment-240871831, or mute the thread https://github.com/notifications/unsubscribe-auth/ABHRscYqrOqB4XOKHjKtrDhWb-J8v0gPks5qhNdMgaJpZM4CVUUG .

chrisjenx avatar Aug 18 '16 22:08 chrisjenx

For some reason using the bold and italics html tags works with Calligraphy fonts in TextViews when used like this:

someTextView.setText(Html.fromHtml("<b>something in bold</b> <i>something in italics</i>"));

Even seems to work with some very unusual fonts I tried (Blzee, Hemi-Head, Noteworthy, World of Water), not sure why...

nirslsk avatar Sep 28 '16 20:09 nirslsk

Yeah this is called text paint bold and fake italic. It works, but not true bold and italic, if it works for you go for it

On Wed, 28 Sep 2016, 14:46 nirslsk, [email protected] wrote:

For some reason using the * and tags works with Calligraphy fonts in TextViews when used like this:*

  • someTextView.setText(Html.fromHtml("something in bold something in italics")); *

Even seems to work with some very unusual fonts I tried (Blzee, Hemi-Head, Noteworthy, World of Water), not sure why...

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/chrisjenx/Calligraphy/issues/66#issuecomment-250294431, or mute the thread https://github.com/notifications/unsubscribe-auth/ABHRsUzLxYQNEtZkik9U_bjVzWqzopRQks5qutIkgaJpZM4CVUUG .

chrisjenx avatar Nov 05 '16 16:11 chrisjenx

Is anyone working on a solution for this?

A-w-K avatar Jun 27 '17 09:06 A-w-K

@A-w-K not only more, this lib is deprecated in favour of https://github.com/InflationX and even that will get depreciated when the new support lib is released

chrisjenx avatar Jun 28 '17 10:06 chrisjenx

@akhyrul Yes I have been researching how to do this. I expect this will be a 2.x feature.

For example you would define:

fonts/
  OpenSans-Regular.ttf
  OpenSans-Bold.ttf
  OpenSans-Italic.ttf

Which would scope to something like:

<TextView
  fontPath="fonts/OpenSans-*" 
  textStyle="bold"/>

Which would inherently lookup OpenSans-Bold if it can't find it then apply fake bold etc.

This would also be applicable to strings <string>Hello <b>World</b></string> which would try to find the correct bold version instead of applying fake bold.

This is what you're thinking right?

Is this implemented in Calligraphy3 ?

omidraha avatar Nov 28 '19 13:11 omidraha

It's not, but we'll take prs for calligraphy 3 as that supports Android x where as this doesn't.

Lemme know if you open a PR over their so I can review.

Thanks!

On Thu, Nov 28, 2019, 6:55 AM Omid Raha [email protected] wrote:

@akhyrul https://github.com/akhyrul Yes I have been researching how to do this. I expect this will be a 2.x feature.

For example you would define:

fonts/ OpenSans-Regular.ttf OpenSans-Bold.ttf OpenSans-Italic.ttf

Which would scope to something like:

<TextView fontPath="fonts/OpenSans-*" textStyle="bold"/>

Which would inherently lookup OpenSans-Bold if it can't find it then apply fake bold etc.

This would also be applicable to strings Hello World which would try to find the correct bold version instead of applying fake bold.

This is what you're thinking right?

Is this implemented in Calligraphy3 https://github.com/InflationX/Calligraphy ?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/chrisjenx/Calligraphy/issues/66?email_source=notifications&email_token=AAI5DMPRFGDEEATBDSQAFATQV7EV7A5CNFSM4ASVIUDKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEFMVRNQ#issuecomment-559503542, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAI5DMNJVA3BCFF2DQX7Y6DQV7EV7ANCNFSM4ASVIUDA .

chrisjenx avatar Nov 28 '19 17:11 chrisjenx