New topic: 

(RESOLVED) g.drawstring .. justify right...?

<http://forums.realsoftware.com/viewtopic.php?t=31465>

       Page 1 of 1
   [ 6 posts ]                 Previous topic | Next topic         Author  
Message       Wolfsnap           Post subject: (RESOLVED) g.drawstring .. 
justify right...?Posted: Mon Dec 07, 2009 10:29 pm                        
Joined: Thu May 10, 2007 10:25 pm
Posts: 41              Well, the subject pretty much says it - is there a way 
to utilize g.drawstring with a justification of "right"? I have a list of 
values that I am trying to place into a canvas with a g.drawstring command in 
the paint event of a canvas, but I would like to have the text aligned on the 
right? Can this be done? (All drawstring events that I've found start from the 
x, y(base) cordinates.
 Is it a matter of drawing the string, then reading the length of the string, 
then re-drawing it based on its length subtracted from its original X 
value...or something like that???????

Now I've REALLY confused myself...?      

    Last edited by Wolfsnap on Tue Dec 08, 2009 2:26 am, edited 1 time in 
total.   
                            Top               timhare           Post subject: 
Re: g.drawstring .. justify right...?Posted: Mon Dec 07, 2009 10:39 pm          
              
Joined: Fri Jan 06, 2006 3:21 pm
Posts: 6902
Location: Portland, OR  USA              You're most of the way there.  You 
don't need to draw the string to find its length.  The graphics object has a 
StringWidth method for that.  Find the width of the string and subtract that 
from the X position where you want the string justified, to find where to start 
drawing.  Draw the string at that position and it will be right justified.   
                            Top               Blake           Post subject: Re: 
g.drawstring .. justify right...?Posted: Mon Dec 07, 2009 11:46 pm              
          
Joined: Fri Sep 30, 2005 8:39 pm
Posts: 250
Location: Arlington, TX              Yup tim got it right, here is a quick 
example for you (or for anyone else who may run into this problem later)

Code:Sub Paint( g As Graphics)
  Dim text As String = "Hello World"
  g.DrawString( text, g.Width - g.StringWidth( text ) - 2, 15 )
End Sub
   
                            Top               serd83           Post subject: 
Re: g.drawstring .. justify right...?Posted: Tue Dec 08, 2009 1:20 am           
             
Joined: Thu Feb 22, 2007 7:08 pm
Posts: 771              Or you can use this method
Code:alignment as string = "left" // global...
Code:Sub DrawText(g as graphics, s as string)
  select case alignment
  case "left"
  g.drawstring s, 0, 10
  case "middle"
  g.drawstring s, g.width/2-g.stringwidth(s)/2, 10
  case "right"
  g.drawstring s, g.width-g.stringwidth(s), 10
  end select
End Sub

Canvas1.Paint:
Code:drawtext(g, "Hello World") // change Hello World to any text you want
If you want change the alignment, then assign it simple with left, middle or 
right. 
To make a quick test add a slider with maximum = 2 and use this in ValueChanged:
Code:select case me.value
case 0
  alignment = "left"
  canvas1.refresh
case 1
  alignment = "middle"
  canvas1.refresh
case 2
  alignment = "right"
  canvas1.refresh
end select
   
                            Top               DaveS           Post subject: Re: 
g.drawstring .. justify right...?Posted: Tue Dec 08, 2009 1:27 am               
                
Joined: Sun Aug 05, 2007 10:46 am
Posts: 1808
Location: San Diego, CA              I would suggest you change the Y position 
to be g.textascent instead of a hardcoded value....


such as 

g.drawstring s, g.width/2-g.stringwidth(s)/2, g.textascent

this way the font and font size are automatically adjust for in all directions  
   
_________________
Dave Sisemore
MacPro, OSX 10.5.8 RB2009r2  
                            Top               Wolfsnap           Post subject: 
Re: g.drawstring .. justify right...?Posted: Tue Dec 08, 2009 2:25 am           
             
Joined: Thu May 10, 2007 10:25 pm
Posts: 41              Wow - ALL of these were greatly helpful - and resolved 
my problem along with future problems!
Thanks all - without y'all, I'd be slap stuck!

Marc   
                            Top           Display posts from previous: All 
posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost 
timeSubject AscendingDescending          Page 1 of 1
   [ 6 posts ]     
-- 
Over 1500 classes with 29000 functions in one REALbasic plug-in collection. 
The Monkeybread Software Realbasic Plugin v9.3. 
http://www.monkeybreadsoftware.de/realbasic/plugins.shtml

[email protected]

Reply via email to