【问题标题】:How to adjust fontsize to fit textarea width and height as much as possible?如何调整字体大小以尽可能适合文本区域的宽度和高度?
【发布时间】:2023-03-19 14:45:02
【问题描述】:

我在textarea 中输入文本。我希望动态调整字体大小,以便文本尽可能多地用类型填充可用的textarea。为此,我在背景中使用span 和display:none 样式来测量当前输入文本的宽度并将其重新缩放到可用宽度。我的逻辑和浏览器有问题:

  1. span 随可用浏览器窗口大小缩放
  2. 短文本不一定要缩小字体大小,也可以在一个完整的单词后换行,这样文本区域的高度也占满(不仅是宽度)。

Working fiddle to illustrate the idea.

如何调整文本区域的宽度和高度?

【问题讨论】:

    标签: javascript jquery html css


    【解决方案1】:

    棘手的部分是减少字体大小的算法。这里有两个解决方案。 first one 是你见过的最丑陋的代码(抱歉这里是凌晨 4:30,我累了)但它展示了解决方案,递归函数

    $(document).ready(function() {
      var textAreaHeight = $('#inp').height();
      var fontSize = 200;
      var font = fontSize + "px";
    
      $('#inp').css("font-size", font);
      $('.hidden').css("font-size", font);
    
      $('#inp').keyup(function() {
        var txt = $(this).val();
        $('#hiddenSpan').html(txt);
    
        fontSize = decreaseFontSize(fontSize);
        font = fontSize + 'px';
    
        $('#inp').css("font-size", fontSize + 'px');
      })
    
      function decreaseFontSize(tempFontSize) {
        var textHeight = $('#hiddenSpan').height();
        if (textHeight > textAreaHeight) {
          var factor = .99; /* Arbitrary scaling factor */
          tempFontSize *= factor;
          $('#hiddenSpan').css("font-size", tempFontSize + 'px');
    
          return decreaseFontSize(tempFontSize);
        } else {
          return tempFontSize;
        }
      }
    })
    

    second one 更简洁,但只要您到达末尾,只需添加另一行。

    $(document).ready(function() {
        var textAreaHeight = $('#inp').height();
        var fontSize = 200;
        var inputLength = 0;
        var font = fontSize + "px"
    
        $('#inp').css("font-size", font);
        $('.hidden').css("font-size", font);
    
        $('#inp').keyup(function() {
            var txt = $(this).val();
            $('#hiddenSpan').html(txt);
    
            var textHeight = $('#hiddenSpan').height();
    
            if( textHeight > textAreaHeight ) {
                var font = decreaseFontSize( textHeight) + "px";
                $(this).css("font-size", font);
                $('.hidden').css("font-size", font);
            }
        })
    
        function decreaseFontSize( textHeight) {
            fontSize = textAreaHeight/(textHeight/fontSize); /* textHeight / fontSize will tell you how many rows are currently in the #hiddenSpan and will then fit those rows inside the height of the textArea */
            return fontSize;
        }
    })
    

    实际上,使这两种解决方案都起作用的答案的主要部分是我将其添加到您的#hiddenSpan

    #hiddenSpan {
      max-width: 300px; /* Makes the text wrap like the input box */
      display: inline-block; 
      word-wrap: break-word; /* Paired with display, allows the text to wrap */
      font-family: "Times New Roman", Times, serif; /* Added the same font to both elements so that they could actually be coordinated */
    } 
    

    在用我的小手指能收集到的尽可能多的字符测试这两个之后,我注意到递归函数稍微好一点,但没有你想象的那么多。

    【讨论】:

    • 我正在测试递归解决方案。我想知道的是,在 else 子句中,您返回 size 但它没有分配给调用函数中的任何内容。是这样吗?
    • 哈,不。这是一个深夜的错误。它确实作为函数的结束,但由于 fontSize 是全局设置的,所以返回的大小会丢失。尽管该函数很难保持包含,因为它不能在不更改 fontSize 的情况下仅给您一个理论答案,但我认为您可以在该递归函数中独立设置 #hiddenSpan 字体大小,然后返回递归函数并相乘由全局 fontSize 来设置新的 fontSize。为了清楚起见,我进行了编辑。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 2013-08-23
    • 2023-04-01
    • 1970-01-01
    • 2023-03-28
    相关资源
    最近更新 更多