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

Neatly truncating text to fit a physical dimension

Does anyone know of a way of neatly truncate a line of text at a given
physical point, i.e. a width specified in pixels. By neatly I mean not
by simply using the overflow CSS property but to truncate the text on
a word boundary and maybe add a '...' or 'cont.'

Many thanks,
Geoff
Jul 20 '05 #1
2 2773
Geoff Soper hu kiteb:
Does anyone know of a way of neatly truncate a line of text at a given
physical point, i.e. a width specified in pixels. By neatly I mean not
by simply using the overflow CSS property but to truncate the text on
a word boundary and maybe add a '...' or 'cont.'


I don't think this is possible. aiui, this would require that javascript
be able to detect the properties of the font, as well as the size it has
been instructed to display it at, and I think knowing both of those is
beyond javascript.
--
--
Fabian
Visit my website often and for long periods!
http://www.lajzar.co.uk

Jul 20 '05 #2
"Fabian" <la****@hotmail.com> writes:
I don't think this is possible. aiui, this would require that javascript
be able to detect the properties of the font, as well as the size it has
been instructed to display it at, and I think knowing both of those is
beyond javascript.


Actually ... (entering unpractical mode)

You *could* start out with a long text in a div, set to not break, and
then keep shortening it and looking at its actual width until it is
shorter than the width you want to assign it to.

It won't be pretty, and I don't think I'd recommend it for anything
practical, but ... just for geeky completeness :)

---
<script type="text/javascript">
function cutToFit(elem) { // elem contains only text
var str = elem.firstChild.nodeValue;
var i = str.length;
while(elem.offsetWidth > elem.parentNode.offsetWidth) {
i--;
var newstr = str.substring(0,i)+"...";
elem.firstChild.nodeValue = newstr;
}
}
</script>
<div style="white-space:nowrap;overflow:visible;">
<span id="foo">
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad
minim veniam, quis nostrud exercitation ullamco laboris nisi ut
aliquip ex ea commodo consequat. Duis aute irure dolor in
reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
culpa qui officia deserunt mollit anim id est laborum.
</span>
</div>
<script type="text/javascript">
cutToFit(document.getElementById("foo"));
</script>
---
Works in Mozilla and Opera 7, not in IE 6 for some reason.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #3

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

Similar topics

34
by: E. Robert Tisdale | last post by:
Please find attached the physical constants header file physical.h It defines conversion factors to mks units. It might be used like this: > cat main.cc #include<iostream>...
14
by: jlemay | last post by:
if you look at http://www.emrose.net/contact1.html the last field is a textarea field and I can't make it align with the other text fields. it's pretty close, but this comes from trial and...
1
by: Jitesh Sinha | last post by:
Hi, I am running Windows 2003/ IIS 6.0. I was stuck with rather a abnormal behaviour of System.Web.mail class. It was truncating the message body after 3,071 character. The code i was testing...
3
by: Bradley | last post by:
I'm developing an application where I'm representing ASCII encrypted Hex values as a string displayed by the UI. In the line above each of the data strings, I want to have a string that contains...
7
by: Yeah | last post by:
Ever since I upgraded my Windows display to 120 DPI (kind of a necessary change), fonts have been clearer and graphics have been crisper. However, the 25% automatic increase in the text size on my...
1
by: Daniel Wilson | last post by:
We have a stored procedure that is giving us an XML representation (using FOR XML Explicit) of a sales order. Sometimes that evaluats to quite a long string. Unfortunately, this XML string is...
2
by: Bre035 | last post by:
I have created a Make-table query in Microsoft Access 2003 to retrieve comments for designated records. The queries runs fine, but it doesn't pull all the text from the comments column of the DB2...
1
by: eric.irestone | last post by:
I am working on an application and I am running into some interesting behavior. From what has been researched thus far IIS seems to not be fulfilling all of the expected body of a HTTP request. ...
1
by: Serenityquinn15 | last post by:
Hi! I have a little trouble about in truncating numbers. What i want to do is to get the two decimal places. here's the example: when i try to get the half of 20500.39 it shows like...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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:
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
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.