# textWidth() returns wrong width

**URL:** https://discourse.processing.org/t/textwidth-returns-wrong-width/44202
**Category:** Processing for Android
**Created:** [April 1, 2024, 4:48pm UTC](https://discourse.processing.org/t/textwidth-returns-wrong-width/44202 "2024-04-01T16:48:11Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![AniManOtaku](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/animanotaku/32/18733_2.png) [@AniManOtaku](https://discourse.processing.org/u/AniManOtaku)
#### Post date: [April 1, 2024, 4:48pm UTC](https://discourse.processing.org/t/textwidth-returns-wrong-width/44202/1 "2024-04-01T16:48:11Z")

</div>

The textWidth () function returns an invalid value. Is there any way to fix it?  
The screenshot shows that the line is longer than the text.

 ![Screenshot_2024-04-01-19-40-20-224_com.calsignlabs.apde.sketchpreview](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/d/5/d54d3e21e7e1418d75d16ea756102d9a0810f8a0.png)

code:

```auto
String str="jjjjjjjjjjjjjjjjjjjj";

void setup(){
  
}

void draw(){
  background(255);
  
  textSize(93);
  fill(0);
  text(str,50,500);
  strokeWeight(3);
  line(50,600,50+textWidth(str),600);
}

```

Thanks in advance!

---

<div class="post-metadata">

### Author: ![alptugan](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/alptugan/32/19406_2.png) [@alptugan](https://discourse.processing.org/u/alptugan)
#### Post date: [April 1, 2024, 7:05pm UTC](https://discourse.processing.org/t/textwidth-returns-wrong-width/44202/2 "2024-04-01T19:05:34Z")

</div>

Actually, there is a consistent deviation, not an invalid one, depending on the **font size**. I had this kind of issue. You can fix it by subtracting a multiplier depending on the font size. If you use a font other than the default font, you need to change this multiplier. You can determine the most appropriate value by trial and error. For your case;  
float delta = 93 \* 0.08;  
`line(50,600,50+textWidth(str) - delta, 600);`

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [April 1, 2024, 8:11pm UTC](https://discourse.processing.org/t/textwidth-returns-wrong-width/44202/3 "2024-04-01T20:11:37Z")

</div>

In my experience it works better when you use  
createFont and setFont so the text is defined

---

<div class="post-metadata">

### Author: ![AniManOtaku](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/animanotaku/32/18733_2.png) [@AniManOtaku](https://discourse.processing.org/u/AniManOtaku)
#### Post date: [April 3, 2024, 1:44pm UTC](https://discourse.processing.org/t/textwidth-returns-wrong-width/44202/4 "2024-04-03T13:44:41Z")

</div>

Thanks for the reply.  
I experimented with different fonts and different text. Unfortunately with some fonts textWidth() still doesn’t work well.

Code:

```auto
String str="Hello! I am AniMan Otaku, and I love Rikka Takanashi so much!";
PFont font;
String fontName="Monospaced";

void setup(){
  font=createFont(fontName,36);
  textFont(font);
  printArray(PFont.list());
  /*
    don't work well: 
    
    SansSerif
    SansSerif-Bold
    Serif
    Serif-Bold
  */
}

void draw(){
  background(255);
  
  fill(0);
  text(str,50,500);
  strokeWeight(3);
  line(50,600,50+textWidth(str),600);
}

void mouseDragged(){
  
  if(mouseX/5f>0)
  font=createFont(fontName,mouseX/10f);
  textFont(font);
}

```

And I’m wondering if Serif, SansSerif, Monospaced are the fonts installed on my phone, or are they the standard processing fonts?
