Reputation: 2200
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?
Upvotes: 67
Views: 152514
Reputation: 3218
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>
}
Upvotes: 40
Reputation: 1
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.
Upvotes: 0
Reputation: 21
if anyone interested doing it by just css/style
<strong style={{textTransform: 'capitalize'}}>{props.alert.type}!
here inner {} is for object {textTransform: 'capitalize'}
Upvotes: 0
Reputation: 71
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>
Upvotes: 1
Reputation: 119
this worked for me!
labelStyle:{
textTransform: 'capitalize',
fontSize:20,
},
Upvotes: 3
Reputation: 1735
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()
Upvotes: 1
Reputation: 8312
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>
}
Upvotes: 1
Reputation: 2119
You can also use the text-transform
css property in style:
<Text style={{textTransform: 'capitalize'}}>{this.state.title}</Text>
Upvotes: 71
Reputation: 4901
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'
/>
Upvotes: 3
Reputation: 10971
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>
Upvotes: 18
Reputation: 2200
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>
Upvotes: 83
Reputation: 517
just use javascript.
text.slice(0,1).toUpperCase() + text.slice(1, text.length)
Upvotes: 9