<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[M5. Lcd.textWidth does not center text exactly in the middle]]></title><description><![CDATA[<p dir="auto">I want to write a text centered in the middle. the text has a variable size. with M5. Lcd.textWidth, the text is not centered exactly in the middle (deviation about 5px). I doing something wrong? How can I center the text in height?</p>
<p dir="auto">void loop() {<br />
M5.Lcd.fillScreen(BLACK);                //set the default background color<br />
M5.Lcd.fillRoundRect(20, 50, 280, 140, 15, RED);<br />
M5.Lcd.fillRoundRect(30, 60, 260, 120, 15, BLACK);<br />
M5.Lcd.setTextSize(5);<br />
M5.Lcd.setTextColor(RED);<br />
for (int i=-1500; i &lt;= 2255; i++){<br />
String text = String(i);<br />
M5.Lcd.fillRoundRect(30, 60, 260, 120, 15, BLACK);<br />
M5.Lcd.drawString(text, (int)((M5.Lcd.width()/2)-(M5.Lcd.textWidth(text)/2)), (int)(M5.Lcd.height()/2), 2);<br />
delay(20);<br />
}<br />
}</p>
]]></description><link>https://community.m5stack.com/topic/4329/m5-lcd-textwidth-does-not-center-text-exactly-in-the-middle</link><generator>RSS for Node</generator><lastBuildDate>Wed, 22 Apr 2026 01:50:52 GMT</lastBuildDate><atom:link href="https://community.m5stack.com/topic/4329.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 28 May 2022 13:44:11 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to M5. Lcd.textWidth does not center text exactly in the middle on Mon, 30 May 2022 15:45:05 GMT]]></title><description><![CDATA[<p dir="auto">👍Thank you!</p>
]]></description><link>https://community.m5stack.com/post/17613</link><guid isPermaLink="true">https://community.m5stack.com/post/17613</guid><dc:creator><![CDATA[Stoni99]]></dc:creator><pubDate>Mon, 30 May 2022 15:45:05 GMT</pubDate></item><item><title><![CDATA[Reply to M5. Lcd.textWidth does not center text exactly in the middle on Mon, 30 May 2022 01:56:57 GMT]]></title><description><![CDATA[<p dir="auto">Hello@Stoni99</p>
<p dir="auto">m5-docs：LCD<br />
setTextDatum(), drawString(), drawCentreString()<br />
<a href="https://github.com/m5stack/m5-docs/blob/master/docs/en/api/lcd.md" target="_blank" rel="noopener noreferrer nofollow ugc">https://github.com/m5stack/m5-docs/blob/master/docs/en/api/lcd.md</a></p>
<p dir="auto">:</p>
<p dir="auto">A. Use the drawCentreString instruction.<br />
The height is therefore adjusted by hand.<br />
drawCentreString : fontname=2</p>
<p dir="auto">M5.Lcd.setTextSize(5);<br />
M5.Lcd.drawCentreString(text, M5.Lcd.width()/2, M5.Lcd.height()/2 -42 , 2);<br />
or<br />
M5.Lcd.setTextSize(4);<br />
M5.Lcd.drawCentreString(text, M5.Lcd.width()/2, M5.Lcd.height()/2 -34 , 2);</p>
<p dir="auto">:</p>
<p dir="auto">B. setTextDatum + drawString + Free_Fonts<br />
Free_Fonts.h : Beautiful font.<br />
<a href="https://github.com/m5stack/M5Stack/blob/master/examples/Advanced/Display/Free_Font_Demo/Free_Fonts.h" target="_blank" rel="noopener noreferrer nofollow ugc">https://github.com/m5stack/M5Stack/blob/master/examples/Advanced/Display/Free_Font_Demo/Free_Fonts.h</a></p>
<p dir="auto">#include &lt;Free_Fonts.h&gt;</p>
<p dir="auto">void setup(){<br />
M5.Lcd.setFreeFont(FMB24); // or FM24</p>
<p dir="auto">void loop(){<br />
M5.Lcd.setTextSize(1);<br />
M5.Lcd.setTextDatum(3);<br />
M5.Lcd.drawString(text, M5.Lcd.width()/2-M5.Lcd.textWidth(text)/2, M5.Lcd.height()/2 -3,1);</p>
]]></description><link>https://community.m5stack.com/post/17611</link><guid isPermaLink="true">https://community.m5stack.com/post/17611</guid><dc:creator><![CDATA[macsbug]]></dc:creator><pubDate>Mon, 30 May 2022 01:56:57 GMT</pubDate></item><item><title><![CDATA[Reply to M5. Lcd.textWidth does not center text exactly in the middle on Sun, 29 May 2022 12:10:07 GMT]]></title><description><![CDATA[<p dir="auto">OK - thank you!<br />
The height is therefore adjusted by hand.</p>
<p dir="auto">Other question:<br />
I can't append the "W" to the string. It is not displayed. Is there another possibility?</p>
<p dir="auto">for (int i=-1500; i &lt;= 2255; i++){<br />
String text = String(i)<strong>+"W";</strong><br />
M5.Lcd.fillRoundRect(30, 60, 260, 120, 15, BLACK);<br />
M5.Lcd.drawCentreString(text, M5.Lcd.width()/2, M5.Lcd.height()/2-30 , 7);      //Text auf x zentrieren (x,y,Schriftgrösse)<br />
<a href="//M5.Lcd.drawString" target="_blank" rel="noopener noreferrer nofollow ugc">//M5.Lcd.drawString</a>(text, (int)((M5.Lcd.width()/2)-(M5.Lcd.textWidth(text)/2)-15), (int)((M5.Lcd.height()/2)-40), 2);<br />
delay(1000);<br />
}</p>
]]></description><link>https://community.m5stack.com/post/17610</link><guid isPermaLink="true">https://community.m5stack.com/post/17610</guid><dc:creator><![CDATA[Stoni99]]></dc:creator><pubDate>Sun, 29 May 2022 12:10:07 GMT</pubDate></item><item><title><![CDATA[Reply to M5. Lcd.textWidth does not center text exactly in the middle on Sun, 29 May 2022 02:35:46 GMT]]></title><description><![CDATA[<p dir="auto">Hello@Stoni99</p>
<ol>
<li>Use the setTextDatum and drawString instructions.<br />
M5.Lcd.setTextDatum(3);<br />
M5.Lcd.drawString(text, (int)((M5.Lcd.width()/2)-(M5.Lcd.textWidth(text)/2)-15),(int)(M5.Lcd.height()/2-0), 2);</li>
</ol>
<p dir="auto">:<br />
2. Use the drawCentreString instruction.<br />
// M5.Lcd.setTextSize(5);<br />
// M5.Lcd.drawString(text, (int)((M5.Lcd.width()/2)-(M5.Lcd.textWidth(text)/2)), (int)(M5.Lcd.height()/2), 2);</p>
<p dir="auto">M5.Lcd.drawCentreString(text, M5.Lcd.width()/2, M5.Lcd.height()/2-40 , 8);</p>
]]></description><link>https://community.m5stack.com/post/17604</link><guid isPermaLink="true">https://community.m5stack.com/post/17604</guid><dc:creator><![CDATA[macsbug]]></dc:creator><pubDate>Sun, 29 May 2022 02:35:46 GMT</pubDate></item></channel></rss>