ios - Dynamically set the height of uitextview like message app -
i having problem of setting height of uitextview
message app. it's achieved increasing height of when ever newline occurs.
my problem when user edits text content it's not decreasing height of container, in textview
child.
i using auto layout , code
- (void)textviewdidchange:(uitextview *)textview { uitextposition* pos = self.posttextview.endofdocument;//explore others beginningofdocument if want customize behaviour cgrect currentrect = [self.posttextview caretrectforposition:pos]; if (currentrect.origin.y > previousrect.origin.y){ if (self.containerheightt.constant == 99) { return; } self.containerheightt.constant += 10; nslog(@"the container height %f",self.containerheightt.constant); } previousrect = currentrect; }
please tell me how detect when line gets deleted textview
, thereafter reducing height of container.
please below way -
make sure uitextview scrolling disable.
add dummy uitextview height constraint default height, connect
iboutlet (nslayoutconstraint - textviewheightconstraint). @property (weak, nonatomic) iboutlet nslayoutconstraint *textviewheightconstraint; - (bool)textview:(uitextview *)textview shouldchangetextinrange:(nsrange)range replacementtext:(nsstring *)text { return yes; } - (void)textviewdidchange:(uitextview *)textview{ cgfloat mymaxsize = 480.0f; cgrect framerect = textview.frame; cgrect rect = [textview.text boundingrectwithsize:cgsizemake(framerect.size.width, cgfloat_max) options:nsstringdrawinguseslinefragmentorigin attributes:@{nsfontattributename: textview.font} context:nil]; cgsize size = rect.size; if (size.height > mymaxsize) { size.height = mymaxsize; [textview setscrollenabled:yes]; } framerect.size = size; self.textviewheightconstraint.constant = size.height; }
Comments
Post a Comment