I'm trying to create a UITextView
where the height increase dynamically.
There is a UIView
to group this UITextView
with a UIButton
.
I've added a observer to the NSnotificationCenter
for the UITextViewTextDidChangeNotification
notification.
According to the height of the current tapping text , I create a new CGRect
:
CGRect newRect = [contentTextView.text boundingRectWithSize:CGSizeMake(222, CGFLOAT_MAX)
options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) attributes:@{NSFontAttributeName:font} context:nil];
Then, I change the size of the current UIView
:
CGRect tmp = commentView.frame;
CGRect fitRectForView = CGRectMake(tmp.origin.x, tmp.origin.y - abs(newRectH - commentView.frame.size.height), tmp.size.width, newRect.size.height);
commentView.frame = fitRectForView;
Finally, I change the height of my UITextView
:
CGRect fitRectForTextView = CGRectMake(contentTextView.frame.origin.x, contentTextView.frame.origin.y, contentTextView.frame.size.width,newRectH);
contentTextView.frame = fitRectForTextView;
Here is the log of the origin and the size of my contentTextView
variable before and after the fitting:
//Before
contentTextView origin : 0.000000 ; 0.000000
contentTextView size : 33.000000 ; 251.000000
//After
contentTextView origin : 0.000000 ; 0.000000
contentTextView size : 84.000000 ; 251.000000
With this screenshot, you can see that only my commentView
's height is changing.
I put a constraint on the send
button to keep it on the bottom of the UIView
(commentView) but it's not effective.
I can't find my mistake.
Here is my first source: create-a-multi-line-uitextfield