How to create a custom Text - react native












0














I completed my project now I want to set my custom font to all Text component.



I think the best way is to create a custom Text component and replace it with default Text of react-native.



now how can I creating a custom Text component with default style?










share|improve this question






















  • Create a component <CustomText> which reuses native Text component with customized style?
    – meta4
    Nov 12 at 12:36










  • Yes. I want to replace this component with Text react-native componenet.
    – S.M_Emamian
    Nov 12 at 12:39
















0














I completed my project now I want to set my custom font to all Text component.



I think the best way is to create a custom Text component and replace it with default Text of react-native.



now how can I creating a custom Text component with default style?










share|improve this question






















  • Create a component <CustomText> which reuses native Text component with customized style?
    – meta4
    Nov 12 at 12:36










  • Yes. I want to replace this component with Text react-native componenet.
    – S.M_Emamian
    Nov 12 at 12:39














0












0








0







I completed my project now I want to set my custom font to all Text component.



I think the best way is to create a custom Text component and replace it with default Text of react-native.



now how can I creating a custom Text component with default style?










share|improve this question













I completed my project now I want to set my custom font to all Text component.



I think the best way is to create a custom Text component and replace it with default Text of react-native.



now how can I creating a custom Text component with default style?







javascript react-native






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 at 12:34









S.M_Emamian

5,969653104




5,969653104












  • Create a component <CustomText> which reuses native Text component with customized style?
    – meta4
    Nov 12 at 12:36










  • Yes. I want to replace this component with Text react-native componenet.
    – S.M_Emamian
    Nov 12 at 12:39


















  • Create a component <CustomText> which reuses native Text component with customized style?
    – meta4
    Nov 12 at 12:36










  • Yes. I want to replace this component with Text react-native componenet.
    – S.M_Emamian
    Nov 12 at 12:39
















Create a component <CustomText> which reuses native Text component with customized style?
– meta4
Nov 12 at 12:36




Create a component <CustomText> which reuses native Text component with customized style?
– meta4
Nov 12 at 12:36












Yes. I want to replace this component with Text react-native componenet.
– S.M_Emamian
Nov 12 at 12:39




Yes. I want to replace this component with Text react-native componenet.
– S.M_Emamian
Nov 12 at 12:39












3 Answers
3






active

oldest

votes


















1














To achieve that, you need to have a react native component that is configurable via style or other properties once instantiated.



For example you can have your custom react native component CustomText like this:



// CustomText.js    
import React from 'react';
import {
Text,
StyleSheet,
} from 'react-native';

export default class CustomText extends React.Component {
constructor(props) {
super(props);
}

render() {
return (
<Text style={[styles.defaultStyle, this.props.style]}>
{this.props.children}
</Text>
);
}
}

const styles = StyleSheet.create({
// ... add your default style here
defaultStyle: {
},
});


And then on your main component you import and call that custom component, something like this:



import CustomText from './CustomText';
//... other imports go here.

// in the render method you call your CustomText component.
render(){

//...
<CustomText style={{ fontWeight: 60, }}>
This is custom Text
</CustomText>
}


Note: If you want only to change the style I think @Yanush solution is the best for that case.



I hope this is helpful.






