Font size auto adjust to fit

后端 未结 7 1789
夕颜
夕颜 2020-12-09 09:06

I\'m trying to do what the title says. I\'ve seen that font-size can be a percentage. So my guess was that font-size: 100%; would do it, but no.

相关标签:
7条回答
  • 2020-12-09 10:10

    Here's a way to find the height of the text that you are using. It's simple and only uses javascript. You can use this to adjust your text relative to the height you want.

    function getTextHeight(text, fontSize) {
        var numberOfLines = 0;
        var STL = text;
        for(var i = 0; i < STL.length; i++){
        if(STL[i] === '<'){
            try{
                if(STL[i + 1] === 'b' && STL[i + 2] === 'r' && STL[i + 3] === '>'){
                    numberOfLines++;
                }
            }
            catch(err){
                break;
            }
        }
        return (numberOfLines + 1) * fontSize;
    }
    
    0 讨论(0)
提交回复
热议问题