How best to truncate text to fit in a box?

Just starting out? Need help? Post your questions and find answers here.
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

How best to truncate text to fit in a box?

Post by davido »

I wish to write a program that displays input text in boxes on a CanvasGadget.
However, if the text is too long, I need to truncate it and add a suffix of ... that will just fit in the box.
I've attached some code I've used to test this. I think its: Obvious, simple, rather crude and probably very slow.
Can anyone suggest a more elegant and probably faster method?

Code: Select all

EnableExplicit

  CompilerIf #PB_Compiler_Unicode
    #XmlEncoding = #PB_UTF8
  CompilerElse 
    #XmlEncoding = #PB_Ascii
  CompilerEndIf

  #Dialog = 0
  #Xml = 0
  Global XML1$, XML2$, Event.i
  

  
  
  Runtime Enumeration Windows
    #WinMain
    #win2
  EndEnumeration
  Runtime Enumeration Gadgets
    #DrawingTextTest
  EndEnumeration
 
  
  Procedure DoTest()
    Protected h.i, w.i = 120, T$  = "This is a very long title To test" ;// Possibly a poor choice!
    Protected DotDotDotLen.i, ThreeDots$
    If StartDrawing(CanvasOutput(#DrawingTextTest))
      h = TextHeight("Ay")
      DotDotDotLen = TextWidth("...")
      Box(20,20,120,h,$dddddd)
      While TextWidth(T$) > 120 - DotDotDotLen
        T$ = Left(T$,Len(T$) - 1)
        ThreeDots$ = "..."
      Wend  
      DrawText(20,20,T$ + ThreeDots$,$000000,$ffffff)
      StopDrawing()
    Else
      MessageRequester("ERROR!","Cannot start drawing!")
    EndIf
    
  EndProcedure
  

  
  Runtime Procedure SetUP()
    DoTest()
  EndProcedure
  
  XML1$ = "<dialogs>"+
         "  <window id='#WinMain' name='Template' text='Window 1' minwidth='auto' minheight='auto' flags='#PB_Window_ScreenCentered |"+
         "  #PB_Window_SystemMenu | #PB_Window_SizeGadget'>" +
         "    <canvas id='#DrawingTextTest' width='400' height='200' />"+
         "  </window>"+
         "</dialogs>"
  
  If CatchXML(#Xml, @XML1$, StringByteLength(XML1$), 0, #XmlEncoding) And XMLStatus(#Xml) = #PB_XML_Success
    
    If CreateDialog(#Dialog) And OpenXMLDialog(#Dialog, #Xml, "Template")
      SetUp()
      Repeat
        Event = WaitWindowEvent()
      Until Event = #PB_Event_CloseWindow 
      
    Else  
      Debug "Dialog error: " + DialogError(#Dialog)
    EndIf
  Else
    Debug "XML error: " + XMLError(#Xml) + " (Line: " + XMLErrorLine(#Xml) + ")"
  EndIf
DE AA EB
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4636
Joined: Sun Apr 12, 2009 6:27 am

Re: How best to truncate text to fit in a box?

Post by RASHAD »

Hi davido

Code: Select all

Procedure DoTest()
  Protected h.i, w.i = 120, T$  = "This is a very long title To test" ;// Possibly a poor choice!
  Protected DotDotDotLen.i, ThreeDots$
  If StartDrawing(CanvasOutput(#DrawingTextTest))
    For k = 1 To 10
      text2$ = StringField(T$, k, " ") + " "
      text$ = text$ + text2$
      If TextWidth(text$) > w
        text$ = RemoveString(text$,text2$)+"...."
        Break
      EndIf
    Next
    DrawText(20,20,text$,$000000,$ffffff)
    StopDrawing()
  Else
    MessageRequester("ERROR!","Cannot start drawing!")
  EndIf   
EndProcedure
Egypt my love
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: How best to truncate text to fit in a box?

Post by davido »

Hi RASHAD,
Nice idea, thank you for sharing it. :D

I think I'll be using it as most of my boxes will use only short words.

I did notice that it fails if there is one extremely long word when only ... is displayed. :mrgreen:

I hope you noticed that your solution was posted very much quicker than this reply. Sorry.
DE AA EB
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4636
Joined: Sun Apr 12, 2009 6:27 am

Re: How best to truncate text to fit in a box?

Post by RASHAD »

Hi davido
If it is only for Windows there is a solution much easier and perfect.
Egypt my love
Post Reply