Skip to content

Support cascading styles #437

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
psegalen opened this issue Nov 27, 2020 · 11 comments
Closed

Support cascading styles #437

psegalen opened this issue Nov 27, 2020 · 11 comments

Comments

@psegalen
Copy link

Hello, I haven't found anywhere any pointer about this particular issue yet: in the HTML code I receive from the server I sometimes have things like that <i><strong>Text</strong></i>
Problem, I have a custom font throughout the app and I haven't found a way yet through tagsStyles to specify a thing like:

i: {
  strong: {
    fontFamily: "MyFont-BoldItalic"
  }
}

Is it event possible?

@jsamr
Copy link
Collaborator

jsamr commented Nov 27, 2020

@psegalen Not at the moment! Cascading styles are complex and not yet supported. It could be a feature of a future release though! With the foundry refactor, that might be more feasible.

@jsamr jsamr changed the title Is it possible to specify a fontFamily for both Bold & Italic? Support cascading styles Nov 27, 2020
@psegalen
Copy link
Author

Thanks for the quick answer!
Since you flagged it as a feature request, I'll let this issue open.

@jsamr
Copy link
Collaborator

jsamr commented Nov 27, 2020

@psegalen If you don't want to specify the font style with the name, and instead use fontFamily: MyFont, check out how to achieve that on android here by declaring a font.xml in your res folder: facebook/react-native#23865
For iOS, I don't remember how to achieve the same result be I remember I did in a past project. Be careful though not to hit this bug facebook/react-native#20398

@psegalen
Copy link
Author

Thanks a lot @jsamr, I'll dig into it!

@psegalen
Copy link
Author

psegalen commented Nov 30, 2020

Thanks again @jsamr, with a little more work it solves my problem!
For the record: I haven't found a way to use this font.xml file on Android from React Native so I ended up overriding the default font for my app.
Also, on iOS it just works out-of-the-box: I just referenced "FontName-Regular" on my HTML body (that I dynamically add if there is none) and the system chooses the right variant (between regular, bold, italic and bold-italic) correctly for each style.

Basically, the problem was coming from the fact that Android did not support applying fontWeight: "bold" and fontStyle: "italic" to my font (it was falling back to Roboto) while iOS did. @jsamr's first link lead me to fix it, thanks again!

@psegalen
Copy link
Author

psegalen commented Dec 1, 2020

Okay, it worked until it was deployed on the store beta program... :'(
I had to scratch my head until I searched inside react-native source code and PRs and finally found what I needed: the ReactFontManager class!
I reverted the default font override and just added ReactFontManager.getInstance().addCustomFont(this, "Srisakdi", R.font.srisakdi); in my app's onCreate!

@jsamr
Copy link
Collaborator

jsamr commented Jan 31, 2021

Tracking this library as candidate to build the feature https://github.com/fb55/css-select

@jsamr
Copy link
Collaborator

jsamr commented Jul 8, 2021

CSS sheets will never be supported per se, because parsing requires creating a syntax tree, which requires a third party library and would most certainly add a lot of overhead.

However, I opened a feature request in our new Canny to convert CSS stylesheets to react-native-render-html compatible code. You can upvote this feature here.. Closing now as feature requests are now handled in this Canny!

@jsamr jsamr closed this as completed Jul 8, 2021
@jsamr
Copy link
Collaborator

jsamr commented Dec 6, 2021

To use XML Fonts, check out this guide: https://stackoverflow.com/a/70247374/2779871

@vishva-shukla
Copy link

How to do it with expo?

@7ammer
Copy link

7ammer commented Jan 23, 2024

@vishva-shukla, For android you can use this package: https://github.com/Armster15/expo-xml-font
I'm unable to get font families working on iOS though :(

Any ideas?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants