Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Is there a way to calculate the width (in pixels) of a string?

ANSWERED

I am referring to the width when drawn, e.g. how wide will "ABC" be if rendered in 40 point System font?

 

The solutions that I found online don't apply to FitBit OS's simplified SVG.

 

Thanks,

 

Florin

Best Answer
0 Votes
1 BEST ANSWER

Accepted Solutions

You can't, but you can get the width of the text element, using yourElement.getBBox().width

View best answer in original post

Best Answer
2 REPLIES 2

You can't, but you can get the width of the text element, using yourElement.getBBox().width

Best Answer

I would like to mention when using fitfont it is possible to get the actual width of the label just by accessing the width property:

const someLabel = new FitFont({ id: "someLabel", font: SmallFont, halign: "start"});
someLabel.text = "short";
console.log("Width: " + someLabel.width);
someLabel.text = "more text";
console.log("Width: " + someLabel.width);

 

Pretty neat and convenient to be honest:

[01:01:48]Width: 95    app/index.js:216,1
[01:01:48]Width: 165   app/index.js:218,1
Best Answer