Dynamic expand UITextView on ios 7

后端 未结 9 1370
天命终不由人
天命终不由人 2020-12-29 09:10

I am using this code

CGRect frame = self.mytext.frame;
frame.size.height = self.mytext.contentSize.height;
self.mytext.frame = frame;

But i

9条回答
  •  孤城傲影
    2020-12-29 09:31

    GrowingTextViewHandler is an NSObject subclass which resizes text view as user types text. Handling resize via NSObject subclass is more useful rather than subclassing UITextView, because it will work for any subclass of UITextView. Here is the sample usage.

    @interface ViewController ()
    
     @property (weak, nonatomic) IBOutlet UITextView *textView;
     @property (weak, nonatomic) IBOutlet NSLayoutConstraint *heightConstraint;
     @property (strong, nonatomic) GrowingTextViewHandler *handler;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
      [super viewDidLoad];
      self.handler = [[GrowingTextViewHandler alloc]initWithTextView:self.textView withHeightConstraint:self.heightConstraint];
      [self.handler updateMinimumNumberOfLines:3 andMaximumNumberOfLine:8];
     }
    
    - (void)textViewDidChange:(UITextView *)textView {
      [self.handler resizeTextViewWithAnimation:YES];
    }
    @end
    

提交回复
热议问题