New topic: 

Label width

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

         Page 1 of 1
   [ 12 posts ]                 Previous topic | Next topic          Author  
Message        Lor          Post subject: Label widthPosted: Mon Nov 13, 2006 
11:03 pm                         
Joined: Mon Nov 13, 2006 10:56 pm
Posts: 24                Hey! 
 
Does anyone know how to change the width of a label to the size of the text (ie 
like an autowidth).  VB6 does it, but I can't find how to do it in REALbasic.  
My labels keep changing text, but if a label has a longer sting it doesn't show 
the whole thing.  Sentences are made up of labels (so I can use different 
colours).  I tried making the width set to the caption X lenght of the string X 
Constant, but this does makes a mess of spacing since letters are not all the 
same width.  IE  "i" is much smaller than "M". 
 
If someone could help that would be awesome!! 
 
Laura   
                             Top                Aaron Ballman          Post 
subject: Posted: Mon Nov 13, 2006 11:27 pm                                 
Joined: Wed Sep 28, 2005 8:39 am
Posts: 9341
Location: St Augusta, MN                Here's how I'd do it.  I'd make a 
module named StaticTextExtension, and add this method to it: 
 
Function AutoSize( extends s as StaticText )
  static p as new Picture( 1, 1, 32 )
  
  if s.Multiline then
  // Don't need to do anything -- it's a multiline StaticText
  else
  dim width as Integer = p.Graphics.StringWidth( s.Text )
  s.Width = width
  end if
End Function


Then you can use it like this:

MyStaticText.AutoSize 
 
For single-line StaticTexts, it will resize the text to fit the caption's 
width.  For multiline ones, it will leave them alone. 
 
Note that this will cause labels with a keyboard mnemonic to be slightly larger 
than they need to be, because the & will be counted in the string's width.  If 
that's a problem, then there are ways to work around it, but it makes the code 
more complex.   
                             Top                Lor          Post subject: 
Posted: Tue Nov 14, 2006 6:40 pm                         
Joined: Mon Nov 13, 2006 10:56 pm
Posts: 24                Hey Aaron, 
 
I tried that, but it never changed the width the text from how it is set in the 
non code view. 
 
An example of my code.... 
 
Label3.Caption = xtra(Z,0) 
Label3.AutoSize 
 
 
The size never changes using the code you gave for autosize.... 
Any suggestions?? 
 
Laura   
                             Top                Aaron Ballman          Post 
subject: Posted: Tue Nov 14, 2006 6:48 pm                                 
Joined: Wed Sep 28, 2005 8:39 am
Posts: 9341
Location: St Augusta, MN                Do you have Multiline checked?  What is 
Label3.Width before and after calling AutoSize?   
                             Top                Lor          Post subject: 
Posted: Thu Nov 16, 2006 6:51 pm                         
Joined: Mon Nov 13, 2006 10:56 pm
Posts: 24                Hi Aaron, 
 
Multiline wasn't checked off.  Without being multiline.....the text width is 
now larger, but not large enough for the string.  The original size of the 
label was 27 so it is a lot larger now with the autofit, but not large enough. 
 
The Font (if this makes a difference) is Garamond and the text size is 18. 
 
Thanks for your quick responses!! 
 
Laura   
                             Top                Aaron Ballman          Post 
subject: Posted: Thu Nov 16, 2006 6:56 pm                                 
Joined: Wed Sep 28, 2005 8:39 am
Posts: 9341
Location: St Augusta, MN                Ah, my code wasn't taking the font and 
size into account.  Oops! 
 
Function AutoSize( extends s as StaticText )
  static p as new Picture( 1, 1, 32 )
  
  if s.Multiline then
  // Don't need to do anything -- it's a multiline StaticText
  else
  p.Graphics.TextFont = s.TextFont
  p.Graphics.TextSize = s.TextSize
  
  dim width as Integer = p.Graphics.StringWidth( s.Text )
  s.Width = width
  end if
End Function
   
                             Top                Lor          Post subject: 
Posted: Thu Nov 16, 2006 7:16 pm                         
Joined: Mon Nov 13, 2006 10:56 pm
Posts: 24                It's bigger, but still too small.  I'm missing the 
last word.... 
If I increase the text size so it will say.... 
 
p.Graphics.TextSize = s.TextSize + 2 
 
The size is now big enough, but I don't think it's really an autofit....(too 
big when making sentences with different labels)   
                             Top                Aaron Ballman          Post 
subject: Posted: Thu Nov 16, 2006 8:06 pm                                 
Joined: Wed Sep 28, 2005 8:39 am
Posts: 9341
Location: St Augusta, MN                Do you have any other styles set?  Like 
Bold?  My code isn't taking that sort of thing into account either.   
                             Top                Lor          Post subject: 
Posted: Thu Nov 16, 2006 10:13 pm                         
Joined: Mon Nov 13, 2006 10:56 pm
Posts: 24                Yes the label is bold. 
 
Is there something I can do for that (other than take bold off)   
                             Top                Aaron Ballman          Post 
subject: Posted: Thu Nov 16, 2006 10:20 pm                                 
Joined: Wed Sep 28, 2005 8:39 am
Posts: 9341
Location: St Augusta, MN                p.Graphics.Bold = s.Bold, off the top 
of my head.   
                             Top                HardyMachia          Post 
subject: Re: Label widthPosted: Wed Dec 05, 2012 11:24 am                       
  
Joined: Mon Dec 11, 2006 12:25 pm
Posts: 51                Is it possible override the set 
label.text/label.caption so that AutoSize can be called each time the caption 
gets set? Then I only need to change all my labels to a subclass LabelAutoSize 
and not have to add a new call everywhere.      
_________________
http://www.catamount.com 
PocketMoney for iOS, OSX, Linux, Windows, and Android  
                             Top                HardyMachia          Post 
subject: Re: Label widthPosted: Wed Dec 05, 2012 12:57 pm                       
  
Joined: Mon Dec 11, 2006 12:25 pm
Posts: 51                I've created a subclass called LabelAutoSize that 
handles all of this.

You can download it from here: 
http://www.catamount.com/forums/viewtop ... =34&t=4463      
_________________
http://www.catamount.com 
PocketMoney for iOS, OSX, Linux, Windows, and Android  
                             Top             Display posts from previous: All 
posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost 
timeSubject AscendingDescending          Page 1 of 1
   [ 12 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