Ios 使UITextView展开并向下移动其他对象
我有一个Ios 使UITextView展开并向下移动其他对象,ios,textview,Ios,Textview,我有一个UITableView。在每个单元格中,我都有一个长度可变的UITextView。 我希望UITextView扩展以适合所有文本 [UITextView sizeToFit]; 不起作用。 此外,我需要它的展开向下移动单元格中的其他标签和对象(当然,单元格的高度必须改变)。 怎么做?您需要根据uitextview的帧更新每个视图的帧 这就是我想说的,它只是一个示例,您已经修改了所有后续视图的框架 -(UITableViewCell *)tableView:(UITableView *)
UITableView
。在每个单元格中,我都有一个长度可变的UITextView
。
我希望UITextView
扩展以适合所有文本
[UITextView sizeToFit];
不起作用。
此外,我需要它的展开向下移动单元格中的其他标签和对象(当然,单元格的高度必须改变)。
怎么做?您需要根据uitextview的帧更新每个视图的帧 这就是我想说的,它只是一个示例,您已经修改了所有后续视图的框架
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tblCell"];
UITextView *txt = [cell viewWithTag:1];
txt.text =[arr objectAtIndex:indexPath.row];
[txt sizeToFit];
CGRect frame = txt.frame;
CGRect frame2;
UILabel *lbl = [cell viewWithTag:2];
frame2 = lbl.frame;
if(frame.size.width + frame2.size.width >= cell.frame.size.width){
frame2.origin.x = 0;
frame2.origin.y += frame.size.height;
[lbl setFrame:frame2];
}
else{
frame2.origin.x = frame.size .width;
[lbl setFrame:frame2];
}
return cell;
}
首先,您必须声明委托方法
heightforrowatinexpath:
,然后使用此代码
希望这段代码对您非常有帮助,您可以轻松地实现它
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat rowHeight;
NSString *text1 = [arrTemp objectAtIndex:indexPath.row]; //your text
CGRect frame = [text1 boundingRectWithSize:CGSizeMake(160, CGFLOAT_MAX)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSFontAttributeName:[UIFont fontWithName:@"Helvetica" size:12]}
context:nil]; // here get the height of ur textline
CGSize size = CGSizeMake(frame.size.width, frame.size.height+1);
rowHeight=size.height*2;
return rowHeight;
}