473,387 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Get text height?

Is there a way to get the hight of text in a table using code?

Jun 9 '06 #1
4 7313
Kourosh wrote:
Is there a way to get the hight of text in a table using code?


Quite a strange question; this implies that you would do some
positioning in regards of the text size, instead of the containing
elements' size. What if the user resizes the window, therefore
potentially increasing the height of the table? I think that explaining
why you want this done could attract far better suggestions that those
proposed below :)
In IE, you can create a range around the text you want to measure, and
then retrieve the dimensions of the range, through the bounding properties.

In other user agents, you would have to determine the line-height of
your container; if you define it in a stylesheet, then reading it
through the styleSheets object could be effective, provided the user
does not alter it. However if you let it to the browser, then you'd have
to retrieve the float value for the computed CSS value of the
"line-height" property, and to date, I'm aware of no browser fully
implementing the methods that you'd use to do so for this specific
property.

Eventually, you could of course emulate such a process by creating one
cell on the fly, with similar CSS characteristics, containing only one
letter, and then measuring the line-height by removing appropriate insets...
---
<table>
<tr>
<td>Hello, World</td>
</tr>
</table>

<a href="#" onclick="alert(h()); return false">Text height</a>

<script type="text/javascript">
// Range-based, IE only
// We make the assumption that the height is uniform
// within the cell
function h(){
var c, d=document, r, h=-1;
if(
d &&
d.getElementsByTagName &&
d.body &&
d.body.createTextRange
){

c=d.getElementsByTagName("td")[0];
r=d.body.createTextRange();
r.moveToElementText(c);
r.collapse(true);
h=r.boundingHeight;
}
return h;
}
</script>
---
Jun 9 '06 #2
Ivo

"Elegie" <El****@invalid.com> schreef in bericht
news:44**********************@news.free.fr...
Kourosh wrote:
Is there a way to get the hight of text in a table using code?
Quite a strange question; ... think that explaining why you want this
done could attract far better suggestions that those proposed below :)

In IE, you can create a range around the text you want to measure, and
then retrieve the dimensions of the range, through the bounding
properties.

<snip> <script type="text/javascript">
// Range-based, IE only
// We make the assumption that the height is uniform
// within the cell
function h(){
var c, d=document, r, h=-1;
if(
d &&
d.getElementsByTagName &&
d.body &&
d.body.createTextRange
){

c=d.getElementsByTagName("td")[0];
r=d.body.createTextRange();
r.moveToElementText(c);
r.collapse(true);
h=r.boundingHeight;
}
return h;
}
</script>


Interesting, but in a quick test, even with this table:
<table>
<tr> <td><small>One</small></td> <td><big>Two</big></td> </tr>
</table>
the values returned for the textRange.boundingHeight and the containing
element.scrollHeight are the same under all circumstances and browser text
size settings. Since scrollHeight is not IE only, the one line answer seems
to do:
<script>
var cells = document.getElementsByTagName( 'td' );
window.alert( cells[0].firstChild.scrollHeight );
</script>

I 'd be interested to see scripts that look at the shape of the actual
letters and return a lower number for text that contains no ascenders
(bdfhklt) on the first line, or descenders (gjpqy) on the last. How round is
an O? No browser I am aware of gives access to the used fonts down to the
last pixel...

hth
ivo
http://www.yorick.onlyfools.com/
Jun 10 '06 #3
Ivo wrote:

Hi,
the values returned for the textRange.boundingHeight and the containing
element.scrollHeight are the same under all circumstances and browser text
size settings.
And what if the containing element has more than one line of text ;) The
range is actually the object that sticks the closest to the text, and
provided you collapse it and suppose a uniform height of the text within
the cell, you'll have a "decent" text height.

Your remark is however completely fair, we never measure the text but
rather use the dimensions of elements, especially in regards of CSS
basic principle of "fluidity", hence my hesitations as to what the OP
really wants to achieve.

I 'd be interested to see scripts that look at the shape of the actual
letters and return a lower number for text that contains no ascenders
(bdfhklt) on the first line, or descenders (gjpqy) on the last. How round is
an O? No browser I am aware of gives access to the used fonts down to the
last pixel...


