简体   繁体   English

根据元素高度自动设置字体大小

[英]Set font-size automatically according to the element height

I have a text input and preview text div.我有一个文本输入和预览文本 div。 I want the preview text font size to adjust automatically according to the given height .我希望预览文本font size根据给定的height自动调整。 Please keep in mind, it will only see height to adjust its font size, width will be auto .请记住,它只会看到height来调整其字体大小, width将是auto
I have google and researched a lot, I have also tried them but these questions/answers are different than mine.我有谷歌并进行了很多研究,我也尝试过它们,但这些问题/答案与我的不同。
When i set the div height to 50px , it should automatically adjust the font size according to that height, height matters , height should not go bigger than the given one & when text goes to next line, then it should decrement the font size to fit the text to the height .当我将 div 高度设置为50px时,它应该根据该高度自动调整font sizeheight matters ,高度不应该大于给定的,当文本进入下一行时,它应该减小font sizefit the textheight
Here is the code:这是代码:

 const input = document.querySelector('input') input.addEventListener('input', (e) => { const self = e.currentTarget const size = parseInt(self.value) const textElem = document.querySelector('.previewText') textElem.style.height = `${size}px` adjustText(size, textElem) }) const adjustText = (size, textElem) => { const childElem = textElem.firstChild.nextElementSibling let fontSize = textElem.style.fontSize fontSize = fontSize.replace('px', '') for (let i = 0; i < size; i++) { if (textElem.offsetHeight > childElem.offsetHeight) { console.log('greater tha') textElem.style.fontSize = `${i}px` } if (textElem.offsetHeight < childElem.offsetHeight) { console.log('less than') textElem.style.fontSize = `${fontSize - 1}px` } } }
 <input type="number"> <div class="previewText"> <div>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod.</div> </div>

It's working almost right, but it's not working perfectly.它几乎可以正常工作,但不能完美运行。 Can someone please help me?有人可以帮帮我吗? I am stuck...我被困住了...

Not sure I got your code right but I guess the condition in your for loop doesn't work.不确定我的代码是否正确,但我猜你的 for 循环中的条件不起作用。

You need to decrease font-size as long as the text height is larger than it's parent div like so:只要文本高度大于它的父 div,您就需要减小字体大小,如下所示:

    for(let i; textHeight>previewTextHeight; i++ ){}

Example snippet示例片段

(You can edit the text to check the font-size adjustment) (您可以编辑文本以检查字体大小调整)

 const fontSize = document.querySelector('#fontSize'); const previewText = document.querySelector('.previewText'); const textInner = document.querySelector('.textInner'); const currentFontSize = document.querySelector('.currentFontSize'); const adjustText = function() { let size = fontSize.value; previewText.style.fontSize = `${size*1}px`; previewText.style.height = `${size}px`; let previewTextHeight = previewText.clientHeight; let textHeight = Math.ceil(textInner.clientHeight); for (let i; textHeight > previewTextHeight; i++) { size -= 1; previewText.style.fontSize = `${size*1}px`; textHeight = Math.ceil(textInner.clientHeight); } currentFontSize.textContent = `fontSize: ${size} height: ${previewTextHeight}`; } // adjustText(); fontSize.addEventListener('change', (e) => { adjustText() }) textInner.addEventListener("input", (e) => { adjustText() });
 .previewText { outline: 1px solid #ccc; line-height: 1.2em; font-family: Arial; } .textInner:focus { outline: none }
 <p>Font-size</p> <input id="fontSize" type="range" value="200" step="10" min="10" max="200"> <p>Editable text</p> <div class="previewText"> <div class="textInner" contentEditable>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod.</div> </div> <p class="currentFontSize"></p>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM