A component that provides 2D Text functionality.
Example use:
<ViroText
text="Hello World"
textAlign="left"
textAlignVertical="top"
textLineBreakMode="justify"
textClipMode="clipToBounds"
color="#ff0000"
width={2} height={2}
style={{fontFamily:"Arial", fontSize:20, fontWeight:400, fontStyle:"italic", color:"#0000FF"}}
position={[0,0,-5]}
/>
Typeface
The typeface to use for text is determined by the fontFamily
in its style
property. Not all platforms support all font families. If a font family is not supported on a given device, the system will fall back to using that platform's default system font.
The fontFamily string may contain a comma-separated list of typefaces. If so, the best typeface in the list will be chosen for each glyph in the text. For example, if fontFamily is set to "Roboto, NotoSansCJK", then Roboto will be used for all English glyphs and NotoSansCJK will be used for all Chinese, Japanese, and Korean glyphs.
Style and Weight
The typeface can be further customized via fontStyle
in the style
property: set this to "italic" to render the italicized version of the typeface, if available.
Finally, you can use fontWeight
in the style
property to specify the weight of the font, using numeric values ranging from 100 (ultra-light) to 900 (extra-black). Not all fonts support all weights, in which case Viro will render the nearest supported weight. Commonly supported weights are '400' (regular), and '700' (bold).
Sizing
Use the fontSize
in the text's style
property to size the text. The actual size of text in a scene is determined by three things:
-
The fontSize property: this determines the size of the font textures used. Increasing fontSize creates bigger, sharper fonts, but uses much more memory. Using too small fontSize will make the text appear blurry.
-
The scale of the text. Scaling the text can increase text size, but will make the fonts more blurry. Try to avoid scaling fonts where possible.
-
The distance of the text from the user. We are in 3D space, so the further away text is from the camera, the smaller it will appear. Use trial and error with your headset to discover the correct font sizes.
In general, try to use the smallest fontSize
possible that reaches the sharpness desired at the given distance from the camera.
3D Text
Set the extrusionDepth
of the text to a value greater than zero to enable 3D text. This property specifies the 'depth' of the 3D text in local node coordinates.
If the Text is rendered in 3D (if extrusionDepth > 0), then you can apply three materials to the text: material 0 will represent the front of the Text, material 1 will represent the back of the Text, and material 2 will represent the sides of the Text. You can set the colors of these materials to change the colors of different parts of the Text, or even set different light models on each material. For example:
// Inside render function
<ViroText fontSize=24
style={styles.boldFont} position={[0, 0, 0]}
width={20} height={5} extrusionDepth={8}
materials={["frontMaterial", "backMaterial", "sideMaterial"]}
text="Bold 3D Text (white, blue, red)" />
...
// Outside render function
var styles = StyleSheet.create({
boldFont: {
color: '#FFFFFF',
flex: 1,
textAlignVertical: 'center',
textAlign: 'center',
fontWeight: 'bold',
},
});
ViroMaterials.createMaterials({
frontMaterial: {
diffuseColor: '#FFFFFF',
},
backMaterial: {
diffuseColor: '#FF0000',
},
sideMaterial: {
diffuseColor: '#0000FF',
},
});
Outline and Drop Shadow
The outerStroke
property can be used to render an outline surrounding the text or a drop shadow extending to the bottom right of the text. These are often used to make the text more legible over busy backgrounds. The ``outerStroke``` lets you set the type of stroke (e.g. outline or drop-shadow), the width of the outer stroke, and the stroke's color.
The width of the outer stroke is given in pixels: a width of 2 displays a fairly standard outline or drop shadow.
The example below shows two text components, each with a different stroke:
// Thick red outline (width 8)
<ViroText fontSize=24
style={styles.italicFont} position={[0, 4, 0]}
width={20} height={5}
outerStroke={{type:"Outline", width:8, color:'#FF0000'}}
text="Thick red outline" />
// Thin grey drop shadow
<ViroText fontSize=24
style={styles.boldFont} position={[0, 3, 0]}
width={20} height={5}
outerStroke={{type:"DropShadow", width:2, color:'#444444'}}
text="Grey drop shadow" />
Bounding Box
Text is contained within a bounding box defined by its width
and height
properties. This bounding box is invisible, but determines how the text wraps, aligns, and clips. Note that width
and height
have no bearing on the size of the text.
Props
Optional Props
PropKey | PropType | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
animation | PropTypes.shape({ A collection of parameters that determine if this component should animate. For more information on animated components please see our Animation Guide. | ||||||||||
color | ColorPropType The color of the text. The default text color is white. Valid color formats are:
| ||||||||||
dragPlane | PropTypes.shape({ When a drag type of "FixedToPlane" is given, dragging is limited to a user defined plane. The dragging behavior is then configured by this property (specified by a point on the plane and its normal vector). You can also limit the maximum distance the dragged object is allowed to travel away from the camera/controller (useful for situations where the user can drag an object towards infinity). | ||||||||||
dragType | PropTypes.oneOf(["FixedDistance", "FixedToWorld"]) Determines the behavior of drag if onDrag is specified.
The default value is "FixedDistance". | ||||||||||
extrusionDepth | PropTypes.number Set the extrusion depth of the text. Set this to 0 to render 2D Text (e.g. bitmap text). If extrusionDepth is greater than 0, then the Text will be rendered in 3D: it will be vectorized with sides of length extrusionDepth. See above for more details. | ||||||||||
height | PropTypes.number The width of the the text in 3D world space. If not in a ViroFlexView the default is set to 1. | ||||||||||
ignoreEventHandling | PropTypes.bool When set to true, this control will ignore events and not prevent controls behind it from receiving event callbacks. The default value is false. | ||||||||||
lightReceivingBitMask | PropTypes.number A bit mask that is bitwise and-ed (&) with each light's influenceBitMask. If the result is > 0, then the light will illuminate this object. For more information please see the Lighting and Materials Guide. | ||||||||||
maxLines | PropTypes.number The maximum number of lines the text should take. The text will automatically be truncated if it exceeds the maxLines specified. | ||||||||||
onClick | React.PropTypes.func Called when an object has been clicked. Example code:
The position parameter represents the position in world coordinates on the object where the click occurred. For the mapping of sources to controller inputs, see the Events section. | ||||||||||
onClickState | React.PropTypes.func Called for each click state an object goes through as it is clicked. Supported click states and their values are the following:
Example code:
For the mapping of sources to controller inputs, see the Events section. | ||||||||||
onCollision | React.PropTypes.func Called when this component's physics body collides with another component's physics body. Also invoked by ViroScene/ViroARScene's
| ||||||||||
onDrag | React.PropTypes.func Called when the view is currently being dragged. The dragToPos parameter provides the current 3D location of the dragged object. Example code:
For the mapping of sources to controller inputs, see the Events section. Unsupported VR Platforms: Cardboard iOS | ||||||||||
onFuse | PropTypes.oneOfType
As shown above, onFuse takes one of the types - either a callback, or a dictionary with a callback and duration. It is called after the user hovers onto and remains hovered on the control for a certain duration of time, as indicated in timeToFuse that represents the duration of time in milliseconds. While hovering, the reticle will display a count down animation while fusing towards timeToFuse. Note that timeToFuse defaults to 2000ms. For example:
For the mapping of sources to controller inputs, see the Events section. | ||||||||||
onHover | React.PropTypes.func Called when the user hovers on or off the control. For example:
For the mapping of sources to controller inputs, see the Events section. | ||||||||||
onPinch | React.PropTypes.func Called when the user performs a pinch gesture on the control. When the pinch starts, the scale factor is set to 1 is relative to the points of the two touch points. For example:
pinchState can be the following values:
This event is only available in AR. | ||||||||||
onRotate | React.PropTypes.func Called when the user performs a rotation touch gesture on the control. Rotation factor is returned in degrees. When setting rotation, the rotation should be relative to it's current rotation, not set to the absolute value of the given rotationFactor. For example:
rotateState can be the following values:
This event is only available in AR. | ||||||||||
onScroll | React.PropTypes.func Called when the user performs a scroll action, while hovering on the control. For example:
For the mapping of sources to controller inputs, see the Events section. Unsupported VR Platforms: Cardboard(Android and iOS) | ||||||||||
onSwipe | React.PropTypes.func Called when the user performs a swipe gesture on the physical controller, while hovering on this control. For example:
For the mapping of sources to controller inputs, see the Events section. Unsupported VR Platforms: Cardboard(Android and iOS) | ||||||||||
onTouch | React.PropTypes.func Called when the user performs a touch action, while hovering on the control. Provides the touch state type, and the x/y coordinate at which this touch event has occurred.
For example:
For the mapping of sources to controller inputs, see the Events section. Unsupported VR Platforms: Cardboard(Android and iOS). | ||||||||||
onTransformUpdate | PropTypes.func A function that is invoked when the component moves and provides an array of numbers representing the component's position in world coordinates. | ||||||||||
outerStroke | PropTypes.shape({ Set an optional outer stroke type, width, and color to use for this text. The outer stroke can be used to render an outline surrounding the text or drop shadow extending to the bottom right of the text. These are often used to make the text more legible over busy backgrounds. The width of the outer stroke is given in pixels: a width of 2 displays a fairly standard outline or drop shadow. Note that outer strokes do not apply to 3D text (text with extrusion depth greater than zero). To remove the outer stroke, set the outerStroke to 'None'. In this case the outerStrokeWidth and outerStrokeColor are be ignored. | ||||||||||
physicsBody | PropTypes.shape({..physics.api..}), Creates and binds a physics body that is configured with the provided collection of physics properties associated with this control. For more information on physics components, please see the physics.api. | ||||||||||
position | PropTypes.arrayOf(PropTypes.number) Cartesian position of the text in 3D world space, specified as [x, y, z]. | ||||||||||
rotation | PropTypes.arrayOf(PropTypes.number) The rotation of the text around it's local axis specified as Euler angles [x, y, z]. Units for each angle are specified in degrees. | ||||||||||
rotationPivot | PropTypes.arrayOf(PropTypes.number) Cartesian position in [x,y,z] about which rotation is applied relative to the component's position. | ||||||||||
shadowCastingBitMask | PropTypes.number A bit mask that is bitwise and-ed (&) with each light's influenceBitMask. If the result is > 0, then this object will cast shadows from the light. For more information please see the Lighting and Materials Guide. | ||||||||||
style | stylePropType The style properties define the typeface, style, size, and weight of the font. See 'Style Props' below. Style properties also determine the position and scale of the component within a ViroFlexView. Please see the UI Controls & Flexbox guide and Styles reference for more information. | ||||||||||
text | PropTypes.string The text that should be displayed. | ||||||||||
textAlign | PropTypes.oneOf(['left', 'right', 'center']) Horizontal alignment of the text. Aligns with respect to the bounding box of this text defined by | ||||||||||
textAlignVertical | PropTypes.oneOf(['top', 'bottom', 'center']) Vertical alignment of the text. Aligns with respect to the bounding box of this text defined by | ||||||||||
textClipMode | PropTypes.oneOf(['none', 'clipToBounds']) Set to | ||||||||||
textLineBreakMode | PropTypes.oneOf(['wordwrap','charwrap','justify','none']) The line break mode to use for text wrapping. We process line breaks against the Set to Set to Set to | ||||||||||
transformBehaviors | PropTypes.arrayOf(PropTypes.string) An array of transform constraints that affect the transform of the object. For example, putting the value "billboard" will ensure the box is facing the user as the user rotates their head on any axis. This is useful for icons or text where you'd like the box to always face the user at a particular rotation. Allowed values(values are case sensitive):
| ||||||||||
width | PropTypes.number The width of the the text in 3D world space. If not in a ViroFlexView the default is set to 1. | ||||||||||
viroTag | PropTypes.string A tag given to other components when their physics body collides with this component's physics body. Refer to physics for more information. | ||||||||||
visible | PropTypes.bool False if the text should be hidden. By default the text is visible and this value is true. | ||||||||||
renderingOrder | PropTypes.number This determines the order in which this Node is rendered relative to other Nodes. Nodes with greater rendering orders are rendered last. The default rendering order is zero. For example, setting a Node's rendering order to -1 will cause the Node to be rendered before all Nodes with rendering orders greater than or equal to 0. |
Style Props
fontFamily | PropTypes.string The comma-separated names of the font families that should be used to render this ViroText. The font families, in conjunction with the font size, weight, and style, fully specify the font used for each glyph of the Text. The fontFamily string may contain a comma-separated list of typefaces. If so, the best typeface in the list will be chosen for each glyph in the Text. For example, if fontFamily is set to If a font family cannot be found, the default system typeface will be used in its place. |
---|---|
fontSize | PropTypes.number Set the point size of the font to use when rendering this ViroText. |
fontStyle | PropTypes.string Set the style of the font. Supported values are Note: not all fonts support all styles. If a style is not supported, Viro will fallback to using the default style. |
fontWeight | PropTypes.string Set the weight of the font. Supported values are:
Note: not all fonts support all weights. If a weight is not supported, Viro will instead render the nearest supported weight. Commonly supported weights are '400' (normal), and '700' (bold). |
Methods
async getTransformAsync() | ||||
---|---|---|---|---|
Async function that returns the component's transform (position, scale and rotation).
|
applyImpulse(force: arrayOf(number), position: arrayOf(number)) | ||||
---|---|---|---|---|
A function used with physics to apply an impulse (instantaneous) force to an object with a physics body.
|
applyTorqueImpulse(torque: arrayOf(number), position: arrayOf(number)) | ||||||
---|---|---|---|---|---|---|
A function used with physics to apply an impulse (instantaneous) torque to an object with a physics body.
|
setVelocity(velocity: arrayOf(number)) | ||||
---|---|---|---|---|
A function used with physics to set the velocity of an object with a physics body.
|
setNativeProps(nativeProps: object) | ||||
---|---|---|---|---|
A wrapper function around the native component's setNativeProps which allow users to set values on the native component without changing state/setting props and re-rendering. Refer to the React Native documentation on Direct Manipulation for more information.
For example, setting position natively would look like this:
|
Updated about a year ago