2015-01-30 01:10:49 +00:00
/ * *
2016-05-17 17:31:07 +00:00
* Copyright ( c ) 2013 - present , Facebook , Inc .
* All rights reserved .
*
* This source code is licensed under the BSD - style license found in the
* LICENSE file in the root directory of this source tree . An additional grant
* of patent rights can be found in the PATENTS file in the same directory .
*
2015-03-26 18:24:15 +00:00
* The examples provided by Facebook are for non - commercial testing and
* evaluation purposes only .
2015-03-23 22:07:33 +00:00
*
2015-03-26 18:24:15 +00:00
* Facebook reserves all rights not expressly granted .
*
* THE SOFTWARE IS PROVIDED "AS IS" , WITHOUT WARRANTY OF ANY KIND , EXPRESS
* OR IMPLIED , INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY ,
* FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT . IN NO EVENT SHALL
* FACEBOOK BE LIABLE FOR ANY CLAIM , DAMAGES OR OTHER LIABILITY , WHETHER IN
* AN ACTION OF CONTRACT , TORT OR OTHERWISE , ARISING FROM , OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE .
2015-03-23 22:07:33 +00:00
*
* @ flow
2017-02-25 11:05:32 +00:00
* @ providesModule TextExample
2015-01-30 01:10:49 +00:00
* /
'use strict' ;
2016-12-19 14:26:07 +00:00
const Platform = require ( 'Platform' ) ;
2016-04-09 03:36:40 +00:00
var React = require ( 'react' ) ;
var ReactNative = require ( 'react-native' ) ;
2015-01-30 01:10:49 +00:00
var {
2015-10-08 18:32:11 +00:00
Image ,
2015-01-30 01:10:49 +00:00
StyleSheet ,
Text ,
View ,
2016-08-10 18:14:36 +00:00
LayoutAnimation ,
2016-04-09 03:36:40 +00:00
} = ReactNative ;
2015-01-30 01:10:49 +00:00
2016-07-26 08:00:02 +00:00
class Entity extends React . Component {
render ( ) {
2015-01-30 01:10:49 +00:00
return (
2015-06-17 12:37:20 +00:00
< Text style = { { fontWeight : '500' , color : '#527fe4' } } >
2015-01-30 01:10:49 +00:00
{ this . props . children }
< / T e x t >
) ;
}
2016-07-26 08:00:02 +00:00
}
2015-01-30 01:10:49 +00:00
2016-07-26 08:00:02 +00:00
class AttributeToggler extends React . Component {
state = { fontWeight : 'bold' , fontSize : 15 } ;
toggleWeight = ( ) => {
2015-06-17 12:37:20 +00:00
this . setState ( {
fontWeight : this . state . fontWeight === 'bold' ? 'normal' : 'bold'
} ) ;
2016-07-26 08:00:02 +00:00
} ;
increaseSize = ( ) => {
2015-01-30 01:10:49 +00:00
this . setState ( {
fontSize : this . state . fontSize + 1
} ) ;
2016-07-26 08:00:02 +00:00
} ;
render ( ) {
2015-06-17 12:37:20 +00:00
var curStyle = { fontWeight : this . state . fontWeight , fontSize : this . state . fontSize } ;
2015-01-30 01:10:49 +00:00
return (
2015-06-17 12:37:20 +00:00
< View >
2015-01-30 01:10:49 +00:00
< Text style = { curStyle } >
Tap the controls below to change attributes .
< / T e x t >
< Text >
2015-06-17 12:37:20 +00:00
< Text > See how it will even work on < Text style = { curStyle } > this nested text < / T e x t > < / T e x t >
2015-01-30 01:10:49 +00:00
< / T e x t >
2015-06-17 12:37:20 +00:00
< Text
style = { { backgroundColor : '#ffaaaa' , marginTop : 5 } }
onPress = { this . toggleWeight } >
Toggle Weight
< / T e x t >
< Text
style = { { backgroundColor : '#aaaaff' , marginTop : 5 } }
onPress = { this . increaseSize } >
Increase Size
< / T e x t >
< / V i e w >
2015-01-30 01:10:49 +00:00
) ;
}
2016-07-26 08:00:02 +00:00
}
2015-01-30 01:10:49 +00:00
2016-08-10 18:14:36 +00:00
var AdjustingFontSize = React . createClass ( {
getInitialState : function ( ) {
return { dynamicText : '' , shouldRender : true , } ;
} ,
reset : function ( ) {
LayoutAnimation . easeInEaseOut ( ) ;
this . setState ( {
shouldRender : false ,
} ) ;
setTimeout ( ( ) => {
LayoutAnimation . easeInEaseOut ( ) ;
this . setState ( {
dynamicText : '' ,
shouldRender : true ,
} ) ;
} , 300 ) ;
} ,
addText : function ( ) {
this . setState ( {
dynamicText : this . state . dynamicText + ( Math . floor ( ( Math . random ( ) * 10 ) % 2 ) ? ' foo' : ' bar' ) ,
} ) ;
} ,
removeText : function ( ) {
this . setState ( {
dynamicText : this . state . dynamicText . slice ( 0 , this . state . dynamicText . length - 4 ) ,
} ) ;
} ,
render : function ( ) {
if ( ! this . state . shouldRender ) {
return ( < View / > ) ;
}
return (
< View >
< Text lineBreakMode = "tail" numberOfLines = { 1 } style = { { fontSize : 36 , marginVertical : 6 } } >
Truncated text is baaaaad .
< / T e x t >
< Text numberOfLines = { 1 } adjustsFontSizeToFit = { true } style = { { fontSize : 40 , marginVertical : 6 } } >
Shrinking to fit available space is much better !
< / T e x t >
< Text adjustsFontSizeToFit = { true } numberOfLines = { 1 } style = { { fontSize : 30 , marginVertical : 6 } } >
{ 'Add text to me to watch me shrink!' + ' ' + this . state . dynamicText }
< / T e x t >
< Text adjustsFontSizeToFit = { true } numberOfLines = { 4 } style = { { fontSize : 20 , marginVertical : 6 } } >
{ 'Multiline text component shrinking is supported, watch as this reeeeaaaally loooooong teeeeeeext grooooows and then shriiiinks as you add text to me! ioahsdia soady auydoa aoisyd aosdy ' + ' ' + this . state . dynamicText }
< / T e x t >
< Text adjustsFontSizeToFit = { true } numberOfLines = { 1 } style = { { marginVertical : 6 } } >
< Text style = { { fontSize : 14 } } >
{ 'Differently sized nested elements will shrink together. ' }
< / T e x t >
< Text style = { { fontSize : 20 } } >
{ 'LARGE TEXT! ' + this . state . dynamicText }
< / T e x t >
< / T e x t >
< View style = { { flexDirection : 'row' , justifyContent : 'space-around' , marginTop : 5 , marginVertical : 6 } } >
< Text
style = { { backgroundColor : '#ffaaaa' } }
onPress = { this . reset } >
Reset
< / T e x t >
< Text
style = { { backgroundColor : '#aaaaff' } }
onPress = { this . removeText } >
Remove Text
< / T e x t >
< Text
style = { { backgroundColor : '#aaffaa' } }
onPress = { this . addText } >
Add Text
< / T e x t >
< / V i e w >
< / V i e w >
) ;
}
} ) ;
2015-01-30 01:10:49 +00:00
exports . title = '<Text>' ;
exports . description = 'Base component for rendering styled text.' ;
2015-03-25 17:16:19 +00:00
exports . displayName = 'TextExample' ;
2015-01-30 01:10:49 +00:00
exports . examples = [
{
title : 'Wrap' ,
render : function ( ) {
return (
< Text >
The text should wrap if it goes on multiple lines . See , this is going to
the next line .
< / T e x t >
) ;
} ,
2015-03-31 03:12:32 +00:00
} , {
title : 'Padding' ,
render : function ( ) {
return (
< Text style = { { padding : 10 } } >
This text is indented by 10 px padding on all sides .
< / T e x t >
) ;
} ,
2015-01-30 01:10:49 +00:00
} , {
title : 'Font Family' ,
render : function ( ) {
return (
< View >
2016-12-19 14:26:07 +00:00
< Text style = { { fontFamily : ( Platform . isTVOS ? 'Times' : 'Cochin' ) } } >
2015-01-30 01:10:49 +00:00
Cochin
< / T e x t >
2016-12-19 14:26:07 +00:00
< Text style = { { fontFamily : ( Platform . isTVOS ? 'Times' : 'Cochin' ) , fontWeight : 'bold' } } >
2015-01-30 01:10:49 +00:00
Cochin bold
< / T e x t >
< Text style = { { fontFamily : 'Helvetica' } } >
Helvetica
< / T e x t >
< Text style = { { fontFamily : 'Helvetica' , fontWeight : 'bold' } } >
Helvetica bold
< / T e x t >
2016-12-19 14:26:07 +00:00
< Text style = { { fontFamily : ( Platform . isTVOS ? 'Courier' : 'Verdana' ) } } >
2015-01-30 01:10:49 +00:00
Verdana
< / T e x t >
2016-12-19 14:26:07 +00:00
< Text style = { { fontFamily : ( Platform . isTVOS ? 'Courier' : 'Verdana' ) , fontWeight : 'bold' } } >
2015-01-30 01:10:49 +00:00
Verdana bold
< / T e x t >
< / V i e w >
) ;
} ,
} , {
title : 'Font Size' ,
render : function ( ) {
return (
< View >
< Text style = { { fontSize : 23 } } >
Size 23
< / T e x t >
< Text style = { { fontSize : 8 } } >
Size 8
< / T e x t >
< / V i e w >
) ;
} ,
} , {
title : 'Color' ,
render : function ( ) {
return (
< View >
< Text style = { { color : 'red' } } >
Red color
< / T e x t >
< Text style = { { color : 'blue' } } >
Blue color
< / T e x t >
< / V i e w >
) ;
} ,
} , {
title : 'Font Weight' ,
render : function ( ) {
return (
2015-03-26 01:19:28 +00:00
< View >
2016-04-24 01:23:01 +00:00
< Text style = { { fontSize : 20 , fontWeight : '100' } } >
2015-03-26 01:19:28 +00:00
Move fast and be ultralight
< / T e x t >
2016-04-24 01:23:01 +00:00
< Text style = { { fontSize : 20 , fontWeight : '200' } } >
2015-03-26 01:19:28 +00:00
Move fast and be light
< / T e x t >
2016-04-24 01:23:01 +00:00
< Text style = { { fontSize : 20 , fontWeight : 'normal' } } >
2015-03-26 01:19:28 +00:00
Move fast and be normal
< / T e x t >
2016-04-24 01:23:01 +00:00
< Text style = { { fontSize : 20 , fontWeight : 'bold' } } >
2015-03-26 01:19:28 +00:00
Move fast and be bold
< / T e x t >
2016-04-24 01:23:01 +00:00
< Text style = { { fontSize : 20 , fontWeight : '900' } } >
2015-03-26 01:19:28 +00:00
Move fast and be ultrabold
< / T e x t >
< / V i e w >
) ;
} ,
} , {
title : 'Font Style' ,
render : function ( ) {
return (
< View >
< Text style = { { fontStyle : 'normal' } } >
Normal text
< / T e x t >
< Text style = { { fontStyle : 'italic' } } >
Italic text
< / T e x t >
< / V i e w >
2015-01-30 01:10:49 +00:00
) ;
} ,
2016-11-18 00:09:43 +00:00
} , {
title : 'Selectable' ,
render : function ( ) {
return (
< View >
< Text selectable = { true } >
This text is < Text style = { { fontWeight : 'bold' } } > selectable < / T e x t > i f y o u c l i c k - a n d - h o l d .
< / T e x t >
< / V i e w >
) ;
} ,
2015-07-07 13:03:56 +00:00
} , {
title : 'Text Decoration' ,
render : function ( ) {
return (
< View >
< Text style = { { textDecorationLine : 'underline' , textDecorationStyle : 'solid' } } >
Solid underline
< / T e x t >
< Text style = { { textDecorationLine : 'underline' , textDecorationStyle : 'double' , textDecorationColor : '#ff0000' } } >
Double underline with custom color
< / T e x t >
< Text style = { { textDecorationLine : 'underline' , textDecorationStyle : 'dashed' , textDecorationColor : '#9CDC40' } } >
Dashed underline with custom color
< / T e x t >
< Text style = { { textDecorationLine : 'underline' , textDecorationStyle : 'dotted' , textDecorationColor : 'blue' } } >
Dotted underline with custom color
< / T e x t >
< Text style = { { textDecorationLine : 'none' } } >
None textDecoration
< / T e x t >
< Text style = { { textDecorationLine : 'line-through' , textDecorationStyle : 'solid' } } >
Solid line - through
< / T e x t >
< Text style = { { textDecorationLine : 'line-through' , textDecorationStyle : 'double' , textDecorationColor : '#ff0000' } } >
Double line - through with custom color
< / T e x t >
< Text style = { { textDecorationLine : 'line-through' , textDecorationStyle : 'dashed' , textDecorationColor : '#9CDC40' } } >
Dashed line - through with custom color
< / T e x t >
< Text style = { { textDecorationLine : 'line-through' , textDecorationStyle : 'dotted' , textDecorationColor : 'blue' } } >
Dotted line - through with custom color
< / T e x t >
< Text style = { { textDecorationLine : 'underline line-through' } } >
Both underline and line - through
< / T e x t >
< / V i e w >
) ;
} ,
2015-01-30 01:10:49 +00:00
} , {
title : 'Nested' ,
description : 'Nested text components will inherit the styles of their ' +
'parents (only backgroundColor is inherited from non-Text parents). ' +
'<Text> only supports other <Text> and raw text (strings) as children.' ,
render : function ( ) {
return (
< View >
< Text >
( Normal text ,
< Text style = { { fontWeight : 'bold' } } >
( and bold
< Text style = { { fontSize : 11 , color : '#527fe4' } } >
( and tiny inherited bold blue )
< / T e x t >
2015-11-04 16:52:58 +00:00
)
2015-11-04 16:52:46 +00:00
< / T e x t >
)
< / T e x t >
2015-11-04 16:52:58 +00:00
< Text style = { { opacity : 0.7 } } >
( opacity
< Text >
( is inherited
< Text style = { { opacity : 0.7 } } >
( and accumulated
< Text style = { { backgroundColor : '#ffaaaa' } } >
( and also applies to the background )
< / T e x t >
)
< / T e x t >
)
< / T e x t >
2015-01-30 01:10:49 +00:00
)
< / T e x t >
< Text style = { { fontSize : 12 } } >
< Entity > Entity Name < / E n t i t y >
< / T e x t >
< / V i e w >
) ;
} ,
} , {
title : 'Text Align' ,
render : function ( ) {
return (
< View >
2015-06-17 12:37:20 +00:00
< Text >
auto ( default ) - english LTR
< / T e x t >
< Text >
أحب اللغة العربية auto ( default ) - arabic RTL
< / T e x t >
2015-01-30 01:10:49 +00:00
< Text style = { { textAlign : 'left' } } >
left left left left left left left left left left left left left left left
< / T e x t >
< Text style = { { textAlign : 'center' } } >
center center center center center center center center center center center
< / T e x t >
< Text style = { { textAlign : 'right' } } >
right right right right right right right right right right right right right
< / T e x t >
2015-06-15 19:00:18 +00:00
< Text style = { { textAlign : 'justify' } } >
2015-07-24 15:25:51 +00:00
justify : this text component { "'" } s contents are laid out with "textAlign: justify"
2015-06-15 19:00:18 +00:00
and as you can see all of the lines except the last one span the
available width of the parent container .
< / T e x t >
2015-01-30 01:10:49 +00:00
< / V i e w >
) ;
} ,
2015-05-12 07:25:08 +00:00
} , {
title : 'Letter Spacing' ,
render : function ( ) {
return (
< View >
< Text style = { { letterSpacing : 0 } } >
letterSpacing = 0
< / T e x t >
< Text style = { { letterSpacing : 2 , marginTop : 5 } } >
letterSpacing = 2
< / T e x t >
< Text style = { { letterSpacing : 9 , marginTop : 5 } } >
letterSpacing = 9
< / T e x t >
< Text style = { { letterSpacing : - 1 , marginTop : 5 } } >
letterSpacing = - 1
< / T e x t >
< / V i e w >
) ;
} ,
2015-01-30 01:10:49 +00:00
} , {
title : 'Spaces' ,
render : function ( ) {
return (
< Text >
A { 'generated' } { ' ' } { 'string' } and some & nbsp ; & nbsp ; & nbsp ; spaces
< / T e x t >
) ;
} ,
} , {
title : 'Line Height' ,
render : function ( ) {
return (
< Text >
< Text style = { { lineHeight : 35 } } >
A lot of space between the lines of this long passage that should
wrap once .
< / T e x t >
< / T e x t >
) ;
} ,
} , {
title : 'Empty Text' ,
description : 'It\'s ok to have Text with zero or null children.' ,
render : function ( ) {
return (
< Text / >
) ;
} ,
} , {
title : 'Toggling Attributes' ,
2016-10-16 11:11:59 +00:00
render : function ( ) : React . Element < any > {
2015-01-30 01:10:49 +00:00
return < AttributeToggler / > ;
} ,
} , {
title : 'backgroundColor attribute' ,
description : 'backgroundColor is inherited from all types of views.' ,
render : function ( ) {
return (
2015-06-17 12:37:20 +00:00
< Text style = { { backgroundColor : 'yellow' } } >
Yellow container background ,
< Text style = { { backgroundColor : '#ffaaaa' } } >
{ ' ' } red background ,
< Text style = { { backgroundColor : '#aaaaff' } } >
{ ' ' } blue background ,
< Text >
{ ' ' } inherited blue background ,
< Text style = { { backgroundColor : '#aaffaa' } } >
{ ' ' } nested green background .
2015-01-30 01:10:49 +00:00
< / T e x t >
< / T e x t >
< / T e x t >
< / T e x t >
2015-06-17 12:37:20 +00:00
< / T e x t >
2015-01-30 01:10:49 +00:00
) ;
} ,
} , {
title : 'numberOfLines attribute' ,
render : function ( ) {
return (
< View >
< Text numberOfLines = { 1 } >
2015-05-28 16:29:27 +00:00
Maximum of one line , no matter how much I write here . If I keep writing , it { "'" } ll just truncate after one line .
2015-01-30 01:10:49 +00:00
< / T e x t >
< Text numberOfLines = { 2 } style = { { marginTop : 20 } } >
2015-05-28 16:29:27 +00:00
Maximum of two lines , no matter how much I write here . If I keep writing , it { "'" } ll just truncate after two lines .
2015-01-30 01:10:49 +00:00
< / T e x t >
< Text style = { { marginTop : 20 } } >
2015-05-28 16:29:27 +00:00
No maximum lines specified , no matter how much I write here . If I keep writing , it { "'" } ll just keep going and going .
2015-01-30 01:10:49 +00:00
< / T e x t >
< / V i e w >
) ;
} ,
2015-07-24 15:25:51 +00:00
} , {
title : 'Text highlighting (tap the link to see highlight)' ,
render : function ( ) {
return (
< View >
< Text > Lorem ipsum dolor sit amet , < Text suppressHighlighting = { false } style = { { backgroundColor : 'white' , textDecorationLine : 'underline' , color : 'blue' } } onPress = { ( ) => null } > consectetur adipiscing elit , sed do eiusmod tempor incididunt ut labore et dolore magna aliqua . Ut enim ad minim veniam , quis nostrud < / T e x t > e x e r c i t a t i o n u l l a m c o l a b o r i s n i s i u t a l i q u i p e x e a c o m m o d o c o n s e q u a t . < / T e x t >
< / V i e w >
) ;
} ,
2015-07-31 14:37:12 +00:00
} , {
title : 'allowFontScaling attribute' ,
render : function ( ) {
return (
< View >
< Text >
By default , text will respect Text Size accessibility setting on iOS .
It means that all font sizes will be increased or descreased depending on the value of Text Size setting in
{ " " } < Text style = { { fontWeight : 'bold' } } > Settings . app - Display & Brightness - Text Size < / T e x t >
< / T e x t >
< Text style = { { marginTop : 10 } } >
You can disable scaling for your Text component by passing { "\"" } allowFontScaling = { "{" } false { "}\"" } prop .
< / T e x t >
< Text allowFontScaling = { false } style = { { marginTop : 20 } } >
This text will not scale .
< / T e x t >
< / V i e w >
) ;
} ,
2015-10-08 18:32:11 +00:00
} , {
2016-05-31 17:18:37 +00:00
title : 'Inline views' ,
2015-10-08 18:32:11 +00:00
render : function ( ) {
return (
< View >
< Text >
2016-05-31 17:18:37 +00:00
This text contains an inline blue view < View style = { { width : 25 , height : 25 , backgroundColor : 'steelblue' } } / > and
an inline image < Image source = { require ( './flux.png' ) } style = { { width : 30 , height : 11 , resizeMode : 'cover' } } / > . Neat , huh ?
2015-10-08 18:32:11 +00:00
< / T e x t >
< / V i e w >
) ;
} ,
2016-01-01 17:32:59 +00:00
} , {
title : 'Text shadow' ,
render : function ( ) {
return (
< View >
< Text style = { { fontSize : 20 , textShadowOffset : { width : 2 , height : 2 } , textShadowRadius : 1 , textShadowColor : '#00cccc' } } >
Demo text shadow
< / T e x t >
< / V i e w >
) ;
} ,
2016-06-10 11:26:45 +00:00
} , {
2016-07-25 20:05:14 +00:00
title : 'Ellipsize mode' ,
2016-06-10 11:26:45 +00:00
render : function ( ) {
return (
< View >
< Text numberOfLines = { 1 } >
This very long text should be truncated with dots in the end .
< / T e x t >
2016-07-25 20:05:14 +00:00
< Text ellipsizeMode = "middle" numberOfLines = { 1 } >
2016-06-10 11:26:45 +00:00
This very long text should be truncated with dots in the middle .
< / T e x t >
2016-07-25 20:05:14 +00:00
< Text ellipsizeMode = "head" numberOfLines = { 1 } >
2016-06-10 11:26:45 +00:00
This very long text should be truncated with dots in the beginning .
< / T e x t >
2016-07-25 20:05:14 +00:00
< Text ellipsizeMode = "clip" numberOfLines = { 1 } >
2016-06-10 11:26:45 +00:00
This very looooooooooooooooooooooooooooong text should be clipped .
< / T e x t >
< / V i e w >
) ;
} ,
2016-08-09 15:42:58 +00:00
} , {
title : 'Font variants' ,
render : function ( ) {
return (
< View >
< Text style = { { fontVariant : [ 'small-caps' ] } } >
Small Caps { '\n' }
< / T e x t >
2016-12-19 14:26:07 +00:00
< Text style = { { fontFamily : ( Platform . isTVOS ? 'Times' : 'Hoefler Text' ) , fontVariant : [ 'oldstyle-nums' ] } } >
2016-08-09 15:42:58 +00:00
Old Style nums 0123456789 { '\n' }
< / T e x t >
2016-12-19 14:26:07 +00:00
< Text style = { { fontFamily : ( Platform . isTVOS ? 'Times' : 'Hoefler Text' ) , fontVariant : [ 'lining-nums' ] } } >
2016-08-09 15:42:58 +00:00
Lining nums 0123456789 { '\n' }
< / T e x t >
< Text style = { { fontVariant : [ 'tabular-nums' ] } } >
Tabular nums { '\n' }
1111 { '\n' }
2222 { '\n' }
< / T e x t >
< Text style = { { fontVariant : [ 'proportional-nums' ] } } >
Proportional nums { '\n' }
1111 { '\n' }
2222 { '\n' }
< / T e x t >
< / V i e w >
) ;
} ,
2016-08-10 18:14:36 +00:00
} , {
title : 'Dynamic Font Size Adjustment' ,
2016-10-16 11:11:59 +00:00
render : function ( ) : React . Element < any > {
2016-08-10 18:14:36 +00:00
return < AdjustingFontSize / > ;
} ,
2015-01-30 01:10:49 +00:00
} ] ;