首页 > 编程语言 > 详细

javascript 文字大小根据字数自动适应文本框

时间:2017-10-11 19:45:26      阅读:240      评论:0      收藏:0      [点我收藏+]

const diffTextCountFontSize = (strText, boxWidth) => {
let textLength = strText.length;
let l = 0;
for (const i in strText) {
if (strText.charCodeAt(i) > 255) {
l += 1;
}
}
const numLength = textLength - l;
if (l > 0) {
if (numLength % 2 === 0) {
textLength = parseInt((strText.length - l) / 2, 10) + l;
} else {
textLength = parseInt((strText.length - l) / 2, 10) + 1 + l;
}
} else {
textLength *= 0.65;
}

return (boxWidth / textLength) * 1.25;
};

 

strText //字符串内容

boxWidth //div固定宽度

(需根据情况,对数据进行相应调整)

javascript 文字大小根据字数自动适应文本框

原文:http://www.cnblogs.com/weimo10235/p/7652155.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!