如何根据输入内容让TextView自动调整高度
2个回答
2015-07-01 · 知道合伙人互联网行家
关注
展开全部
- (void)textViewDidChange:(UITextView *)textView{
if(textView.text.length > 20)//一行最多多少字节
{
//TextView底面背景图片根据内容自动调整高度
UIImage *img = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"inputbox"ofType:@"png"]];
[BgImage setImage:[img stretchableImageWithLeftCapWidth:21 topCapHeight:14]];
UIFont *font = [UIFont systemFontOfSize:12];
CGSize size = [textView.text sizeWithFont:font constrainedToSize:CGSizeMake(320, 140)lineBreakMode:UILineBreakModeWordWrap];
BgImage.frame = CGRectMake(0, 202-size.height+15, 320, size.height+28);
InputTextVeiw.contentInset = UIEdgeInsetsZero;//以换行为基准
[textView setFrame:CGRectMake(51, 210-size.height+18, 200, size.height+5)];
if(textView.text.length > 20)//一行最多多少字节
{
//TextView底面背景图片根据内容自动调整高度
UIImage *img = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"inputbox"ofType:@"png"]];
[BgImage setImage:[img stretchableImageWithLeftCapWidth:21 topCapHeight:14]];
UIFont *font = [UIFont systemFontOfSize:12];
CGSize size = [textView.text sizeWithFont:font constrainedToSize:CGSizeMake(320, 140)lineBreakMode:UILineBreakModeWordWrap];
BgImage.frame = CGRectMake(0, 202-size.height+15, 320, size.height+28);
InputTextVeiw.contentInset = UIEdgeInsetsZero;//以换行为基准
[textView setFrame:CGRectMake(51, 210-size.height+18, 200, size.height+5)];
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询