IOS疯狂基础之 UITextView 自动调整高度

这里主要记录一下 textview 的自动调整高度的代码

方法二和三比较可以,方法一在中文后面不断输入数字时 会换行显示之后就算不准,输入一般的字符也是挺符合要求

- (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:15];

        CGSize size = [textView.text sizeWithFont:font constrainedToSize:CGSizeMake(300, 1000) lineBreakMode:UILineBreakModeCharacterWrap];

        //BgImage.frame = CGRectMake(0, 202-size.height+15, 320, size.height+28);

        [textView setFrame:CGRectMake(0, 1, 320, size.height+10)];

    //}

    

//方法二

    CGRect frame = textView.frame;

    CGSize size = [textView.text sizeWithFont:textView.font

                                constrainedToSize:CGSizeMake(280, 1000)

                                    lineBreakMode:UILineBreakModeTailTruncation];

    frame.size.height = size.height > 1 ? size.height + 20 : 64;

    textView.frame = frame;

    

 //方法三   

//    CGRect frame = textView.frame;

//    frame.size.height = textView.contentSize.height;

//    textView.frame = frame;

}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值