0.1.5 • Published 5 years ago

react-native-formatted-text v0.1.5

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

react-native-formatted-text

An easy way to dynamically add text format in react native

  • Provides multiple approaches to locate text needs to be formatted
  • Easy to customize with any valid Text styles
  • Automatically detect and handle link or email in the string

Example

npm.io

examples/App.js

Installation

If using yarn:

yarn add react-native-formatted-text

If using npm:

npm i react-native-formatted-text

Usage

import { FormattedText } from 'react-native-formatted-text';

Simply wrap the text in FormattedText tags. You may use ranges prop and to declare start and end index of the text.

<FormattedText ranges={{start: 6, end: 12, style: {color: 'red'}}}>
  Hello, World!
</FormattedText>

Another way is to use matches and provide a text or regex value.

<FormattedText
  matches={[
    {text: 'Hello', style: {color: 'red'}},
    {regex: /World/g, style: {color: 'blue'}},
  ]}>
  Hello, World!
</FormattedText>

Both ranges and matches accept Object for a single lookup or array for multiple setups.

Documentation

Base props

NameDescriptionDefaultType
rangesDeclares matching range[]Array or Object of Range
matchesProvides text or regex value for string matches[]Array or Object of Match
enabledLinkTypes*Enables automatic link checking[]Array or Object of LinkMatch

* This feature uses naive regex to match url/email by design. If a more comprehensive matching is desired, use matches instead. One can also use more specialized libraries as react-native-hyperlink.

Range Object

NameDescriptionType
startDefines start index of text to format, inclusiveInt
endDefines end index of text to format, exclusiveInt
styleProvides format style, can use any valid Text styleObject
onPressDefines the function of press eventFunc

Match Object

NameDescriptionType
textDefines the text to formatInt
regexProvides a regex to find text to formatInt
styleProvides format style, can use any valid Text styleObject
onPressDefines the function of press eventFunc

Link Match Object

NameDescriptionType
typeSet a link type to formatFORMATTED_LINK_MATCH_TYPE
styleProvides format style, can use any valid Text styleObject
onPressDefines the function of press eventFunc

Contributing

Pull requests are welcome! Open a new GitHub issue for any bug or suggestion.

Author

Xiao Ma

Roadmap

  • Allow range overlapping
  • A more flexible matches approach that allows to format all matched text or ones before/after certain index
  • Provide a way to customize press feedback UI
  • Add more link detection type

License

MIT