62

I have to capitalize first letter of text that i want to display. I searched for it but i cant found clear thing to do that, also there is no such props for text in react native official documentation.

I am showing my text with following format:

<Text style={styles.title}>{item.item.title}</Text>

or

<Text style={styles.title}>{this.state.title}</Text>

How can I do it?

Suggestions are welcome?

Mayuresh Patil
  • 2,072
  • 1
  • 21
  • 44

12 Answers12

75

Write a function like this

Capitalize(str){
return str.charAt(0).toUpperCase() + str.slice(1);
}

then call it from <Text> tag By passing text as parameter

<Text>{this.Capitalize(this.state.title)} </Text>
Mayuresh Patil
  • 2,072
  • 1
  • 21
  • 44
62

You can also use the text-transform css property in style:

<Text style={{textTransform: 'capitalize'}}>{this.state.title}</Text>
tmsss
  • 1,979
  • 19
  • 23
  • 7
    This only works for all lower case text. (1) If you have "heLLo", this will turn the text into "HeLLo". (2) If you want "Hello" as a result, you can use `"heLLo".toLowerCase()` with `textTransform: 'capitalize'` css. – Michael Harley Nov 20 '19 at 08:20
  • 1
    This will capitalize every word in the input. Which might be OK, but the OP asks only for capitalization of the first letter. – Chris Bartley Oct 08 '21 at 20:44
36

React native now lets you make text uppercase directly with textTransform: 'capitalize'. No function necessary.

import { Text } from 'react-native'

// will render as Hello!
export const Caps = () => {
  return <Text style={{ textTransform: 'capitalize' }}>hello!</Text>
}
Fernando Rojo
  • 2,633
  • 19
  • 17
18

Instead of using a function, a cleaner way is to write this as a common component.

import React from 'react';
import { View, Text } from 'react-native';

const CapitalizedText = (props) => {
  let text = props.children.slice(0,1).toUpperCase() + props.children.slice(1, props.children.length);

  return (
      <View>
        <Text {...props}>{text}</Text>
      </View>
  );
};

export default CapitalizedText;

Wherever you're using <Text>, replace it with <CapitalizedText>

Adam Kipnis
  • 10,175
  • 10
  • 35
  • 48
9

just use javascript.

text.slice(0,1).toUpperCase() + text.slice(1, text.length)

akshay
  • 507
  • 2
  • 10
4

TextInput have this to handle using

autoCapitalize enum('none', 'sentences', 'words', 'characters') 

for example try like this

<TextInput
     placeholder=""
     placeholderTextColor='rgba(28,53,63, 1)'
     autoCapitalize = 'none'
     value ='test'
     />
Vishal Vaghasiya
  • 4,618
  • 3
  • 29
  • 40
3

this worked for me!

labelStyle:{
        textTransform: 'capitalize',
        fontSize:20,

      },
1

Since this is very general functionality I put it in a file called strings.js under my library:

// lib/strings.js

export const CapitalizeFirstLetter = (str) => {
  return str.length ? str.charAt(0).toUpperCase() + str.slice(1) : str
}

And simply import it in the components that need it:

import React from 'react';
import { View, Text } from 'react-native';
import { CapitalizeFirstLetter} from 'lib/strings'

export default function ComponentWithCapitalizedText() {

  return <Text>CapitalizeFirstLetter("capitalize this please")</Text>

}
Nicolai Lissau
  • 7,298
  • 5
  • 43
  • 57
1

I just added a prototype function, based on @mayuresh answer

String.prototype.Capitalize = function() {
    return this.charAt(0).toUpperCase() + this.slice(1);
}

and use it like this:

myStr.Capitalize()
Yinon_90
  • 1,315
  • 12
  • 17
0

If you want to capitalize only the first letter of the input:

function CapitalizeFirstLetterOfInput () {

const onInputUppercase = (e) => {
    let firstLetter = e.target.value.charAt(0);
    e.target.value = firstLetter.toUpperCase() + e.target.value.slice(1);
  };
  
return (
  <div>
 <input onInput={onInputUppercase}/>
  </div>
)
}
 

 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.1/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.1/umd/react-dom.production.min.js"></script>
Dom V
  • 51
  • 7
0

if anyone interested doing it by just css/style

<strong style={{textTransform: 'capitalize'}}>{props.alert.type}!

here inner {} is for object {textTransform: 'capitalize'}

Nirjhor 3029
  • 21
  • 1
  • 3
0

This answer worked for me:

text.slice(0,1).toUpperCase() + text.slice(1, text.length)

I used it with a props in an alt tag:

`${(props.image).slice(0,1).toUpperCase() + (props.image).slice(1, (props.image).length)}`

Then I guess you can apply that to any text you want.