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

时间:2018-09-04 12:57:08

标签: javascript reactjs dom

我必须在方形的div容器中放入文本。如果文本太大而无法容纳在容器中,则必须将字体大小从32px减小到18px。即使那不合适,我也必须用“ ...”截断文本。看起来很简单。我正在使用普通的JavaScript / React。

我正在尝试一些方法。

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

如果clientHeight

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

我正在根据状态更改在容器上设置样式。

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

如果文本仍然溢出,我想减少截断文本。不确定如何截断文本。

到目前为止的代码段:

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" />

1 个答案:

答案 0 :(得分:0)

这可以通过更新组件文本直到适合为止来完成。然后最后设置状态。该代码段如下所示。

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" />

继续缩小文本直到适合为止,最后更新状态。

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