473,403 Members | 2,222 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,403 software developers and data experts.

real text size calculation?

Hello!

I've got a small problem here. I'm trying to write some code that would
generate a drop-down menue for me, where I'd just need to enter the
menu- and submenu items into an array. The items are to be displayed as
text. Now, I want to have the layers with the submenues to appear more
or less under the appropriate main menu items. For this I need to be
able to calculate how many pixels a given text uses up on the screen of
the user. That's all fine with IE and an unproportional font like
courier. The problem is, there is this nice setting that allows you to
change the diplay size of the text - which is pretty much disfunctional
in IE, but in mozilla, it works. Which means that I can't just say that
one letter in courier, size 2, is 4 pixels wide and be happy with my
calculation, because then somebody can just pop up with another text
diplay size and *boom* - my calculation is wrong again.

Now, is there a realistic way to find out how many pixels _exactly_ a
piece of text takes up on the user's screen? Or can I make the given
piece of text a DIV and then have some kind of document.divname.left
that gives me the actual location of this text?

Thanks a lot in advance,
-Wojtek

Jul 20 '05 #1
2 1588
grayFalcon wrote:
Hello!

I've got a small problem here. I'm trying to write some code that would
generate a drop-down menue for me, where I'd just need to enter the
menu- and submenu items into an array. The items are to be displayed as
text. Now, I want to have the layers with the submenues to appear more
or less under the appropriate main menu items. For this I need to be
able to calculate how many pixels a given text uses up on the screen of
the user. That's all fine with IE and an unproportional font like
courier. The problem is, there is this nice setting that allows you to
change the diplay size of the text - which is pretty much disfunctional
in IE, but in mozilla, it works. Which means that I can't just say that
one letter in courier, size 2, is 4 pixels wide and be happy with my
calculation, because then somebody can just pop up with another text
diplay size and *boom* - my calculation is wrong again.

Now, is there a realistic way to find out how many pixels _exactly_ a
piece of text takes up on the user's screen? Or can I make the given
piece of text a DIV and then have some kind of document.divname.left
that gives me the actual location of this text?


Just put your text in a <span> or <div> (a table cell my also do it).
Once yout get the reference to that object using document.getElementByID
or other DOM commands you can read its size (object.offsetWidth,
object.offsetHeight) and its relative position (object.offsetLeft,
object.offsetTop). Now you have to run up the DOM tree in order to get
the absolute position of your object.

// get position of an object
var obj = document.getElementById('objectname');
var oX = obj.offsetLeft;
var oY = obj.offsetTop;

// add position of the parent object
var parent = obj.offsetParent;
while(parent && parent!=null)
{
oX += parent.offsetLeft;
oY += parent.offsetTop;
parent = parent.offsetParent;
}
Regards, brotherli

--

brotherli

[ mails to nu**@brotherli.ch won't be read! replace nugs with endless. ]
Jul 20 '05 #2
On Sun, 15 Feb 2004 21:37:23 +0100, grayFalcon <fa******@gmx.de> wrote:

[snip]
Now, is there a realistic way to find out how many pixels _exactly_
a piece of text takes up on the user's screen? Or can I make the
given piece of text a DIV and then have some kind of
document.divname.left that gives me the actual location of this
text?


It is not possible to get font dimensions using JavaScript. With a fixed
piece of text, and trial and error, you can usually approximate width
using "em" dimensions (1em is the height of line, basically), but this
is of no use dynamically.

Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 20 '05 #3

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

Similar topics

2
by: Alberto Santini | last post by:
I ported a Jos Stam's demo about Fluid mech to check the difference of speed between C implementation and Python. I think I achieved good results with Python and there is space to improve, without...
3
by: chuck | last post by:
Hi, This is a test for a larger form i am working on. I want to have a real time display of the radio button value when selected. I tried with various event handlers with varying success. The...
2
by: kuhni | last post by:
Hi everybody! After searching newsgroups for a couple of hours, I now try asking directly (running the risk of asking again the same question). My problem is to predict when the size of the...
16
by: Rob | last post by:
Assume 3 controls on a form.... Textbox1, Textbox2, and Textbox3 If you want Textbox3 to equal Val(Textbox1) + Val(Textbox2).. How do you hardcode this to Textbox3 ?
12
by: one man army | last post by:
New to this, good programmer, picking up some js I wanted to set a line of text as text BOLD more text BOLD text BOLD I can set the text in javascript in a single <div>, but I tried this ...
12
by: paii, Ron | last post by:
Sorry about that last one. Does anyone know how to calculate the width a string of text for given Font name and size? I want to buildup a block of text strings to display in a unbound control,...
1
by: Oron.Morad | last post by:
Hello, I develop C#.NET (WinForm) over 2003server. I am looking for a way to adjust a component size (width pixels) according to a given text of a certain font (i.e font family, size and style)...
14
by: David C. Stone | last post by:
Is there a way in CSS to adjust the baseline position of text marked up with either <sup></supor <sub></sub>, relative to the surrounding text? Reason for asking is that the default rendering in...
2
by: Keith G Hicks | last post by:
I have the following code which runs fine Imports System.Drawing Partial Class _Default Inherits System.Web.UI.Page Protected Sub Button2_Click(ByVal sender As Object, ByVal e As...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.