├── README.md
├── example
├── index.android.js
├── index.ios.js
└── package.json
├── index.js
└── package.json
/README.md:
--------------------------------------------------------------------------------
1 | # react-native-textfit [](https://www.npmjs.org/package/react-native-textfit)
2 | Auto size text to fit inside a box
3 |
4 | ***This repo will be discontinued as `adjustsFontSizeToFit` was added to react-native Text elements https://github.com/facebook/react-native/pull/4026 .. Finally! :wink:***
5 |
6 |
7 | ## Props
8 |
9 | - `children`: ***required*** Text input that should be resized
10 | - `width`: Maximum width of the box. (Default: auto)
11 | - `height`: Maximum height of the box. (Default: auto)
12 | - [Other React props](https://facebook.github.io/react-native/docs/text.html#props)
13 |
14 | ## Installation
15 |
16 | ```js
17 | npm i -S react-native-textfit
18 | ```
19 |
20 | ## Usage Examples
21 |
22 | ```js
23 | import TextFit from "react-native-textfit"
24 | ```
25 |
26 | ```html
27 | console.log(event)}>
32 | {'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'}
33 |
34 | ```
35 |
36 | ## Try example
37 |
38 | ```
39 | - git clone https://github.com/stoffern/react-native-textfit.git
40 | - cd react-native-textfit/example
41 | - npm start
42 | ```
43 |
--------------------------------------------------------------------------------
/example/index.android.js:
--------------------------------------------------------------------------------
1 | import React,{Component, AppRegistry} from 'react'
2 | import {
3 | ,StyleSheet
4 | ,View
5 | } from 'react-native'
6 |
7 | import TextFit from 'react-native-textfit'
8 |
9 | class Example extends Component{
10 | render() {
11 | return (
12 |
13 | console.log(event)}>
18 | {'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'}
19 |
20 |
21 | );
22 | }
23 | }
24 |
25 | const styles = StyleSheet.create({
26 | container: {
27 | flex: 1,
28 | backgroundColor: 'blue',
29 | }
30 | })
31 | AppRegistry.registerComponent('Example', () => Example)
--------------------------------------------------------------------------------
/example/index.ios.js:
--------------------------------------------------------------------------------
1 | import React,{Component, AppRegistry} from 'react'
2 | import {
3 | ,StyleSheet
4 | ,View
5 | } from 'react-native'
6 |
7 | import TextFit from 'react-native-textfit'
8 |
9 | class Example extends Component{
10 | render() {
11 | return (
12 |
13 | console.log(event)}>
18 | {'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'}
19 |
20 |
21 | );
22 | }
23 | }
24 |
25 | const styles = StyleSheet.create({
26 | container: {
27 | flex: 1,
28 | backgroundColor: 'blue',
29 | }
30 | })
31 | AppRegistry.registerComponent('Example', () => Example);
--------------------------------------------------------------------------------
/example/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "example",
3 | "version": "1.0.0",
4 | "description": "",
5 | "main": "index.js",
6 | "scripts": {
7 | "start": "npm i && react-native upgrade"
8 | },
9 | "author": "stoffern",
10 | "license": "ISC",
11 | "dependencies": {
12 | "react": "^15.2.1",
13 | "react-native": "^0.29.2",
14 | "react-native-textfit": "^0.1.0"
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/index.js:
--------------------------------------------------------------------------------
1 | import React, {Component, PropTypes} from 'react'
2 | import {
3 | StyleSheet,
4 | Text,
5 | View,
6 | NativeModules
7 | } from 'react-native'
8 |
9 | const UIManager = NativeModules.UIManager;
10 |
11 | class TextFit extends Component {
12 | constructor(props) {
13 | super(props);
14 | this.state = {
15 | size: 0.5,
16 | complete: false,
17 | }
18 | }
19 |
20 | setSize() {
21 | const maxHeight = this.props.height
22 | this.refs.field.measure((x, y, width, height, px, py) =>{
23 | if (maxHeight < height) {
24 | if (this.state.size == 0.5) {
25 | this.setState({complete: true});
26 | } else {
27 | this.setState({size: this.state.size -= 0.5, complete: true});
28 | this.setSize()
29 | }
30 | } else {
31 | if (!this.state.complete) {
32 | this.setState({size: this.state.size += 0.5})
33 | this.setSize()
34 | }
35 | }
36 | })
37 | }
38 | componentDidMount() {
39 | this.setSize()
40 | }
41 |
42 | render() {
43 | return (
44 |
55 | {this.props.children}
56 |
57 | )
58 | }
59 | }
60 |
61 | TextFit.defaultProps = {
62 | style:{}
63 | }
64 | TextFit.propTypes = {
65 | children: React.PropTypes.any.isRequired,
66 | style: React.PropTypes.object,
67 | }
68 |
69 | export default TextFit;
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "react-native-textfit",
3 | "version": "0.1.1",
4 | "description": "Auto size text to fit inside a box",
5 | "main": "index.js",
6 | "scripts": {
7 | "test": "echo \"Error: no test specified\" && exit 1"
8 | },
9 | "repository": {
10 | "type": "git",
11 | "url": "git+https://github.com/stoffern/react-native-textfit.git"
12 | },
13 | "keywords": [
14 | "autosize",
15 | "react-native",
16 | "fit",
17 | "text",
18 | "text"
19 | ],
20 | "author": "stoffern",
21 | "license": "ISC",
22 | "bugs": {
23 | "url": "https://github.com/stoffern/react-native-textfit/issues"
24 | },
25 | "homepage": "https://github.com/stoffern/react-native-textfit#readme"
26 | }
27 |
--------------------------------------------------------------------------------