TextBox自动增高

(function($) {
$.fn.autoTextarea = function(options) {
var defaults = {
maxlength: 200, //默认:200字符
minHeight: $(this).height() //默认最小高度,也就是文本框最初的高度,当内容高度小于这个高度的时候,文本以这个高度显示
};
var opts = $.extend({}, defaults, options);
return $(this).each(function() {
$(this).bind("paste cut keydown keyup focus blur", function() {
var height, style = this.style;
this.style.height = opts.minHeight + 'px';
if (this.scrollHeight > opts.minHeight) {
if (this.value.length > opts.maxlength) {
this.value = this.value.substring(0, maxlength);
} else {
height = this.scrollHeight;
style.overflowY = 'hidden';
}
style.height = height + 'px';
}
});
});
};
})(jQuery);

后台控制显示高度的代码:

private void InitTextBox(TextBox thebox,Unit theHeight)
{
//根据宽度字号算出每行字数,总字数除以每行字数=行数

//this.TextBox.Height=new Unit( "行数*行高 ");

double tmpBoxWidth = thebox.Width.Value*1.03;
double tmpBoxHeight = 0;

double tmpFontSize = thebox.Font.Size.Unit.Value;

double tmpFontWidth = tmpFontSize * 1;
double tmpFontHeight = tmpFontSize * 0.85;

double n = tmpBoxWidth / tmpFontWidth;

int allFontNum=System.Text.Encoding.Default.GetBytes(thebox.Text).Count();
double rNum = allFontNum / n;

tmpBoxHeight = tmpFontHeight * rNum;


if (tmpBoxHeight > theHeight.Value)
{
thebox.Height = new Unit(tmpBoxHeight);
}
else{
thebox.Height=theHeight;
}
}

posted @ 2011-12-23 15:48  Areas  阅读(335)  评论(0编辑  收藏  举报