Hi All,
If you want to change height of textView according to content height than you can follow this code.
Use auto layout to set view in story board and make IBOutlet for textview . Assign delegate to textview and dont give height constraint to textview
In Objective c
Add this delegate method in your view controller.
- (void)textViewDidChange:(UITextView *)textView
{
CGFloat fixedWidth = textView.frame.size.width;
CGSize newSize = [textView sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)];
CGRect newFrame = textView.frame;
newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), newSize.height);
textView.frame = newFrame;
}
In Swift
Add this delegate method in your viewcontroller
func textViewDidChange(textView: UITextView){
let fixedWidth = textView.frame.size.width
textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.max))
let newSize = textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.max))
var newFrame = textView.frame
newFrame.size = CGSize(width: max(newSize.width, fixedWidth), height: newSize.height)
textView.frame = newFrame;
}
Now textview height will change according to text in it.
0 Comment(s)