I don't think you can do that, at least not in a normal security
environment without the help of an external component. Anyway, that's
not the philosophy of CSS or DOM ranges (the W3C ranges do not include
any measuring properties).

Kind regards.
Jun 10 '06 #4
Elegie wrote:
Kourosh wrote:
Is there a way to get the hight of text in a table using code?
Quite a strange question; this implies that you would do some
positioning in regards of the text size, instead of the containing
elements' size. What if the user resizes the window, therefore
potentially increasing the height of the table? I think that explaining
why you want this done could attract far better suggestions that those
proposed below :)


I don't find the question strange, but I agree that it would behoove
the author to add some words of explanation.
In IE, you can create a range around the text you want to measure, and
then retrieve the dimensions of the range, through the bounding properties.

In other user agents, you would have to determine the line-height of
your container; if you define it in a stylesheet, then reading it
through the styleSheets object could be effective, provided the user
does not alter it. However if you let it to the browser, then you'd have
to retrieve the float value for the computed CSS value of the
"line-height" property, and to date, I'm aware of no browser fully
implementing the methods that you'd use to do so for this specific
property.


Here is an alternate approach working in FF/IE, for when the
container's height is based on the included text, illustrated by the
below. So if there are multiple td's on a single row where the content
of another one has more lines, or if the height is fixed or not
otherwise form fitting, then one would need to be a little bit more
clever than the example.

Csaba Gabor from Vienna

<table border>
<tr><td id=me style="font-weight:bold;line-height:23px"
onclick="alert(lineHeight(this))">
Hello, World<br>Goodbye for now</td></tr>
<tr><td id=me onclick="alert(lineHeight(this))">
Click in<br>the table<br>to get the line height</td></tr>
</table>

<script type="text/javascript">
// Insertion-base, generic
function lineHeight(elem) {
var addendum = "<br>x";
var h0 = elem.offsetHeight;
elem.innerHTML += addendum;
var h1 = elem.offsetHeight;
elem.innerHTML = elem.innerHTML.slice(0,-addendum.length);
return h1-h0;
}
</script>

Jun 10 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: IkBenHet | last post by:
Hello, I found this script to create a simple rich text form (http://programmabilities.com/xml/index.php?id=17): <html> <head> <title>Rich Text Editor</title> </head> <body>
0
by: VorTechS | last post by:
I'm having a problem with an inherited label, applying text rotation to aligned text. If text rotation is applied to the aligned text, the alignment goes 'nuts'. I can find no logic to what is...
3
by: David Golightly | last post by:
I'm taking a stab at making CSS sparklines - see http://www.edwardtufte.com/bboard/q-and-a-fetch-msg?msg_id=0001OR&topic_id=1&topic= <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC...
3
by: =?Utf-8?B?SlIx?= | last post by:
I would like to add text to an image. I have tried to use DrawString and it works on some images but on others it is very very small. I am pretty sure it has something to do with the size of the...
12
by: tim | last post by:
I am using foldoutmenu 3 and am having problems with viewing my menus in firefox. On my sub3 menus i have more than one line of text in some places. firefox does not recognise that there is more...
4
by: pablorp80 | last post by:
Hello, Here is what I need: I need the focus and the cursor set to a textbox named txtGT, every time no matter if it is the first page load or whether it is a postback. Here is the problem: I...
2
by: GloStix | last post by:
For some reason, FF likes to put a black underline on all my buttons. No matter what I do, it has the line I've tried displaying as block and cursor, anything.. Also I've been trying to get it so...
1
by: littlealex | last post by:
IE6 not displaying text correctly - IE 7 & Firefox 3 are fine! Need some help with this as fairly new to CSS! In IE6 the text for the following page doesn't display properly - rather than being...
27
matheussousuke
by: matheussousuke | last post by:
I'm having trouble with e-mail sending, I mean, the website is suposed to send a confirmation email after sign up, but it gets like text plain instead of HTML May someone help me, please? ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.