# Center Strings inside two separate rectangles

**URL:** <https://psychtoolbox.discourse.group/t/center-strings-inside-two-separate-rectangles/339>\
**Category:** Uncategorized\
**Created:** [April 17, 2013, 9:06pm UTC](https://psychtoolbox.discourse.group/t/center-strings-inside-two-separate-rectangles/339 "2013-04-17T21:06:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![vrakotomalala](https://avatars.discourse-cdn.com/v4/letter/v/c6cbf5/32.png) [@vrakotomalala](https://psychtoolbox.discourse.group/u/vrakotomalala)\
**Post date:** [April 17, 2013, 9:06pm UTC](https://psychtoolbox.discourse.group/t/center-strings-inside-two-separate-rectangles/339/1 "2013-04-17T21:06:23Z")

</div>

Hi,  
I'd like to display two strings inside two separate rectangles on the same screen. How to center each string inside each rectangle ? I tried DrawFormatedText but this uses the whole screen to center in.  
  
Thank you

---

<div class="post-metadata">

**Author:** ![peter.scarfe](https://avatars.discourse-cdn.com/v4/letter/p/e9a140/32.png) [@peter.scarfe](https://psychtoolbox.discourse.group/u/peter.scarfe)\
**Post date:** [April 18, 2013, 12:36am UTC](https://psychtoolbox.discourse.group/t/center-strings-inside-two-separate-rectangles/339/2 "2013-04-18T00:36:11Z")

</div>

Read the help file associated with DrawFormattedText, you can position text anywhere on the screen.   
  

> --- In [psychtoolbox@yahoogroups.com](mailto:psychtoolbox@yahoogroups.com), "Veneree" \<vrakotomalala@...\> wrote:  
> \>  
> \> Hi,  
> \> I'd like to display two strings inside two separate rectangles on the same screen. How to center each string inside each rectangle ? I tried DrawFormatedText but this uses the whole screen to center in.  
> \>   
> \> Thank you  
> \>

---

<div class="post-metadata">

**Author:** ![matthdub](https://avatars.discourse-cdn.com/v4/letter/m/d6d6ee/32.png) [@matthdub](https://psychtoolbox.discourse.group/u/matthdub)\
**Post date:** [April 18, 2013, 7:17am UTC](https://psychtoolbox.discourse.group/t/center-strings-inside-two-separate-rectangles/339/3 "2013-04-18T07:17:36Z")

</div>

Hi,   
  
there are a handful of functions to help you.   
  
To compute the size or center of a rectangle:  
RectSize  
CenterRect  
CenterRectOnPoint  
  
To compute the size of a text string:  
TextBounds  
Screen('TextBounds', )  
  
Be aware that those functions differ in which text 'area' is defined. Their help is worth a look.   
  
Best,   
  
Matthieu  
  
  
  

> --- In [psychtoolbox@yahoogroups.com](mailto:psychtoolbox@yahoogroups.com), "Veneree" \<vrakotomalala@...\> wrote:  
> \>  
> \> Hi,  
> \> I'd like to display two strings inside two separate rectangles on the same screen. How to center each string inside each rectangle ? I tried DrawFormatedText but this uses the whole screen to center in.  
> \>   
> \> Thank you  
> \>

---

<div class="post-metadata">

**Author:** ![mariokleiner](https://avatars.discourse-cdn.com/v4/letter/m/13edae/32.png) [@mariokleiner](https://psychtoolbox.discourse.group/u/mariokleiner)\
**Post date:** [April 20, 2013, 6:09pm UTC](https://psychtoolbox.discourse.group/t/center-strings-inside-two-separate-rectangles/339/4 "2013-04-20T18:09:00Z")

</div>

I think having the ability to center or layout strings within an arbitrary target rectangle as requested by the OP would be generally useful, so i added a new optional 11th parameter 'rect' to DrawFormattedText(), which will allow to specify this. Default behaviour is to use the whole target window or texture, as in the past.  
  
-\> UpdatePsychtoolbox  
  
-mario  
  

> --- In [psychtoolbox@yahoogroups.com](mailto:psychtoolbox@yahoogroups.com), "Matthieu Dubois" \<matthdub@...\> wrote:  
> \>  
> \> Hi,   
> \>   
> \> there are a handful of functions to help you.   
> \>   
> \> To compute the size or center of a rectangle:  
> \> RectSize  
> \> CenterRect  
> \> CenterRectOnPoint  
> \>   
> \> To compute the size of a text string:  
> \> TextBounds  
> \> Screen('TextBounds', )  
> \>   
> \> Be aware that those functions differ in which text 'area' is defined. Their help is worth a look.   
> \>   
> \> Best,   
> \>   
> \> Matthieu  
> \>   
> \>   
> \>   
> \> --- In [psychtoolbox@yahoogroups.com](mailto:psychtoolbox@yahoogroups.com), "Veneree" \<vrakotomalala@\> wrote:  
> \> \>  
> \> \> Hi,  
> \> \> I'd like to display two strings inside two separate rectangles on the same screen. How to center each string inside each rectangle ? I tried DrawFormatedText but this uses the whole screen to center in.  
> \> \>   
> \> \> Thank you  
> \> \>  
> \>
