2015-01-30 01:10:49 +00:00
/ * *
2018-09-11 22:27:47 +00:00
* Copyright ( c ) Facebook , Inc . and its affiliates .
2016-05-17 17:31:07 +00:00
*
2018-02-17 02:24:55 +00:00
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree .
2016-05-17 17:31:07 +00:00
*
2018-05-11 20:32:37 +00:00
* @ format
2015-03-23 22:07:33 +00:00
* @ flow
2015-01-30 01:10:49 +00:00
* /
2018-05-11 20:32:37 +00:00
2015-01-30 01:10:49 +00:00
'use strict' ;
2016-12-19 14:26:07 +00:00
const Platform = require ( 'Platform' ) ;
2018-10-09 22:24:29 +00:00
const React = require ( 'react' ) ;
const ReactNative = require ( 'react-native' ) ;
const { Text , TextInput , View , LayoutAnimation , Button } = ReactNative ;
2018-09-07 18:26:22 +00:00
const TextLegend = require ( './Shared/TextLegend' ) ;
2017-10-03 19:54:50 +00:00
type TextAlignExampleRTLState = { |
isRTL : boolean ,
| } ;
class TextAlignRTLExample extends React . Component < * , TextAlignExampleRTLState > {
constructor ( ... args : Array < * > ) {
super ( ... args ) ;
this . state = {
isRTL : false ,
} ;
}
render ( ) {
const { isRTL } = this . state ;
const toggleRTL = ( ) => this . setState ( { isRTL : ! isRTL } ) ;
return (
< View style = { { direction : isRTL ? 'rtl' : 'ltr' } } >
< Text > auto ( default ) - english LTR < / T e x t >
< Text >
{ '\u0623\u062D\u0628 \u0627\u0644\u0644\u063A\u0629 ' +
'\u0627\u0644\u0639\u0631\u0628\u064A\u0629 auto (default) - arabic RTL' }
< / T e x t >
< 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 >
< Text style = { { textAlign : 'justify' } } >
justify : this text component { "'" } s contents are laid out with
"textAlign: justify" 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 >
< Button
onPress = { toggleRTL }
title = { ` Switch to ${ isRTL ? 'LTR' : 'RTL' } ` }
/ >
< / V i e w >
) ;
}
}
2015-01-30 01:10:49 +00:00
2017-08-18 01:36:54 +00:00
class Entity extends React . Component < $FlowFixMeProps > {
2016-07-26 08:00:02 +00:00
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
2017-08-18 01:36:54 +00:00
class AttributeToggler extends React . Component < { } , $FlowFixMeState > {
2016-07-26 08:00:02 +00:00
state = { fontWeight : 'bold' , fontSize : 15 } ;
toggleWeight = ( ) => {
2015-06-17 12:37:20 +00:00
this . setState ( {
2017-10-03 19:54:50 +00:00
fontWeight : this . state . fontWeight === 'bold' ? 'normal' : 'bold' ,
2015-06-17 12:37:20 +00:00
} ) ;
2016-07-26 08:00:02 +00:00
} ;
increaseSize = ( ) => {
2015-01-30 01:10:49 +00:00
this . setState ( {
2017-10-03 19:54:50 +00:00
fontSize : this . state . fontSize + 1 ,
2015-01-30 01:10:49 +00:00
} ) ;
2016-07-26 08:00:02 +00:00
} ;
render ( ) {
2018-10-30 21:29:54 +00:00
const curStyle = {
2017-10-03 19:54:50 +00:00
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 >
2017-10-03 19:54:50 +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
2018-10-09 22:24:29 +00:00
type AdjustingFontSizeProps = $ReadOnly < { || } > ;
type AdjustingFontSizeState = { |
dynamicText : string ,
shouldRender : boolean ,
| } ;
class AdjustingFontSize extends React . Component <
AdjustingFontSizeProps ,
AdjustingFontSizeState ,
> {
state = {
dynamicText : '' ,
shouldRender : true ,
} ;
reset = ( ) => {
2016-08-10 18:14:36 +00:00
LayoutAnimation . easeInEaseOut ( ) ;
this . setState ( {
shouldRender : false ,
} ) ;
2017-10-03 19:54:50 +00:00
setTimeout ( ( ) => {
2016-08-10 18:14:36 +00:00
LayoutAnimation . easeInEaseOut ( ) ;
this . setState ( {
dynamicText : '' ,
shouldRender : true ,
} ) ;
} , 300 ) ;
2018-10-09 22:24:29 +00:00
} ;
addText = ( ) => {
2016-08-10 18:14:36 +00:00
this . setState ( {
2017-10-03 19:54:50 +00:00
dynamicText :
this . state . dynamicText +
( Math . floor ( ( Math . random ( ) * 10 ) % 2 ) ? ' foo' : ' bar' ) ,
2016-08-10 18:14:36 +00:00
} ) ;
2018-10-09 22:24:29 +00:00
} ;
removeText = ( ) => {
2016-08-10 18:14:36 +00:00
this . setState ( {
2017-10-03 19:54:50 +00:00
dynamicText : this . state . dynamicText . slice (
0 ,
this . state . dynamicText . length - 4 ,
) ,
2016-08-10 18:14:36 +00:00
} ) ;
2018-10-09 22:24:29 +00:00
} ;
render ( ) {
2016-08-10 18:14:36 +00:00
if ( ! this . state . shouldRender ) {
2017-10-03 19:54:50 +00:00
return < View / > ;
2016-08-10 18:14:36 +00:00
}
return (
< View >
2017-10-03 19:54:50 +00:00
< Text
ellipsizeMode = "tail"
numberOfLines = { 1 }
style = { { fontSize : 36 , marginVertical : 6 } } >
2016-08-10 18:14:36 +00:00
Truncated text is baaaaad .
< / T e x t >
2017-10-03 19:54:50 +00:00
< Text
numberOfLines = { 1 }
adjustsFontSizeToFit = { true }
style = { { fontSize : 40 , marginVertical : 6 } } >
2016-08-10 18:14:36 +00:00
Shrinking to fit available space is much better !
< / T e x t >
2017-10-03 19:54:50 +00:00
< Text
adjustsFontSizeToFit = { true }
numberOfLines = { 1 }
style = { { fontSize : 30 , marginVertical : 6 } } >
{ 'Add text to me to watch me shrink!' + ' ' + this . state . dynamicText }
2016-08-10 18:14:36 +00:00
< / T e x t >
2017-10-03 19:54:50 +00:00
< 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 }
2016-08-10 18:14:36 +00:00
< / T e x t >
2017-10-03 19:54:50 +00:00
< Text
adjustsFontSizeToFit = { true }
numberOfLines = { 1 }
style = { { marginVertical : 6 } } >
< Text style = { { fontSize : 14 } } >
2016-08-10 18:14:36 +00:00
{ 'Differently sized nested elements will shrink together. ' }
< / T e x t >
2017-10-03 19:54:50 +00:00
< Text style = { { fontSize : 20 } } >
2016-08-10 18:14:36 +00:00
{ 'LARGE TEXT! ' + this . state . dynamicText }
< / T e x t >
< / T e x t >
2017-10-03 19:54:50 +00:00
< View
style = { {
flexDirection : 'row' ,
justifyContent : 'space-around' ,
marginTop : 5 ,
marginVertical : 6 ,
} } >
< Text style = { { backgroundColor : '#ffaaaa' } } onPress = { this . reset } >
2016-08-10 18:14:36 +00:00
Reset
< / T e x t >
2017-10-03 19:54:50 +00:00
< Text style = { { backgroundColor : '#aaaaff' } } onPress = { this . removeText } >
2016-08-10 18:14:36 +00:00
Remove Text
< / T e x t >
2017-10-03 19:54:50 +00:00
< Text style = { { backgroundColor : '#aaffaa' } } onPress = { this . addText } >
2016-08-10 18:14:36 +00:00
Add Text
< / T e x t >
< / V i e w >
< / V i e w >
) ;
2018-10-09 22:24:29 +00:00
}
}
2016-08-10 18:14:36 +00:00
2018-02-16 01:40:10 +00:00
class TextBaseLineLayoutExample extends React . Component < * , * > {
render ( ) {
2018-10-30 21:29:54 +00:00
const texts = [ ] ;
for ( let i = 9 ; i >= 0 ; i -- ) {
2018-05-11 20:32:37 +00:00
texts . push (
< Text key = { i } style = { { fontSize : 8 + i * 5 , backgroundColor : '#eee' } } >
{ i }
< / T e x t > ,
) ;
2018-02-16 01:40:10 +00:00
}
2018-05-11 20:32:37 +00:00
const marker = (
< View style = { { width : 20 , height : 20 , backgroundColor : 'gray' } } / >
) ;
2018-02-16 01:40:10 +00:00
const subtitleStyle = { fontSize : 16 , marginTop : 8 , fontWeight : 'bold' } ;
return (
< View >
< Text style = { subtitleStyle } > { 'Nested <Text/>s:' } < / T e x t >
< View style = { { flexDirection : 'row' , alignItems : 'baseline' } } >
{ marker }
2018-05-11 20:32:37 +00:00
< Text > { texts } < / T e x t >
2018-02-16 01:40:10 +00:00
{ marker }
< / V i e w >
< Text style = { subtitleStyle } > { 'Array of <Text/>s in <View>:' } < / T e x t >
< View style = { { flexDirection : 'row' , alignItems : 'baseline' } } >
{ marker }
{ texts }
{ marker }
< / V i e w >
< Text style = { subtitleStyle } > { '<TextInput/>:' } < / T e x t >
< View style = { { flexDirection : 'row' , alignItems : 'baseline' } } >
{ marker }
2018-05-11 20:32:37 +00:00
< TextInput style = { { margin : 0 , padding : 0 } } > { texts } < / T e x t I n p u t >
2018-02-16 01:40:10 +00:00
{ marker }
< / V i e w >
< Text style = { subtitleStyle } > { '<TextInput multiline/>:' } < / T e x t >
< View style = { { flexDirection : 'row' , alignItems : 'baseline' } } >
{ marker }
< TextInput multiline = { true } style = { { margin : 0 , padding : 0 } } >
{ texts }
< / T e x t I n p u t >
{ marker }
< / V i e w >
< / V i e w >
) ;
}
}
2018-08-24 20:28:36 +00:00
class TextRenderInfoExample extends React . Component < * , * > {
state = {
textMetrics : {
x : 0 ,
y : 0 ,
width : 0 ,
height : 0 ,
capHeight : 0 ,
descender : 0 ,
ascender : 0 ,
xHeight : 0 ,
} ,
numberOfTextBlocks : 1 ,
fontSize : 14 ,
} ;
render ( ) {
const topOfBox =
this . state . textMetrics . y +
this . state . textMetrics . height -
( this . state . textMetrics . descender + this . state . textMetrics . capHeight ) ;
return (
< View >
< View >
< View
style = { {
position : 'absolute' ,
left : this . state . textMetrics . x + this . state . textMetrics . width ,
top : topOfBox ,
width : 5 ,
height : Math . ceil (
this . state . textMetrics . capHeight -
this . state . textMetrics . xHeight ,
) ,
backgroundColor : 'red' ,
} }
/ >
< View
style = { {
position : 'absolute' ,
left : this . state . textMetrics . x + this . state . textMetrics . width ,
top :
topOfBox +
( this . state . textMetrics . capHeight -
this . state . textMetrics . xHeight ) ,
width : 5 ,
height : Math . ceil ( this . state . textMetrics . xHeight ) ,
backgroundColor : 'green' ,
} }
/ >
< Text
style = { { fontSize : this . state . fontSize } }
onTextLayout = { event => {
const { lines } = event . nativeEvent ;
if ( lines . length > 0 ) {
this . setState ( { textMetrics : lines [ lines . length - 1 ] } ) ;
}
} } >
{ new Array ( this . state . numberOfTextBlocks )
. fill ( 'A tiny block of text.' )
. join ( ' ' ) }
< / T e x t >
< / V i e w >
< Text
onPress = { ( ) =>
this . setState ( {
numberOfTextBlocks : this . state . numberOfTextBlocks + 1 ,
} )
} >
More text
< / T e x t >
< Text
onPress = { ( ) => this . setState ( { fontSize : this . state . fontSize + 1 } ) } >
Increase size
< / T e x t >
< Text
onPress = { ( ) => this . setState ( { fontSize : this . state . fontSize - 1 } ) } >
Decrease size
< / T e x t >
< / V i e w >
) ;
}
}
class TextWithCapBaseBox extends React . Component < * , * > {
state = {
textMetrics : {
x : 0 ,
y : 0 ,
width : 0 ,
height : 0 ,
capHeight : 0 ,
descender : 0 ,
ascender : 0 ,
xHeight : 0 ,
} ,
} ;
render ( ) {
return (
< Text
onTextLayout = { event => {
const { lines } = event . nativeEvent ;
if ( lines . length > 0 ) {
this . setState ( { textMetrics : lines [ 0 ] } ) ;
}
} }
style = { [
{
marginTop : Math . ceil (
- (
this . state . textMetrics . ascender -
this . state . textMetrics . capHeight
) ,
) ,
marginBottom : Math . ceil ( - this . state . textMetrics . descender ) ,
} ,
this . props . style ,
] } >
{ this . props . children }
< / T e x t >
) ;
}
}
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 = [
2017-10-03 19:54:50 +00:00
{
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-01-30 01:10:49 +00:00
} ,
2018-08-24 20:28:36 +00:00
{
title : 'Text metrics' ,
render : function ( ) {
return < TextRenderInfoExample / > ;
} ,
} ,
{
title : 'Text metrics legend' ,
render : ( ) => < TextLegend / > ,
} ,
{
title : 'Baseline capheight box' ,
render : ( ) => (
< View style = { { backgroundColor : 'red' } } >
< TextWithCapBaseBox > Some example text . < / T e x t W i t h C a p B a s e B o x >
< / V i e w >
) ,
} ,
2017-10-03 19:54:50 +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-03-31 03:12:32 +00:00
} ,
2017-10-03 19:54:50 +00:00
{
title : 'Font Family' ,
render : function ( ) {
return (
< View >
2018-06-10 20:37:53 +00:00
< Text style = { { fontFamily : Platform . isTV ? 'Times' : 'Cochin' } } >
2017-10-03 19:54:50 +00:00
Cochin
< / T e x t >
< Text
style = { {
2018-06-10 20:37:53 +00:00
fontFamily : Platform . isTV ? 'Times' : 'Cochin' ,
2017-10-03 19:54:50 +00:00
fontWeight : 'bold' ,
} } >
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 >
2018-06-10 20:37:53 +00:00
< Text style = { { fontFamily : Platform . isTV ? 'Courier' : 'Verdana' } } >
2017-10-03 19:54:50 +00:00
Verdana
< / T e x t >
< Text
style = { {
2018-06-10 20:37:53 +00:00
fontFamily : Platform . isTV ? 'Courier' : 'Verdana' ,
2017-10-03 19:54:50 +00:00
fontWeight : 'bold' ,
} } >
Verdana bold
< / T e x t >
< / V i e w >
) ;
} ,
2015-01-30 01:10:49 +00:00
} ,
2017-10-03 19:54:50 +00:00
{
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 >
) ;
} ,
2015-01-30 01:10:49 +00:00
} ,
2017-10-03 19:54:50 +00:00
{
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 >
) ;
} ,
2015-01-30 01:10:49 +00:00
} ,
2017-10-03 19:54:50 +00:00
{
title : 'Font Weight' ,
render : function ( ) {
return (
< View >
< Text style = { { fontSize : 20 , fontWeight : '100' } } >
Move fast and be ultralight
< / T e x t >
< Text style = { { fontSize : 20 , fontWeight : '200' } } >
Move fast and be light
< / T e x t >
< Text style = { { fontSize : 20 , fontWeight : 'normal' } } >
Move fast and be normal
< / T e x t >
< Text style = { { fontSize : 20 , fontWeight : 'bold' } } >
Move fast and be bold
< / T e x t >
< Text style = { { fontSize : 20 , fontWeight : '900' } } >
Move fast and be ultrabold
< / T e x t >
< / V i e w >
) ;
} ,
2015-03-26 01:19:28 +00:00
} ,
2017-10-03 19:54:50 +00:00
{
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
} ,
2017-10-03 19:54:50 +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
you click - and - hold .
< / T e x t >
< / V i e w >
) ;
} ,
2016-11-18 00:09:43 +00:00
} ,
2017-10-03 19:54:50 +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-07-07 13:03:56 +00:00
} ,
2017-10-03 19:54:50 +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-01-30 01:10:49 +00:00
< / T e x t >
2015-11-04 16:52:58 +00:00
)
2015-11-04 16:52:46 +00:00
< / T e x t >
2017-10-03 19:54:50 +00:00
< Text style = { { opacity : 0.7 } } >
( opacity
2015-11-04 16:52:58 +00:00
< Text >
( is inherited
2017-10-03 19:54:50 +00:00
< Text style = { { opacity : 0.7 } } >
( and accumulated
< Text style = { { backgroundColor : '#ffaaaa' } } >
( and also applies to the background )
2015-11-04 16:52:58 +00:00
< / T e x t >
2017-10-03 19:54:50 +00:00
)
< / T e x t >
2015-11-04 16:52:58 +00:00
)
< / T e x t >
2017-10-03 19:54:50 +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 >
) ;
} ,
2015-01-30 01:10:49 +00:00
} ,
2017-10-03 19:54:50 +00:00
{
title : 'Text Align' ,
render : function ( ) {
return (
< View >
< Text > auto ( default ) - english LTR < / T e x t >
< Text >
{ '\u0623\u062D\u0628 \u0627\u0644\u0644\u063A\u0629 ' +
'\u0627\u0644\u0639\u0631\u0628\u064A\u0629 auto (default) - arabic ' +
'RTL' }
< / T e x t >
< 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 >
< Text style = { { textAlign : 'justify' } } >
justify : this text component { "'" } s contents are laid out with
"textAlign: justify" 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 >
< / V i e w >
) ;
} ,
2015-01-30 01:10:49 +00:00
} ,
2017-10-03 19:54:50 +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 >
Implement letterSpacing on Android >= 5.0
Summary:
`letterSpacing` is completely missing from RN Android at the moment.
I've reviewed the `letterSpacing` implementations in #13199, #13877 and #16801 (that all seem to have stalled) and managed to put together an improved one based on #13199, updated to merge cleanly post https://github.com/facebook/react-native/commit/6114f863c3aed826355530bcf404ee5aed2f927b, that resolves the [issues](https://github.com/facebook/react-native/pull/13199#issuecomment-354568863) I've identified with that code.
I believe this is the closest PR yet to a correct implementation of this feature, with a few caveats:
- As with the other PRs, this only works on Android >= 5.0 (silently falling back to no letter spacing on older versions). Is this acceptable for a RN feature, in general? Would a dev mode warning be desirable?
- The other PRs seem to have explored the space of potential solutions to the layout issue ([Android renders space _around_ glyphs](https://issuetracker.google.com/issues/37079859), iOS to the _right_ of each one) and come up empty, so I've opted to merely document the difference.
- I have neither updated nor tested the "Flat" UI implementation - everything compiles but I've taken [this comment](https://github.com/facebook/react-native/issues/12770#issuecomment-294052694) to mean there's no point in trying to wade through it on my own right now; I'm happy to tackle it if given some pointers.
- The implementation in `ReactEditText` is only there to handle the placeholder text, as `ReactBaseTextShadowNode` already affects the input control's contents correctly.
- I'm not sure whether `<TextInput>` is meant to respect `allowFontScaling`; I've taken my cue here from `ReactTextInputManager.setFontSize()`, and used the same units (SP) to interpret the value in `ReactEditText.setLetterSpacingPt()`.
- I'm not sure whether `<TextInput>` is even meant to support `letterSpacing` - it doesn't actually work on iOS. I'm not going to be able to handle the Objective-C side of this, not as part of this PR at least.
- I have not added unit tests to `ReactTextTest` - is this desirable? I see that some other props such as `lineHeight` aren't covered there (unless I'm not looking in the right place).
- Overall, I'm new to this codebase, so it's likely I've missed something not mentioned here.
Note comment re: unit tests above; RNTester screenshots follow.
| iOS (existing functionality, amended test) | Android (new functionality & test) |
| - | - |
| <img src=https://user-images.githubusercontent.com/2246565/34458459-c8d59498-edcb-11e7-8c8f-e7426f723886.png width=300> | <img src=https://user-images.githubusercontent.com/2246565/34458473-2a1ca368-edcc-11e7-9ce6-30c6d3a48660.png width=300> |
| iOS _(not implemented, test not in this branch)_ | Android (new functionality & test) |
| - | - |
| <img src=https://user-images.githubusercontent.com/2246565/34458481-6c60a36e-edcc-11e7-9af5-9734dd722ced.png width=300> | <img src=https://user-images.githubusercontent.com/2246565/34458486-8b3cdcf8-edcc-11e7-974b-25c6085fa674.png width=300> |
| iOS _(not implemented, test not in this branch)_ | Android (new functionality & test) |
| - | - |
| <img src=https://user-images.githubusercontent.com/2246565/34458492-d69a77be-edcc-11e7-896f-21212621dbee.png width=300> | <img src=https://user-images.githubusercontent.com/2246565/34458490-b3a1139e-edcc-11e7-88c8-79d4430d1514.png width=300> |
https://github.com/facebook/react-native-website/pull/105 - this docs PR is edited slightly from what's in `TextStylePropTypes` here; happy to align either one to the other after a review.
[ANDROID] [FEATURE] [Text] - Implemented letterSpacing
Closes https://github.com/facebook/react-native/pull/17398
Reviewed By: mdvacca
Differential Revision: D6837718
Pulled By: hramos
fbshipit-source-id: 5c9d49e9cf4af6457b636416ce5fe15315aab72c
2018-02-27 22:26:20 +00:00
< View style = { { flexDirection : 'row' } } >
2018-05-11 20:32:37 +00:00
< Text
style = { {
fontSize : 12 ,
letterSpacing : 2 ,
backgroundColor : 'fuchsia' ,
marginTop : 5 ,
} } >
Implement letterSpacing on Android >= 5.0
Summary:
`letterSpacing` is completely missing from RN Android at the moment.
I've reviewed the `letterSpacing` implementations in #13199, #13877 and #16801 (that all seem to have stalled) and managed to put together an improved one based on #13199, updated to merge cleanly post https://github.com/facebook/react-native/commit/6114f863c3aed826355530bcf404ee5aed2f927b, that resolves the [issues](https://github.com/facebook/react-native/pull/13199#issuecomment-354568863) I've identified with that code.
I believe this is the closest PR yet to a correct implementation of this feature, with a few caveats:
- As with the other PRs, this only works on Android >= 5.0 (silently falling back to no letter spacing on older versions). Is this acceptable for a RN feature, in general? Would a dev mode warning be desirable?
- The other PRs seem to have explored the space of potential solutions to the layout issue ([Android renders space _around_ glyphs](https://issuetracker.google.com/issues/37079859), iOS to the _right_ of each one) and come up empty, so I've opted to merely document the difference.
- I have neither updated nor tested the "Flat" UI implementation - everything compiles but I've taken [this comment](https://github.com/facebook/react-native/issues/12770#issuecomment-294052694) to mean there's no point in trying to wade through it on my own right now; I'm happy to tackle it if given some pointers.
- The implementation in `ReactEditText` is only there to handle the placeholder text, as `ReactBaseTextShadowNode` already affects the input control's contents correctly.
- I'm not sure whether `<TextInput>` is meant to respect `allowFontScaling`; I've taken my cue here from `ReactTextInputManager.setFontSize()`, and used the same units (SP) to interpret the value in `ReactEditText.setLetterSpacingPt()`.
- I'm not sure whether `<TextInput>` is even meant to support `letterSpacing` - it doesn't actually work on iOS. I'm not going to be able to handle the Objective-C side of this, not as part of this PR at least.
- I have not added unit tests to `ReactTextTest` - is this desirable? I see that some other props such as `lineHeight` aren't covered there (unless I'm not looking in the right place).
- Overall, I'm new to this codebase, so it's likely I've missed something not mentioned here.
Note comment re: unit tests above; RNTester screenshots follow.
| iOS (existing functionality, amended test) | Android (new functionality & test) |
| - | - |
| <img src=https://user-images.githubusercontent.com/2246565/34458459-c8d59498-edcb-11e7-8c8f-e7426f723886.png width=300> | <img src=https://user-images.githubusercontent.com/2246565/34458473-2a1ca368-edcc-11e7-9ce6-30c6d3a48660.png width=300> |
| iOS _(not implemented, test not in this branch)_ | Android (new functionality & test) |
| - | - |
| <img src=https://user-images.githubusercontent.com/2246565/34458481-6c60a36e-edcc-11e7-9af5-9734dd722ced.png width=300> | <img src=https://user-images.githubusercontent.com/2246565/34458486-8b3cdcf8-edcc-11e7-974b-25c6085fa674.png width=300> |
| iOS _(not implemented, test not in this branch)_ | Android (new functionality & test) |
| - | - |
| <img src=https://user-images.githubusercontent.com/2246565/34458492-d69a77be-edcc-11e7-896f-21212621dbee.png width=300> | <img src=https://user-images.githubusercontent.com/2246565/34458490-b3a1139e-edcc-11e7-88c8-79d4430d1514.png width=300> |
https://github.com/facebook/react-native-website/pull/105 - this docs PR is edited slightly from what's in `TextStylePropTypes` here; happy to align either one to the other after a review.
[ANDROID] [FEATURE] [Text] - Implemented letterSpacing
Closes https://github.com/facebook/react-native/pull/17398
Reviewed By: mdvacca
Differential Revision: D6837718
Pulled By: hramos
fbshipit-source-id: 5c9d49e9cf4af6457b636416ce5fe15315aab72c
2018-02-27 22:26:20 +00:00
With size and background color
< / T e x t >
< / V i e w >
2017-10-03 19:54:50 +00:00
< Text style = { { letterSpacing : - 1 , marginTop : 5 } } >
letterSpacing = - 1
< / T e x t >
2018-05-11 20:32:37 +00:00
< Text
style = { {
letterSpacing : 3 ,
backgroundColor : '#dddddd' ,
marginTop : 5 ,
} } >
Implement letterSpacing on Android >= 5.0
Summary:
`letterSpacing` is completely missing from RN Android at the moment.
I've reviewed the `letterSpacing` implementations in #13199, #13877 and #16801 (that all seem to have stalled) and managed to put together an improved one based on #13199, updated to merge cleanly post https://github.com/facebook/react-native/commit/6114f863c3aed826355530bcf404ee5aed2f927b, that resolves the [issues](https://github.com/facebook/react-native/pull/13199#issuecomment-354568863) I've identified with that code.
I believe this is the closest PR yet to a correct implementation of this feature, with a few caveats:
- As with the other PRs, this only works on Android >= 5.0 (silently falling back to no letter spacing on older versions). Is this acceptable for a RN feature, in general? Would a dev mode warning be desirable?
- The other PRs seem to have explored the space of potential solutions to the layout issue ([Android renders space _around_ glyphs](https://issuetracker.google.com/issues/37079859), iOS to the _right_ of each one) and come up empty, so I've opted to merely document the difference.
- I have neither updated nor tested the "Flat" UI implementation - everything compiles but I've taken [this comment](https://github.com/facebook/react-native/issues/12770#issuecomment-294052694) to mean there's no point in trying to wade through it on my own right now; I'm happy to tackle it if given some pointers.
- The implementation in `ReactEditText` is only there to handle the placeholder text, as `ReactBaseTextShadowNode` already affects the input control's contents correctly.
- I'm not sure whether `<TextInput>` is meant to respect `allowFontScaling`; I've taken my cue here from `ReactTextInputManager.setFontSize()`, and used the same units (SP) to interpret the value in `ReactEditText.setLetterSpacingPt()`.
- I'm not sure whether `<TextInput>` is even meant to support `letterSpacing` - it doesn't actually work on iOS. I'm not going to be able to handle the Objective-C side of this, not as part of this PR at least.
- I have not added unit tests to `ReactTextTest` - is this desirable? I see that some other props such as `lineHeight` aren't covered there (unless I'm not looking in the right place).
- Overall, I'm new to this codebase, so it's likely I've missed something not mentioned here.
Note comment re: unit tests above; RNTester screenshots follow.
| iOS (existing functionality, amended test) | Android (new functionality & test) |
| - | - |
| <img src=https://user-images.githubusercontent.com/2246565/34458459-c8d59498-edcb-11e7-8c8f-e7426f723886.png width=300> | <img src=https://user-images.githubusercontent.com/2246565/34458473-2a1ca368-edcc-11e7-9ce6-30c6d3a48660.png width=300> |
| iOS _(not implemented, test not in this branch)_ | Android (new functionality & test) |
| - | - |
| <img src=https://user-images.githubusercontent.com/2246565/34458481-6c60a36e-edcc-11e7-9af5-9734dd722ced.png width=300> | <img src=https://user-images.githubusercontent.com/2246565/34458486-8b3cdcf8-edcc-11e7-974b-25c6085fa674.png width=300> |
| iOS _(not implemented, test not in this branch)_ | Android (new functionality & test) |
| - | - |
| <img src=https://user-images.githubusercontent.com/2246565/34458492-d69a77be-edcc-11e7-896f-21212621dbee.png width=300> | <img src=https://user-images.githubusercontent.com/2246565/34458490-b3a1139e-edcc-11e7-88c8-79d4430d1514.png width=300> |
https://github.com/facebook/react-native-website/pull/105 - this docs PR is edited slightly from what's in `TextStylePropTypes` here; happy to align either one to the other after a review.
[ANDROID] [FEATURE] [Text] - Implemented letterSpacing
Closes https://github.com/facebook/react-native/pull/17398
Reviewed By: mdvacca
Differential Revision: D6837718
Pulled By: hramos
fbshipit-source-id: 5c9d49e9cf4af6457b636416ce5fe15315aab72c
2018-02-27 22:26:20 +00:00
[ letterSpacing = 3 ]
< Text style = { { letterSpacing : 0 , backgroundColor : '#bbbbbb' } } >
[ Nested letterSpacing = 0 ]
< / T e x t >
< Text style = { { letterSpacing : 6 , backgroundColor : '#eeeeee' } } >
[ Nested letterSpacing = 6 ]
< / T e x t >
< / T e x t >
2017-10-03 19:54:50 +00:00
< / V i e w >
) ;
} ,
2015-01-30 01:10:49 +00:00
} ,
2017-10-03 19:54:50 +00:00
{
title : 'Spaces' ,
render : function ( ) {
return (
< Text >
2018-05-11 20:32:37 +00:00
A { 'generated' } { 'string' } and some & nbsp ; & nbsp ; & nbsp ; spaces
2017-10-03 19:54:50 +00:00
< / T e x t >
) ;
} ,
2017-10-02 18:03:00 +00:00
} ,
2017-10-03 19:54:50 +00:00
{
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 >
2017-10-02 19:12:10 +00:00
< / T e x t >
2017-10-03 19:54:50 +00:00
) ;
} ,
2017-10-02 19:12:10 +00:00
} ,
2017-10-03 19:54:50 +00:00
{
title : 'Empty Text' ,
description : "It's ok to have Text with zero or null children." ,
render : function ( ) {
return < Text / > ;
} ,
2017-10-02 19:12:10 +00:00
} ,
2017-10-03 19:54:50 +00:00
{
title : 'Toggling Attributes' ,
render : function ( ) : React . Element < any > {
return < AttributeToggler / > ;
} ,
2017-10-02 19:12:10 +00:00
} ,
2017-10-03 19:54:50 +00:00
{
title : 'backgroundColor attribute' ,
description : 'backgroundColor is inherited from all types of views.' ,
render : function ( ) {
return (
< 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 .
< / T e x t >
2015-01-30 01:10:49 +00:00
< / T e x t >
< / T e x t >
< / T e x t >
< / T e x t >
2017-10-03 19:54:50 +00:00
) ;
} ,
2015-01-30 01:10:49 +00:00
} ,
2017-10-03 19:54:50 +00:00
{
title : 'numberOfLines attribute' ,
render : function ( ) {
return (
< View >
< Text numberOfLines = { 1 } >
Maximum of one line , no matter how much I write here . If I keep
writing , it { "'" } ll just truncate after one line .
< / T e x t >
< Text numberOfLines = { 2 } style = { { marginTop : 20 } } >
Maximum of two lines , no matter how much I write here . If I keep
writing , it { "'" } ll just truncate after two lines .
< / T e x t >
< Text style = { { marginTop : 20 } } >
No maximum lines specified , no matter how much I write here . If I
keep writing , it { "'" } ll just keep going and going .
< / T e x t >
< / V i e w >
) ;
} ,
2015-01-30 01:10:49 +00:00
} ,
2017-10-03 19:54:50 +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 > { ' ' }
exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat .
< / T e x t >
< / V i e w >
) ;
} ,
2015-07-24 15:25:51 +00:00
} ,
2017-10-03 19:54:50 +00:00
{
title : 'allowFontScaling attribute' ,
render : function ( ) {
return (
< View >
< Text >
By default , text will respect Text Size accessibility setting on
2019-01-16 14:44:16 +00:00
iOS . It means that all font sizes will be increased or decreased
2017-10-03 19:54:50 +00:00
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 } } >
2018-05-11 20:32:37 +00:00
You can disable scaling for your Text component by passing { '"' } allowFontScaling = {
'{'
} false { '}"' } prop .
2017-10-03 19:54:50 +00:00
< / T e x t >
2019-01-16 14:44:16 +00:00
< Text allowFontScaling = { false } style = { { marginTop : 20 , fontSize : 15 } } >
This text will not scale . { ' ' }
< Text style = { { fontSize : 15 } } >
This text also won ' t scale because it inherits "allowFontScaling"
from its parent .
< / T e x t >
2017-10-03 19:54:50 +00:00
< / T e x t >
< / V i e w >
) ;
} ,
2015-07-31 14:37:12 +00:00
} ,
2017-10-03 19:54:50 +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-01-01 17:32:59 +00:00
} ,
2017-10-03 19:54:50 +00:00
{
title : 'Ellipsize mode' ,
render : function ( ) {
return (
< View >
< Text numberOfLines = { 1 } >
This very long text should be truncated with dots in the end .
< / T e x t >
< Text ellipsizeMode = "middle" numberOfLines = { 1 } >
This very long text should be truncated with dots in the middle .
< / T e x t >
< Text ellipsizeMode = "head" numberOfLines = { 1 } >
This very long text should be truncated with dots in the beginning .
< / T e x t >
< Text ellipsizeMode = "clip" numberOfLines = { 1 } >
This very looooooooooooooooooooooooooooong text should be clipped .
< / T e x t >
< / V i e w >
) ;
} ,
2016-06-10 11:26:45 +00:00
} ,
2017-10-03 19:54:50 +00:00
{
title : 'Font variants' ,
render : function ( ) {
return (
< View >
< Text style = { { fontVariant : [ 'small-caps' ] } } > Small Caps { '\n' } < / T e x t >
< Text
style = { {
2018-06-10 20:37:53 +00:00
fontFamily : Platform . isTV ? 'Times' : 'Hoefler Text' ,
2017-10-03 19:54:50 +00:00
fontVariant : [ 'oldstyle-nums' ] ,
} } >
Old Style nums 0123456789 { '\n' }
< / T e x t >
< Text
style = { {
2018-06-10 20:37:53 +00:00
fontFamily : Platform . isTV ? 'Times' : 'Hoefler Text' ,
2017-10-03 19:54:50 +00:00
fontVariant : [ 'lining-nums' ] ,
} } >
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 >
) ;
} ,
} ,
{
title : 'Dynamic Font Size Adjustment' ,
render : function ( ) : React . Element < any > {
return < AdjustingFontSize / > ;
} ,
2016-08-09 15:42:58 +00:00
} ,
2017-10-03 19:54:50 +00:00
{
title : 'Text Align with RTL' ,
render : function ( ) {
return < TextAlignRTLExample / > ;
} ,
2016-08-10 18:14:36 +00:00
} ,
2018-02-16 01:40:10 +00:00
{
2018-05-11 20:32:37 +00:00
title : "Text `alignItems: 'baseline'` style" ,
2018-02-16 01:40:10 +00:00
render : function ( ) {
return < TextBaseLineLayoutExample / > ;
2018-05-11 20:32:37 +00:00
} ,
2018-04-16 15:59:26 +00:00
} ,
{
title : 'Transform' ,
render : function ( ) {
return (
< View >
2018-05-11 20:32:37 +00:00
< Text style = { { textTransform : 'uppercase' } } >
2018-04-16 15:59:26 +00:00
This text should be uppercased .
< / T e x t >
2018-05-11 20:32:37 +00:00
< Text style = { { textTransform : 'lowercase' } } >
2018-04-16 15:59:26 +00:00
This TEXT SHOULD be lowercased .
< / T e x t >
2018-05-11 20:32:37 +00:00
< Text style = { { textTransform : 'capitalize' } } >
2018-04-16 15:59:26 +00:00
This text should be CAPITALIZED .
< / T e x t >
2018-05-11 20:32:37 +00:00
< Text style = { { textTransform : 'capitalize' } } >
Mixed : < Text style = { { textTransform : 'uppercase' } } > uppercase < / T e x t >
< Text style = { { textTransform : 'lowercase' } } > LoWeRcAsE < / T e x t >
< Text style = { { textTransform : 'capitalize' } } >
2018-04-16 15:59:26 +00:00
capitalize each word
< / T e x t >
< / T e x t >
2018-05-11 20:32:37 +00:00
< Text >
Should be "ABC" :
< Text style = { { textTransform : 'uppercase' } } >
a < Text > b < / T e x t > c
< / T e x t >
2018-04-16 15:59:26 +00:00
< / T e x t >
2018-05-11 20:32:37 +00:00
< Text >
Should be "AbC" :
< Text style = { { textTransform : 'uppercase' } } >
a < Text style = { { textTransform : 'none' } } > b < / T e x t > c
< / T e x t >
2018-04-16 15:59:26 +00:00
< / T e x t >
< / V i e w >
) ;
2018-02-16 01:40:10 +00:00
} ,
} ,
2017-10-03 19:54:50 +00:00
] ;