share|improve this answer





























    1














    this guide will help you on how to apply custom fonts, I have been using the method in my apps.
    To create a custom text component



    export default Text = (props)=>{
    return(
    <Text style={[styles.defaultStyles,props.style]}>{props.children}</Text>
    )
    }


    Now in all the files where you have used Text from react native remove import from react native and add



    import Text from './path/to/component'





    share|improve this answer































      0














      I would suggest using a style instead of a custom component but it's up to you.
      In my project I have created a file named "commonStyles.js" that looks like this:



      export default StyleSheet.create({
      textTitle: {
      fontSize: 20,
      color: '#dddddd',
      fontFamily: 'YourCustomFont',
      },
      });


      then I'm importing this file wherever needed using:



      import stylesCommon from './styles/stylesCommon';


      and each text that needs to be changed should look like this:



      <Text style={stylesCommon.textTitle}>
      This is my title
      </Text>





      share|improve this answer





















        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53262325%2fhow-to-create-a-custom-text-react-native%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        1














        To achieve that, you need to have a react native component that is configurable via style or other properties once instantiated.



        For example you can have your custom react native component CustomText like this:



        // CustomText.js    
        import React from 'react';
        import {
        Text,
        StyleSheet,
        } from 'react-native';

        export default class CustomText extends React.Component {
        constructor(props) {
        super(props);
        }

        render() {
        return (
        <Text style={[styles.defaultStyle, this.props.style]}>
        {this.props.children}
        </Text>
        );
        }
        }

        const styles = StyleSheet.create({
        // ... add your default style here
        defaultStyle: {
        },
        });


        And then on your main component you import and call that custom component, something like this:



        import CustomText from './CustomText';
        //... other imports go here.

        // in the render method you call your CustomText component.
        render(){

        //...
        <CustomText style={{ fontWeight: 60, }}>
        This is custom Text
        </CustomText>
        }


        Note: If you want only to change the style I think @Yanush solution is the best for that case.



        I hope this is helpful.






        share|improve this answer


























          1














          To achieve that, you need to have a react native component that is configurable via style or other properties once instantiated.



          For example you can have your custom react native component CustomText like this:



          // CustomText.js    
          import React from 'react';
          import {
          Text,
          StyleSheet,
          } from 'react-native';

          export default class CustomText extends React.Component {
          constructor(props) {
          super(props);
          }

          render() {
          return (
          <Text style={[styles.defaultStyle, this.props.style]}>
          {this.props.children}
          </Text>
          );
          }
          }

          const styles = StyleSheet.create({
          // ... add your default style here
          defaultStyle: {
          },
          });


          And then on your main component you import and call that custom component, something like this:



          import CustomText from './CustomText';
          //... other imports go here.

          // in the render method you call your CustomText component.
          render(){

          //...
          <CustomText style={{ fontWeight: 60, }}>
          This is custom Text
          </CustomText>
          }


          Note: If you want only to change the style I think @Yanush solution is the best for that case.



          I hope this is helpful.






          share|improve this answer
























            1












            1








            1






            To achieve that, you need to have a react native component that is configurable via style or other properties once instantiated.



            For example you can have your custom react native component CustomText like this:



            // CustomText.js    
            import React from 'react';
            import {
            Text,
            StyleSheet,
            } from 'react-native';

            export default class CustomText extends React.Component {
            constructor(props) {
            super(props);
            }

            render() {
            return (
            <Text style={[styles.defaultStyle, this.props.style]}>
            {this.props.children}
            </Text>
            );
            }
            }

            const styles = StyleSheet.create({
            // ... add your default style here
            defaultStyle: {
            },
            });


            And then on your main component you import and call that custom component, something like this:



            import CustomText from './CustomText';
            //... other imports go here.

            // in the render method you call your CustomText component.
            render(){

            //...
            <CustomText style={{ fontWeight: 60, }}>
            This is custom Text
            </CustomText>
            }


            Note: If you want only to change the style I think @Yanush solution is the best for that case.



            I hope this is helpful.






            share|improve this answer












            To achieve that, you need to have a react native component that is configurable via style or other properties once instantiated.



            For example you can have your custom react native component CustomText like this:



            // CustomText.js    
            import React from 'react';
            import {
            Text,
            StyleSheet,
            } from 'react-native';

            export default class CustomText extends React.Component {
            constructor(props) {
            super(props);
            }

            render() {
            return (
            <Text style={[styles.defaultStyle, this.props.style]}>
            {this.props.children}
            </Text>
            );
            }
            }

            const styles = StyleSheet.create({
            // ... add your default style here
            defaultStyle: {
            },
            });


            And then on your main component you import and call that custom component, something like this:



            import CustomText from './CustomText';
            //... other imports go here.

            // in the render method you call your CustomText component.
            render(){

            //...
            <CustomText style={{ fontWeight: 60, }}>
            This is custom Text
            </CustomText>
            }


            Note: If you want only to change the style I think @Yanush solution is the best for that case.



            I hope this is helpful.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 12 at 13:28









            ismnoiet

            2,8901625




            2,8901625

























                1














                this guide will help you on how to apply custom fonts, I have been using the method in my apps.
                To create a custom text component



                export default Text = (props)=>{
                return(
                <Text style={[styles.defaultStyles,props.style]}>{props.children}</Text>
                )
                }


                Now in all the files where you have used Text from react native remove import from react native and add



                import Text from './path/to/component'





                share|improve this answer




























                  1














                  this guide will help you on how to apply custom fonts, I have been using the method in my apps.
                  To create a custom text component



                  export default Text = (props)=>{
                  return(
                  <Text style={[styles.defaultStyles,props.style]}>{props.children}</Text>
                  )
                  }


                  Now in all the files where you have used Text from react native remove import from react native and add



                  import Text from './path/to/component'





                  share|improve this answer


























                    1












                    1








                    1






                    this guide will help you on how to apply custom fonts, I have been using the method in my apps.
                    To create a custom text component



                    export default Text = (props)=>{
                    return(
                    <Text style={[styles.defaultStyles,props.style]}>{props.children}</Text>
                    )
                    }


                    Now in all the files where you have used Text from react native remove import from react native and add



                    import Text from './path/to/component'





                    share|improve this answer














                    this guide will help you on how to apply custom fonts, I have been using the method in my apps.
                    To create a custom text component



                    export default Text = (props)=>{
                    return(
                    <Text style={[styles.defaultStyles,props.style]}>{props.children}</Text>
                    )
                    }


                    Now in all the files where you have used Text from react native remove import from react native and add



                    import Text from './path/to/component'






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 12 at 12:56

























                    answered Nov 12 at 12:51









                    Ammar Tariq

                    978




                    978























                        0














                        I would suggest using a style instead of a custom component but it's up to you.
                        In my project I have created a file named "commonStyles.js" that looks like this:



                        export default StyleSheet.create({
                        textTitle: {
                        fontSize: 20,
                        color: '#dddddd',
                        fontFamily: 'YourCustomFont',
                        },
                        });


                        then I'm importing this file wherever needed using:



                        import stylesCommon from './styles/stylesCommon';


                        and each text that needs to be changed should look like this:



                        <Text style={stylesCommon.textTitle}>
                        This is my title
                        </Text>





                        share|improve this answer


























                          0














                          I would suggest using a style instead of a custom component but it's up to you.
                          In my project I have created a file named "commonStyles.js" that looks like this:



                          export default StyleSheet.create({
                          textTitle: {
                          fontSize: 20,
                          color: '#dddddd',
                          fontFamily: 'YourCustomFont',
                          },
                          });


                          then I'm importing this file wherever needed using:



                          import stylesCommon from './styles/stylesCommon';


                          and each text that needs to be changed should look like this:



                          <Text style={stylesCommon.textTitle}>
                          This is my title
                          </Text>





                          share|improve this answer
























                            0












                            0








                            0






                            I would suggest using a style instead of a custom component but it's up to you.
                            In my project I have created a file named "commonStyles.js" that looks like this:



                            export default StyleSheet.create({
                            textTitle: {
                            fontSize: 20,
                            color: '#dddddd',
                            fontFamily: 'YourCustomFont',
                            },
                            });


                            then I'm importing this file wherever needed using:



                            import stylesCommon from './styles/stylesCommon';


                            and each text that needs to be changed should look like this:



                            <Text style={stylesCommon.textTitle}>
                            This is my title
                            </Text>





                            share|improve this answer












                            I would suggest using a style instead of a custom component but it's up to you.
                            In my project I have created a file named "commonStyles.js" that looks like this:



                            export default StyleSheet.create({
                            textTitle: {
                            fontSize: 20,
                            color: '#dddddd',
                            fontFamily: 'YourCustomFont',
                            },
                            });


                            then I'm importing this file wherever needed using:



                            import stylesCommon from './styles/stylesCommon';


                            and each text that needs to be changed should look like this:



                            <Text style={stylesCommon.textTitle}>
                            This is my title
                            </Text>






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 12 at 12:51









                            Yanush

                            513




                            513






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.





                                Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                Please pay close attention to the following guidance:


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53262325%2fhow-to-create-a-custom-text-react-native%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Bressuire

                                Vorschmack

                                Quarantine