Git Product home page Git Product logo

react-native-spannable-string's Introduction

react-native-spannable-string

npm

ogimage-1260-630

The simple Text builder for multiple styling in single Text component

How do you make this?

Verbose way ๐Ÿ˜“

<Text style={{ fontSize: 24 }}>
  Using <Text style={{ fontWeight: 'bold', fontSize: 24 }}>Bold</Text>{' '}
  in Text
</Text>

SpannableBuilder way ๐Ÿ”ฅ

SpannableBuilder.getInstance({ fontSize: 24 })
  .append('Using ')
  .appendBold('Bold')
  .append(' in Text')
  .build()

Contents ๐Ÿ†

Install ๐Ÿ’ 

npm i @mj-studio/react-native-spannable-string

or

yarn add @mj-studio/react-native-spannable-string

Usage ๐Ÿ“Œ

  1. Import class from package
import SpannableBuilder from '@mj-studio/react-native-spannable-string';
  1. Create SpannableBuilder instance

Instantiate SpannableBuiler instance with static getInstance function. getInstance receive TextStyle parameter for base style used by SpannableBuilder

SpannableBuilder.getInstance({ fontSize: 24 })

We can also instantate it with Text component with getInstanceWithComponent like this.

SpannableBuilder.getInstanceWithComponent(Text)

// or custom Text component
type Props = { fontFamily: string } & TextProps;
function MyText({fontFamily = 'NotoSansKR-Bold', ...rest}: React.PropsWithChildren<Props>) {
  const { style, children, ...withOutStyle } = rest;

  return (
    <Text style={[style, { fontFamily: fontFamily }]} {...withOutStyle}>
      {children}
    </Text>
  );
}

SpannableBuilder.getInstanceWithComponent(MyText)
  1. Append your texts with spannable free
<View style={{ marginVertical: 64 }}>
  {SpannableBuilder.getInstance({ fontSize: 24 })
    .append('Using ')
    .appendBold('Bold')
    .append(' in Text')
    .build()}
  {SpannableBuilder.getInstance({ fontSize: 24 })
    .append('Using ')
    .appendItalic('Italic')
    .append(' in Text')
    .build()}
  {SpannableBuilder.getInstance({ fontSize: 24 })
    .append('Using ')
    .appendColored('Color', 'red')
    .append(' in Text')
    .build()}
  {SpannableBuilder.getInstance({ fontSize: 24 })
    .append('Using ')
    .appendCustom('Custom Style', {
      textShadowColor: 'blue',
      textShadowRadius: 8,
    })
    .append(' in Text')
    .build()}
</View>

// Sample Title
 SpannableBuilder.getInstance({ fontSize: 44 })
   .appendColored('S', 'red')
   .appendItalic('p')
   .appendCustom('a', {
     fontSize: 30,
     textShadowColor: 'blue',
     textShadowRadius: 12,
   })
   .appendColored('n', 'orange')
   .appendCustom('n', {
     fontSize: 22,
     textDecorationLine: 'underline',
   })
   .appendColored('a', 'skyblue')
   .appendCustom('b', {
     backgroundColor: 'black',
     color: 'white',
     fontSize: 22,
   })
   .appendCustom('l', { fontSize: 18, color: 'red' })
   .appendBold('e ')
   .build(),

Change Logs ๐Ÿ”ง

  • 1.0.0
    • First Release ๐Ÿ”ฅ
  • 1.0.1
    • Add baseStyle parameter in getInstanceWithComponent
  • 1.0.4
    • Add config options additionalTextStyle, outerTextStyle
  • 1.0.7
    • Fix additionalTextStyle bugs in appendBold, appendColored, appendItalic
  • 1.0.8
    • Enable re-usability of Builder
  • 1.0.9
    • Add appendBoldWithDelimiter
  • 1.1.1
    • Ignore if first parameter of appendXXX is not a string
  • 1.1.3
    • Add appendCustomWithDelimiter, appendColoredWithDelimiter, appendItalicWithDelimiter,

feel free your fork or any PR! Thanks

react-native-spannable-string's People

Contributors

mym0404 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.