简体   繁体   English

通过减小字体大小和截断文本来使文本适合容器内

[英]Fit text within a container by reducing font-size and truncating text

I have to fit text within a div container which is square. 我必须在方形的div容器中放入文本。 If the text is too large to fit within the container, I have to reduce the font-size from 32px to 18px. 如果文本太大而无法容纳在容器中,则必须将字体大小从32px减小到18px。 And even if that is not fitting, I have to truncate the text with "...". 即使那不合适,我也必须用“ ...”截断文本。 It looks simple enough. 看起来很简单。 I am using plain JavaScript / React. 我正在使用普通的JavaScript / React。

There are some approaches that I am trying. 我正在尝试一些方法。

<div className="container" ref={c => { this.comp = c; }}>
  {text}
</div>

If the clientHeight < scrollHeight, the text overflows, hence I reduce the font size. 如果clientHeight <scrollHeight,则文本溢出,因此我减小了字体大小。

if (this.comp.clientWidth < this.comp.scrollHeight) {
  this.setState({ overflow: true });
}

I am setting a style on the container based on the state changes. 我根据状态更改在容器上设置样式。

style={this.state.overflow ? { fontSize: 18 } : undefined}

I like to reduce to truncate the text if it still overflows. 如果文本仍然溢出,我想减少截断文本。 Not sure how to truncate the text. 不知道如何截断文本。

Code snippet so far: 到目前为止的代码段:

 class App extends React.Component { constructor() { super(); this.state = {}; } componentDidMount() { if (this.comp.clientHeight < this.comp.scrollHeight) { console.log('reducing font size from 32px to 18px'); this.setState({ overflow: true }); } } render() { const { overflow } = this.state; return ( <div className="container" ref={c => { this.comp = c; }} style={overflow ? { fontSize: 18 } : undefined} > This is a long text which wont fit within the container. Inspite of reducing the font size, it wont fit. And I want to truncate with ellipsis. It is not possible with text-overflow as it is multi-line text. How do I figure how many characters to truncate it to? </div> ); } } ReactDOM.render(<App />, document.getElementById('root')); 
 .container { display: inline-block; width: 200px; height: 200px; background-color: #fafafa; padding: 10px; font-size: 32px; overflow: hidden; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> <div id="root" /> 

This can be done by updating the component text till it fits. 这可以通过更新组件文本直到适合为止来完成。 And then finally setting the state. 然后最后设置状态。 The code snippet is shown below. 该代码段如下所示。

 class App extends React.Component { constructor() { super(); this.state = {}; } componentDidMount() { let overflow; if (this.comp.clientHeight < this.comp.scrollHeight) { overflow = true; this.comp.style.fontSize = '18px'; } let truncatedText; while (this.comp.clientHeight < this.comp.scrollHeight) { const words = this.comp.innerText.split(' '); words.pop(); truncatedText = words.join(' ') + '...'; this.comp.innerText = truncatedText; } this.setState({ overflow, truncatedText }); } render() { const { overflow, truncatedText } = this.state; const text = 'This is a long text which wont fit within the container.Inspite of reducing the font size, it wont fit.And I want to truncate with ellipsis.It is not possible with textoverflow as it is multiline text.How do I figure how many characters to truncate it to.'; return ( <div className = "container" ref = { c => { this.comp = c; } } style = { overflow ? { fontSize: 18 } : undefined }> {truncatedText || text} </div> ); } } ReactDOM.render( <App /> , document.getElementById('root')); 
 .container { display: inline-block; width: 200px; height: 200px; background-color: #fafafa; padding: 10px; font-size: 32px; overflow: hidden; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> <div id="root" /> 

Keep reducing the text till it fits and finally update the state. 不断缩小文本,直到适合为止,最后更新状态。

    let truncatedText;
    while (this.comp.clientHeight < this.comp.scrollHeight) {
      const words = this.comp.innerText.split(' ');
      words.pop();
      truncatedText = words.join(' ') + '...';
      this.comp.innerText = truncatedText;
    }

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

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