Giter Site home page Giter Site logo

lingxiao / react-native-measure-text Goto Github PK

View Code? Open in Web Editor NEW

This project forked from airamrguez/react-native-measure-text

0.0 2.0 0.0 283 KB

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

License: BSD 2-Clause "Simplified" License

Java 31.23% JavaScript 22.41% Python 7.95% Objective-C 35.51% Ruby 2.90%

react-native-measure-text's Introduction

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!

react-native-measure-text's People

Contributors

airamrguez avatar

Watchers

James Cloos avatar Xiao Ling avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.