简体   繁体   English

<textarea>调整IE 10的字体大小

[英]<textarea> resize font issue IE 10

I'm trying to implement a behavior like this one , that is a draggable resizable textarea with dynamic font sizing. 我正在尝试实现像这样的行为,这是一个具有动态字体大小的可拖动的可调整大小的textarea。 I have to admit that I'm quite new to web dev so forgive me if I will write some stupid thing. 我不得不承认我对网络开发还很陌生,所以请原谅我,如果我会写一些愚蠢的东西。

I'm facing the following issue when I try to resize the font in the text area, below is the code I use to resize the font depending on the textarea height 当我尝试在文本区域中调整字体大小时遇到​​以下问题,下面是我根据textarea高度调整字体大小的代码

// Called on keyup
that._adaptFontSize = function(){
  var i = 1;
  while ( $textArea[0].scrollHeight <= $textArea.height() ){
    console.log('Iteration : '+(i++));
    $textArea.css('font-size', '+=1');
  }
};

My idea here is to maximize the font size based on the textarea height (maybe I should also consider the width. 我的想法是根据textarea高度最大化字体大小(也许我也应该考虑宽度。

My issue is that the code above produce different results based on the browser, in Chrome and Safari the font size at the end of the loop is the same on Firefox is the double, I don't really understand what's going on.. this basic fiddle shows the issue 我的问题是上面的代码根据浏览器产生不同的结果,在Chrome和Safari中,循环结束时的字体大小是相同的,在Firefox上是双倍的,我真的不明白发生了什么......这个基本的小提琴显示了这个问题

UPDATE 1 更新1

I have found that the problem of calculating a wrong size comes from the placeholder presence. 我发现计算错误大小的问题来自占位符存在。

So basically on Chrome / Safari if there is no placeholder the font size at the end of the iteration is equal to the size I get on Firefox. 所以基本上在Chrome / Safari上如果没有占位符,迭代结束时的字体大小等于我在Firefox上获得的大小。 So there is something wrong in relying to the placeholder for the max size calculation. 因此,依靠占位符进行最大尺寸计算会出现问题。

UPDATE 2 更新2

the following fiddle does more or less what I would like to achieve, the real problem is that it does not work at all on IE 10 , the reason is that the scrollHeight on IE10 does not change when changing the font-size. 下面的小提琴或多或少地做了我想要实现的,真正的问题是它在IE 10上根本不起作用 ,原因是IE10上的scrollHeight在改变font-size时没有改变。

UPDATE 3 更新3

Apparently the scrollHeight on IE10 does not get update when increasing the font size inside the loop. 显然,当增加循环内的字体大小时,IE10上的scrollHeight不会得到更新。 That basically is the naive method used to fit the font inside the textarea. 这基本上是用于拟合textarea内部字体的天真方法。

I was wondering if an alternative to scrollHeight exist to workaround this problem, but I did not find any working solution so far 我想知道是否存在scrollHeight的替代方法来解决这个问题,但到目前为止我还没有找到任何可行的解决方案

Chrome calculates scrollHeight as the maximum of the placeholder text's height and the textarea content's height. Chrome scrollHeight计算为占位符文本高度和textarea内容高度的最大值。 I don't know if that's a bug or a feature. 我不知道这是一个错误还是一个功能。

The workaround is simple – clear placeholder before adjusting the font size, then restore it afterwards: 解决方法很简单 - 在调整字体大小之前清除placeholder ,然后将其恢复:

var textArea = $('.area');

var keyupHandler = function(e) {
  console.log('keyup');
  var ph= textArea[0].placeholder;
  textArea[0].placeholder= '';
  while ( textArea[0].scrollHeight <= textArea.height()) {
    console.log('increase');
    textArea.css('font-size', '+=1');
  }
  while ( textArea[0].scrollHeight > textArea.height()) {
    console.log('decrease');
    textArea.css('font-size', '-=1');
  }
  textArea[0].placeholder= ph;
}
textArea.on('keyup',keyupHandler);

i have managed to grow and shrink font according to the text entered into the text area 我已设法根据输入文本区域的文本增长和缩小字体

Link to fiddle 链接到小提琴

and the javascript modified as below 和javascript修改如下

var textArea = $('.area');
var maxTextSize = '75';
var keyupHandler = function (e) {
    console.log('keyup');
    console.log(textArea[0].clientHeight);
    console.log(textArea[0].scrollHeight);
    console.log($("#ta").css('font-size'));
    while (textArea[0].clientHeight = textArea[0].scrollHeight && $("#ta").css('font-size') < maxTextSize) {
        console.log('font increase');
        textArea.css('font-size', '+=1');
    }

    while (textArea[0].clientHeight < textArea[0].scrollHeight) {
        console.log('increase');
        textArea.css('font-size', '-=1');
    }


}
textArea.on('keydown', keyupHandler);

Edit : tested in firefox 25.01 as well 编辑:在Firefox 25.01中测试过

UPDATE UPDATE

I think your problem is the placeholder. 我认为你的问题是占位符。 Chrome & safari will resize the placeholder text as well, making it the defining text once you start typing. Chrome&safari也会调整占位符文本的大小,使其在您开始输入后成为定义文本。 If you leave it out it works fine: http://jsfiddle.net/me2loveit2/qjvbhquj/11/ 如果你把它留下来它工作正常: http//jsfiddle.net/me2loveit2/qjvbhquj/11/

OLD RESPONSE 旧的回应

Here is one solution: http://jsfiddle.net/me2loveit2/qjvbhquj/ 这是一个解决方案: http//jsfiddle.net/me2loveit2/qjvbhquj/

I am using jQuery UI to bind the resize event to my fitText() function. 我正在使用jQuery UI将resize事件绑定到我的fitText()函数。

JS: JS:

$("textarea").resizable({
    resize: function() {
        fitText();
    },
    minWidth:'100px',
    minHeight:'50px'
});
$(document).keyup(function(){
    fitText();
});
function fitText(){
    var textarea = $('textarea');
    while((textarea.get(0).scrollHeight < textarea.outerHeight()) && (textarea.get(0).scrollWidth < textarea.outerWidth())){
        textarea.css('font-size','+=1');
    }
    while((textarea.get(0).scrollHeight > textarea.outerHeight()) || (textarea.get(0).scrollWidth > textarea.outerWidth())){
        textarea.css('font-size','-=1');
    }
}
fitText();

CSS: CSS:

textarea{
    text-align: center;
    width:100px;
    height:50px;
    overflow:hidden;
}

HTML: HTML:

<textarea>hi this is text</textarea>

Maybe you can use a "contentEditable" div instead of a textarea? 也许你可以使用“contentEditable”div而不是textarea? I think that on the provided igmur link they use a div as well. 我认为在提供的igmur链接上他们也使用div。

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

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