Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Fixed Width Text in React Native
Spencer Carli
Spencer Carli

Posted on • Originally published atreactnativeschool.com

     

Fixed Width Text in React Native

Despite working on React Native daily for 7 years, I'm still constantly learning new things about it. Recently it was thefontVariant style prop on aText component.

This post waspublished a week ago on React Native School. Be sure to visit for the latest tips, tricks and tutorials on React Native!

I was working a multi-theme stop watch app in React Native (it'sopen source!) and ran into an issue where the width of each number was different.

Normally this is not a problem but when you are changing the numbers every millisecond it looks quite hectic.

Demo of Problem

To fix this is a simple one liner:

When styling the text, addfontVariant: ["tabular-nums"]. This will make the text a fixed width so your layout stays fixed.

Code from theexample app:

// ...conststyles=StyleSheet.create({// ...timeText:{fontSize:60,fontWeight:"300",marginTop:100,fontVariant:["tabular-nums"],// fixed width character},})exportdefaultStopWatch
Enter fullscreen modeExit fullscreen mode

Demo of Solution

MDN has a good resource through which you can learn more about thefont-variant CSS property.

Top comments(1)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
yongchanghe profile image
Yongchang He
An IoT and blockChain developer;interested in Cloud Computing;love coding...
  • Email
  • Location
    Saskatoon, Saskatchewan
  • Education
    Computer Science, master's, University of Saskatchewan
  • Joined

Thank you for sharing this!

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Student. Teacher. Firefighter. Pizza fiend. I teach others while teaching myself, typically with React Native.
  • Location
    Nashville, TN
  • Work
    Developer/Teacher
  • Joined

More fromSpencer Carli

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp