javascript - jquery根据字符计算字体大小

标签 javascript jquery asp.net-mvc

我正在尝试创建方法来读取文本框中的所有字符并返回字体大小值。

我的功能无法正常工作。下面是我的代码

    function sizeInput(input) {
    return shrinkToFill(input, 48, '', 'Impact');
}
function shrinkToFill(input, fontSize, fontWeight, fontFamily) {
    var font = fontWeight + ' ' + fontSize + 'px ' + fontFamily;
    var $input = $(input);
    var maxWidth = $input.width() - 50;

    // we're only concerned with the largest line of text.
    var longestLine = $input.val().split('\n').sort(function (a, b) {
        return measureText(a, font).width - measureText(b, font).width;
    }).pop();

    var textWidth = measureText(longestLine, font).width;
    if (textWidth >= maxWidth) {
        // if it's too big, calculate a new font size
        // the extra .9 here makes up for some over-measures
        fontSize = fontSize * maxWidth / textWidth * 0.9;
        font = fontWeight + ' ' + fontSize + 'px ' + fontFamily;
        // and set the style on the input

    }
    else {
        font = fontWeight + ' ' + fontSize + 'px ' + fontFamily;
        // and set the style on the input        
    }
    $input.css({
        'font-size': fontSize
    });
    return fontSize;
}
var measureText = function (str, font) {
    str = str.replace(/ /g, ' ');
    var id = 'text-width-tester';
    var $tag = $('#' + id);
    if (!$tag.length) {
        $tag = $('<span id="' + id + '" style="display:none;font:' + font + ';">' + str + '</span>');
        $('body').append($tag);
    } else {
        $tag.css({
            font: font
        }).html(str);
    }
    return {
        width: $tag.width(),
        height: $tag.height()
    };
};
var topText = $('#topText');
var bottomText = $('#bottomText');

var textUpdated = function () {
    var topFontSize = sizeInput(topText);
    var bottomFontSize = sizeInput(bottomText);
};

它不会返回任何错误,但不起作用。

最佳答案

解决方案在这里 https://jsfiddle.net/5bdLomyp/2/

var sizeInput = function(input) {
    return shrinkToFill(input, 48, '', 'Impact');
}

var shrinkToFill = function(input, fontSize, fontWeight, fontFamily) {
    var font = fontWeight + ' ' + fontSize + 'px ' + fontFamily;
    var $input = $(input);
    var maxWidth = $input.width() - 50;

    // we're only concerned with the largest line of text.
    var longestLine = $input.val().split('\n').sort(function (a, b) {
        return measureText(a, font).width - measureText(b, font).width;
    }).pop();

    var textWidth = measureText(longestLine, font).width;
    if (textWidth >= maxWidth) {
        // if it's too big, calculate a new font size
        // the extra .9 here makes up for some over-measures
        fontSize = fontSize * maxWidth / textWidth * 0.9;
        font = fontWeight + ' ' + fontSize + 'px ' + fontFamily;
        // and set the style on the input
       
    }
    else {
        font = fontWeight + ' ' + fontSize + 'px ' + fontFamily;
        // and set the style on the input        
    }
    $input.css({
        'font-size': fontSize
    });
    return fontSize;
}
var measureText = function (str, font) {
    str = str.replace(/ /g, '&nbsp;');
    var id = 'text-width-tester';
    var $tag = $('#' + id);
    if (!$tag.length) {
        $tag = $('<span id="' + id + '" style="display:none;font:' + font + ';">' + str + '</span>');
        $('body').append($tag);
    } else {
        $tag.css({
            font: font
        }).html(str);
    }
    return {
        width: $tag.width(),
        height: $tag.height()
    };
};

var topText = $('#topText');
var bottomText = $('#bottomText');

$('#topText, #bottomText').keyup(function(){
	 var topFontSize = sizeInput(topText);
   var bottomFontSize = sizeInput(bottomText);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="topText" class="hide text-tool top-text" type="text" placeholder="Top Text" />
<input id="bottomText" class="hide text-tool bottom-text" type="text" placeholder="Bottom Text" />

我想这就是您正在寻找的。

我没有在 HTML 中使用 onkeyuponchange,而是使用 jQuery keyup 功能。

关于javascript - jquery根据字符计算字体大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45181582/

相关文章:

javascript - 在 WebOS/Mojo 中从舞台助手调用 setupWidget

jquery - 在 ajax 请求后使用 .each() 函数淡化 div 有问题吗?

javascript - 创建文本元素并赋予类后缓入文本

asp.net-mvc - 网络 MVC : LinkedIn The remote server returned an error: (401) Unauthorized

jquery - 如何处理mvc3中radiobuttonfor的点击或更改事件

c# - 带有 Entity Framework 的 ASP.NET : EF does not generate identity automatically

javascript - 同步函数和异步函数的区别

javascript - 延迟 jQuery 下拉菜单

javascript - 在 Bootstrap 选项卡面板中创建 Cycle2-carousel

javascript - 如果另一个函数为真,则有一个函数