# react-native-formatted-text

> An easy way to dynamically add text format in react native

Latest version **0.1.5** (published 2020-08-26) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-native-formatted-text
pnpm add react-native-formatted-text
yarn add react-native-formatted-text
bun add react-native-formatted-text
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.5 |
| Published | 2020-08-26 |
| First published | 2020-08-24 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 10.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 7 |
| Author | Xiao Ma |
| Maintainers | xiaoist |
| Keywords | react-native, react native, mobile, style text, text style, format text, text format, attributed string, spannable text |

## Links

- npm: https://www.npmjs.com/package/react-native-formatted-text
- Repository: https://github.com/MaXiao/react-native-formatted-text
- Homepage: https://github.com/MaXiao/react-native-formatted-text#readme
- Issues: https://github.com/MaXiao/react-native-formatted-text/issues
- npm.io page: https://npm.io/package/react-native-formatted-text

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 0.1.5 (latest) — 2020-08-26
- 0.1.4 — 2020-08-24
- 0.1.3 — 2020-08-24
- 0.1.2 — 2020-08-24
- 0.1.1 — 2020-08-24

## README

# 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
![](assets/example.png) 

[examples/App.js](examples/App.js)

## Installation
If using yarn:

```
yarn add react-native-formatted-text
```

If using npm:

```
npm i react-native-formatted-text
```

## Usage
```jsx
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.

```jsx
<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.

```jsx
<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

| Name             | Description                                     | Default      | Type            |
| ---------------- | ----------------------------------------------- | ------------ | --------------- |
| ranges           | Declares matching range                         | []           | Array or Object of Range |
| matches          | Provides 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](https://github.com/obipawan/react-native-hyperlink).

### Range Object
| Name        | Description                                         |  Type        |  
| ----------- | --------------------------------------------------- | ------------ |
| start       | Defines start index of text to format, inclusive    | Int          |
| end         | Defines end index of text to format, exclusive      | Int          |
| style       | Provides format style, can use any valid Text style | Object       |
| onPress     | Defines the function of press event                 | Func         |

### Match Object
| Name        | Description                                         |  Type        |  
| ----------- | --------------------------------------------------- | ------------ |
| text        | Defines the text to format                          | Int          |
| regex       | Provides a regex to find text to format             | Int          |
| style       | Provides format style, can use any valid Text style | Object       |
| onPress     | Defines the function of press event                 | Func         |

### Link Match Object
| Name        | Description                                         |  Type                     |  
| ----------- | --------------------------------------------------- | ------------------------- |
| type        | Set a link type to format                           | FORMATTED_LINK_MATCH_TYPE |
| style       | Provides format style, can use any valid Text style | Object                    |
| onPress     | Defines the function of press event                 | Func                      |

## 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](./LICENSE)

---
_Source: https://npm.io/package/react-native-formatted-text · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
