I like to answer the question in a more general way, so that it is useful to a wider group of people.
There are the following macros which allow to store the width, height (the material above the baseline) and depth (the material below the baseline) of a given content.
\settowidth{\somelength}{<content>}
\settodepth{\somelength}{<content>}
\settoheight{\somelength}{<content>}
The calc package also provides one for the total height (height + depth):
\settototalheight{\somelength}{<content>}
as well as
\widthof{<content>}
\heightof{<content>}
\depthof{<content>}
\totalheightof{<content>}
which can be used directly inside \setlength or \addtolength.
If you need multiple dimension of the same content you can also store it in a box register and use its dimension directly (the above macros do this as well internally). These are dimension expressions and can be prefixed with a factor, e.g. .5\wd\mybox is half the width.
\newsavebox\mybox
\sbox{\mybox}{<content>}
\wd\mybox % width
\ht\mybox % height
\dp\mybox % depth
For the totalheight you need to add \ht\mybox and \dp\mybox together.
calcpackage may redefine some units. For example, I get an error using math unitmuwhen the package is loaded. – M.Reza Nov 22 '13 at 07:20mucan be used only in math and only for\mkernor\mskip. – egreg Nov 22 '13 at 10:04textposto specify width for thetextblock, but it seems like the with returned by\widthofcan not be taken bytextblock, with error showing Missing number, treated as zero. – zyy May 01 '20 at 19:44\widthofreturns a suitable result only in some places, one of which is the argument to\parbox. Unfortunatelytextposcannot use it, as far as I know. – egreg May 01 '20 at 19:58\settowidth{\dimen0}{<text>}and then passes\dimen0to the macro that has to use the width. – egreg May 01 '20 at 20:26textblocktakes in a relative value with no units while\widthofreturns with a unit, this is explained in the documentation oftextposwhere it talks about troubleshooting Missing number, treated as zero. – zyy May 02 '20 at 04:09textblockquite a long time ago; there istextpos*that accepts lengths, AFAIR. – egreg May 02 '20 at 08:26\parboxthat looks LaTeX to me. I modified the answer, so you can retract your undeserved downvote. – egreg Jun 23 '23 at 12:08