ios中Textview高度随文字的高度的变化而变化

来源:互联网 发布:nginx反向代理服务器 编辑:程序博客网 时间:2024/05/16 06:08

- (void)textViewDidChange:(UITextView *)textView

{

    [textView flashScrollIndicators];  // 闪动滚动条

    

   static CGFloat maxHeight =130.0f;

   CGRect frame = textView.frame;

   CGSize constraintSize = CGSizeMake(frame.size.width,MAXFLOAT);

   CGSize size = [textView sizeThatFits:constraintSize];

   if (size.height >= maxHeight)

    {

        size.height = maxHeight;

        textView.scrollEnabled =YES;   // 允许滚动

    }

   else

    {

        textView.scrollEnabled =NO;    // 不允许滚动,当textview的大小足以容纳它的text的时候,需要设置scrollEnabedNO,否则会出现光标乱滚动的情况

    }

    textView.frame =CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, size.height);

}


0 0
原创粉丝点击