Current location: Hot Scripts Forums » General Web Coding » HTML/XHTML/XML » dynamically changing font with text size

dynamically changing font with text size

Reply
  #1 (permalink)  
Old 11-17-09, 01:27 AM
hemi hemi is offline
Newbie Coder
 
Join Date: Aug 2009
Posts: 62
Thanks: 3
Thanked 0 Times in 0 Posts
dynamically changing font with text size

hi

i got one doubt if we can change font size of a text dynamically

ex: if we give name as SACHIN TENDULKAR it should be shown as font size of some 40 or 60

and if we give another text as TENDULKAR TON HELPS INDIA TO WIN THE MATCH this should be automatically adjusted to font size of some 30 to 35.

I searched Google but i didn't find it if anyone one know plz help me out
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 11-17-09, 02:26 AM
hemi hemi is offline
Newbie Coder
 
Join Date: Aug 2009
Posts: 62
Thanks: 3
Thanked 0 Times in 0 Posts
i dont understand wht u r saying Mr.Pennicles01
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 11-17-09, 06:22 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 2,726
Thanks: 0
Thanked 32 Times in 32 Posts
Quote:
Originally Posted by hemi View Post
hi

i got one doubt if we can change font size of a text dynamically

ex: if we give name as SACHIN TENDULKAR it should be shown as font size of some 40 or 60

and if we give another text as TENDULKAR TON HELPS INDIA TO WIN THE MATCH this should be automatically adjusted to font size of some 30 to 35.

I searched Google but i didn't find it if anyone one know plz help me out
You will have to use javascript to dynamically change the font attributes.
Here is a simple example that checks how many characters of text is in a span element and changes the font size accordingly.

In this example, if the number of characters is 1 to 29 then the font size is set to 60.
If the number of characters is 30 to 59 then the font size is set to 30.
Else font size is set to 12.

HTML Code:
<html>
<head>
<script>
function changeFontSize()
{
 var elms = document.getElementsByTagName("span");
 for(var i = 0;i < elms.length;i++)
 {
  var elmsHtml = elms[i].innerHTML,FontSize,ln = elmsHtml.length;
  if(ln > 0 && ln < 30){FontSize = 60;}
  else if(ln > 29 && ln < 60){FontSize = 30;}
  else if(ln > 59){FontSize = 12;}
  elms[i].style.fontSize = FontSize;
  }
 }
</script>
</head>
<body>
<span style='font-size:12px;'>SACHIN TENDULKAR</span>
<br />
<span style='font-size:12px;'>TENDULKAR TON HELPS INDIA TO WIN THE MATCH</span>
<br />
<button onclick="changeFontSize()">Change font size</button>
</body>
</html>
Here is another example that will try to change the font size to match the containers width.
HTML Code:
<html>
<head>
<script>
function changeFontSize()
{
 var elms = document.getElementsByTagName("span");
 for(var i = 0;i < elms.length;i++)
 {
  var elmsHtml = elms[i].innerHTML;
  var widthFactor = parseInt(document.getElementById("container").style.width);
  var FontSize = 0;
  var ln = elmsHtml.length;
  FontSize = Math.ceil(widthFactor/ln*1.5);
  elms[i].style.fontSize = FontSize;
  }
 }
</script>
</head>
<body>
<div>
<center>
<div id="container" style="width:275px;border:1px solid #000;">
<span style='font-size:10px;'>SACHIN TENDULKAR</span>
<br />
<span style='font-size:10px;'>TENDULKAR TON HELPS INDIA TO WIN THE MATCH</span>
</div>
<p><button onclick="changeFontSize()">Change font size</button></p>
</center>
</div>
</body>
</html>
__________________
Jerry Broughton

Last edited by job0107; 11-17-09 at 08:03 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
The Following User Says Thank You to job0107 For This Useful Post:
hemi (11-17-09)
  #4 (permalink)  
Old 11-17-09, 07:57 AM
hemi hemi is offline
Newbie Coder
 
Join Date: Aug 2009
Posts: 62
Thanks: 3
Thanked 0 Times in 0 Posts
thnk u for tht
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
text box with scroll bar silvermane CSS 7 01-16-09 03:03 AM
div css theighost CSS 11 09-14-08 02:30 AM
Draggable Tables Ares JavaScript 10 08-03-06 06:55 AM
Need Your HelP! Loading Multiple External Text into Multiple Dynamic Text Fields Flash_Boi Flash & ActionScript 2 03-30-06 03:27 PM
picking random entries with a filter... Double selection problem dsumpter PHP 7 11-16-03 07:19 PM


All times are GMT -5. The time now is 04:51 AM.
vBulletin® Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.