Fanning Software Consulting

Determining the Width of a Text String

Facebook Twitter RSS Google+

QUESTION: How can I determine the width of a text string, using the Text function?

ANSWER: Chris Torrence provides the answer to this function graphics question.

You can use the undocumented "GetTextDimensions" method. For example, like this.

   IDL> p = Plot(/test)
   IDL> t = Text(0, 1, 'Data Layer Text', /DATA, CLIP=0)  ; in the data layer
   IDL> Print, t.GetTextDimensions()
          14.931223     0.072726995      0.00000000
   IDL> t1 = Text(0,0, 'Annotation Layer Text')  ; in the annotation layer
   IDL> Print, t1.GetTextDimensions()
         0.30468721     0.054687291      0.00000000

For text in the "data" layer, the units will be in "data" units. For text in the "annotation" layer, the units will be normalized 0-1. You can use the ConvertCoord() method to convert between different coordinates.

Version of IDL used to prepare this article: IDL 8.2.3.

Written: 12 Oct 2014