Skip to content

Commit

Permalink
Merge pull request #15 from donni106/master
Browse files Browse the repository at this point in the history
Updated README.md with example for flatHeights
  • Loading branch information
Alberto Martínez committed May 7, 2019
2 parents 3083a68 + 66f7e12 commit ea6cf77
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,59 @@ The result is a Promise that resolves to an array with the height of each block

Unlike measure, `null` elements returns 0 without generating error, and empty strings returns the same height that RN assigns to empty `<Text>` components (the difference of the result between `null` and empty is intentional).

### Example

```jsx
//...
import rnTextSize, { TSFontSpecs } from 'react-native-text-size'

type Props = {}
type State = { heights: number[] }

// On iOS 9+ will show 'San Francisco' and 'Roboto' on Android
const fontSpecs: TSFontSpecs = {
fontFamily = undefined,
fontSize = 24,
fontStyle = 'italic',
fontWeight = 'bold',
}
const texts = ['I ❤️ rnTextSize', 'I ❤️ rnTextSize using flatHeights', 'Thx for flatHeights']

class Test extends Component<Props, State> {
state = {
heights: [],
}

async componentDidMount() {
const width = Dimensions.get('window').width * 0.8
const heights = await rnTextSize.flatHeights({
text: texts, // array of texts to measure, can include symbols
width, // max-width of the "virtual" container
...fontSpecs, // RN font specification
})
this.setState({
heights
})
}

render() {
const { heights } = this.state

return (
<View style={{ padding: 12 }}>
{texts.map(
(text, index) => (
<Text style={{ height: heights[index], ...fontSpecs }}>
{text}
</Text>
)
)}
</View>
)
}
}
```

## specsForTextStyles

```ts
Expand Down

0 comments on commit ea6cf77

Please sign in to comment.