[英]Size of UITextView after setting its height depending of its content SWIFT
[英]setting the size of UITextView and UITableViewCell depending on Content
大家好我正在我的應用程序上構建聊天功能。
在每個自定義表格單元格中,我都有一個UITextView,其中包含用戶注釋。
我試圖根據內容調整UITextView的大小,然后還要調整單元格的高度。
我遇到的第一個問題是當我嘗試使用sizeToFit調整UITextView的大小時。 由於某種原因,它使UITextView的寬度有時非常狹窄,並且通常根本不起作用。
這是我到目前為止的代碼:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"ClubChatCell";
ClubChatCell *cell = (ClubChatCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[ClubChatCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
ClubDetails *club = nil;
club = [_clubs objectAtIndex:indexPath.row];
[cell.commentText setScrollEnabled:YES];
cell.commentText.text = club.comment;
[cell.commentText sizeToFit];
[cell.commentText setScrollEnabled:NO];
cell.fullNameLabel.text = club.creator;
cell.profilePic.image = club.creatorProfilePic;
cell.profilePic.layer.cornerRadius = cell.profilePic.frame.size.height /2;
cell.profilePic.layer.masksToBounds = YES;
cell.profilePic.layer.borderWidth = 0;
cell.timeLabel.text = club.commentDate;
return cell;
}
然后為TableViewCell的高度:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
ClubDetails *club = nil;
club = [_clubs objectAtIndex:indexPath.row];
NSString *cellText = club.comment;
UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:14.0];
CGSize constraintSize = CGSizeMake(225.0f, MAXFLOAT);
CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
return labelSize.height + 80;
}
有人能指出我正確的方向嗎?
嘗試在此處找到解決方案- 如何調整UITextView的大小? 。
有解決方案:
CGRect frame = _textView.frame;
frame.size.height = _textView.contentSize.height;
_textView.frame = frame;
對於我的應用程序,我使用了以下解決方案:
- (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;
}
我最近才遇到這個問題,可以通過使用boundingRectWithSize:options:attributes:context
此方法采用NSString的文本,並返回CGRect,該CGRect將使用提供的屬性使該文本適合該文本。
您可以類似地使用它:
NSString *text = @"This is my text. It could be any length";
CGSize maxLabelSize = CGSizeMake(280, FLT_MAX);
CGRect rectForTextView = [text boundingRectWithSize:maxLabel Sizeoptions:NSStringDrawingUsesLineFragmentOrigin attributes:textAttributes context:nil];
CGSize變量是CGRect / TextView的約束。 在這種情況下,我做到了使其寬度最大為280,高度為最大浮點值。 將高度設為最大值將使它幾乎無限期擴展。 您顯然可以將這些值設置為所需的任何值。
接下來要做的是獲取CGRect'rectForTextView'的高度並將其分配給CGFloat。
CGFloat cellHeight = CGRectGetHeight(rectForTextView);
現在分配給cellHeight變量的高度就是您要設置tableView的行高和textView的高度。
請記住,您可能想要為文本添加一些額外的邊距空間,因為它實際上是約束給定文本所需的確切高度。
我創建了執行此操作的基本類方法,以便可以在tableView:heightForRowAtIndexPath:
方法中輕松地重用它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.