473,770 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.divnam e.left
that gives me the actual location of this text?

Thanks a lot in advance,
-Wojtek

Jul 20 '05 #1
2 1602
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.divnam e.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.getEle mentByID
or other DOM commands you can read its size (object.offsetW idth,
object.offsetHe ight) and its relative position (object.offsetL eft,
object.offsetTo p). 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.getEle mentById('objec tname');
var oX = obj.offsetLeft;
var oY = obj.offsetTop;

// add position of the parent object
var parent = obj.offsetParen t;
while(parent && parent!=null)
{
oX += parent.offsetLe ft;
oY += parent.offsetTo p;
parent = parent.offsetPa rent;
}
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.d e> 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.divnam e.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.******@blueyo nder.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
3708
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 to extend the program with C routine, I mean. -- Good hack, Alberto Santini (http://www.albertosantini.it/)
3
2823
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 code below works in mac/win firefox, other browsers don't handle it at all. I have tried using onclick, and that buggers it in all browsers. <script type="text/javascript"> function updateSize()
2
2536
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 database (1GB I expect) is over 1GB by calculating the maximal number of data sets (tuple) added: 1. Is the following calculation for estimating the mdb file size in Access 97 correct: 1 integer field = 4 Byte
16
2228
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
2432
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 <div id="divResText1_id" style=" font-family: Arial,sans-serif; text-align: center; font-size: 14px;"> The search returned </div> <div id="divResTextCnt_id" style=" font-size: 20px; font-weight:bold;">
12
14516
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, that has limited width but multiple lines. Currently I am using the left$() function to trim the text to a standard number of characters. I want a function to return the number of characters that will fit in the width of the control. To keep the...
1
1415
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) Lets say i have a sentence(english or hebrew), i want the textbox to expand and minimize if i change the font's size.
14
37096
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 most browsers always increases the spacing between the line of text containing these elements and those above/below that lack it. I can easily reduce the size of the subscript or superscript
2
1451
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 System.EventArgs) Handles Button2.Click
0
9617
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10099
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10037
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9904
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8931
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7456
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2849
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.