All Projects → airamrguez → react-native-measure-text

airamrguez / react-native-measure-text

Licence: BSD-2-Clause license
Measure text width and/or height without laying it out.

Programming Languages

objective c
16641 projects - #2 most used programming language
java
68154 projects - #9 most used programming language
javascript
184084 projects - #8 most used programming language
Starlark
911 projects
ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to react-native-measure-text

QPrompt
Personal teleprompter software for all video creators. Built with ease of use, productivity, control accuracy, and smooth performance in mind.
Stars: ✭ 168 (+60%)
Mutual labels:  text
muse-as-service
REST API for sentence tokenization and embedding using Multilingual Universal Sentence Encoder.
Stars: ✭ 45 (-57.14%)
Mutual labels:  text
strings-truncation
Truncate strings with fullwidth characters and ANSI codes.
Stars: ✭ 45 (-57.14%)
Mutual labels:  text
fql
Formatted text processing with SQL
Stars: ✭ 20 (-80.95%)
Mutual labels:  text
imperial
Official mono-repo for https://imperialb.in/
Stars: ✭ 35 (-66.67%)
Mutual labels:  text
svensktext
Svenska språkresurser: kvinno- och mansnamn, orter, län, kommuner, länder, nationaliteter, yrken, sentimentlexikon, moral, stoppord, myndigheter m.m.
Stars: ✭ 54 (-48.57%)
Mutual labels:  text
nimtesseract
A Tesseract OCR wrapper for Nim
Stars: ✭ 23 (-78.1%)
Mutual labels:  text
Splain
small parser to create more interesting language/sentences
Stars: ✭ 15 (-85.71%)
Mutual labels:  text
FNet-pytorch
Unofficial implementation of Google's FNet: Mixing Tokens with Fourier Transforms
Stars: ✭ 204 (+94.29%)
Mutual labels:  text
text-to-freemind
A simple text to Freemind conversion program
Stars: ✭ 38 (-63.81%)
Mutual labels:  text
spyql
Query data on the command line with SQL-like SELECTs powered by Python expressions
Stars: ✭ 694 (+560.95%)
Mutual labels:  text
heroku-buildpack-tex
A Heroku buildpack to run TeX Live inside a dyno.
Stars: ✭ 18 (-82.86%)
Mutual labels:  text
readtext
an R package for reading text files
Stars: ✭ 102 (-2.86%)
Mutual labels:  text
glitched-writer
Glitched, text writing js module. Highly customizable settings. Decoding, decrypting, scrambling, or simply spelling out text.
Stars: ✭ 51 (-51.43%)
Mutual labels:  text
cummings.ee
A collection of the work of Edward Estlin Cummings, as it enters the public domain.
Stars: ✭ 32 (-69.52%)
Mutual labels:  text
glText
Cross-platform single header text rendering library for OpenGL
Stars: ✭ 93 (-11.43%)
Mutual labels:  text
lt2circuitikz
Python (3.5) tool to convert .asc files into circuiTikz graphics
Stars: ✭ 57 (-45.71%)
Mutual labels:  text
hotscript
HotScript - Revolutionizing how Windows works.
Stars: ✭ 29 (-72.38%)
Mutual labels:  text
Prestyler
Elegant text formatting tool in Swift 🔥
Stars: ✭ 36 (-65.71%)
Mutual labels:  text
lda2vec
Mixing Dirichlet Topic Models and Word Embeddings to Make lda2vec from this paper https://arxiv.org/abs/1605.02019
Stars: ✭ 27 (-74.29%)
Mutual labels:  text

React Native Measure Text

Measure text height and/or width without laying it out.

npm version

Installation

Automatic installation

Run

$ yarn add react-native-measure-text

or, if you want the latest features, then run:

$ yarn add react-native-measure-text@next

And then:

$ react-native link react-native-measure-text

Manual installation

iOS

  1. In XCode, in the project navigator, right click LibrariesAdd Files to [your project's name]
  2. Go to node_modulesreact-native-measure-text and add RNMeasureText.xcodeproj
  3. In XCode, in the project navigator, select your project. Add libRNMeasureText.a to your project's Build PhasesLink Binary With Libraries
  4. Run your project (Cmd+R)<

Android

  1. Open up android/app/src/main/java/[...]/MainActivity.java
  • Add import io.github.airamrguez.RNMeasureTextPackage; to the imports at the top of the file
  • Add new RNMeasureTextPackage() to the list returned by the getPackages() method
  1. Append the following lines to android/settings.gradle:
    include ':react-native-measure-text'
    project(':react-native-measure-text').projectDir = new File(rootProject.projectDir, 	'../node_modules/react-native-measure-text/android')
    
  2. Insert the following lines inside the dependencies block in android/app/build.gradle:
      compile project(':react-native-measure-text')
    

Usage

import MeasureText from 'react-native-measure-text';

const texts = [
  'This is an example',
  'This is the second line'
];
const width = 100;
const fontSize = 15;
const fontFamily = 'Arvo';

class Test extends Component {
  state = {
    heights: [],
  }
  async componentDidMount() {
    const heights = await MeasureText.heights({(
      texts, /* texts to measure */
      width, /* container width */
      fontSize,
      fontFamily /* fontFamily is optional! */
    );
    this.setState({ heights });
  }
  render() {
    const { heights } = this.state;
    return (
      <View>
        {texts.map((text, i) => (
          <Text
            key={`text-${i}`}
            style={{
              width,
              fontSize,
              fontFamily,
              height: heights[i],
            }}
          >
            {text}
          </Text>
        ))}
    </View>
  }
}

API

MeasureText.heights(options)

Returns a promise that resolves to all the heights of the texts passed in options.

MeasureText.widths(options)

Returns a promise that resolves to all the widths of the texts passed in options.

Measure options:

  • texts: An array of texts to measure.
  • width: Container width when you want to measure the height.
  • height: Container height when you want to measure the width.
  • fontSize: The size of the font.
  • fontFamily: The name of a custom font or a preinstalled font. This is optional.
  • fontWeight: Specifies font weight. The values are the same that React Native allows: enum('normal', 'bold', '100', '200', '300', '400', '500', '600', '700', '800', '900')

How to use a custom font?

Follow these steps:

  1. Create assets/fonts at the root of your React Native project.
  2. Add this piece of JSON into your package.json file:
  "rnpm": {
    "assets": [
      "./assets/fonts/"
    ]
  }
  1. If you want to use the same font cross-platform, in order to avoid platform incompatibility issues, be sure that your font file name matches the font name! For example if the font name is "Arvo" the file should be named "Arvo.ttf". You can specify different fonts for each platform if you want.
  2. Run the command react-native link on the root of your project to link the added fonts.

Using fontWeight

If you are using custom fonts then you have to add the bold version into the assets/fonts directory. Follow the convention that React Native applies. Extracted from the docs:

Given a "family" font family the files in the assets/fonts folder need to be family.ttf (.otf) family_bold.ttf(.otf) family_italic.ttf(.otf) and family_bold_italic.ttf(.otf)

You're ready to go!

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].