Ios UITextView使用自动布局自动调整大小
我正在为UICollectionView构建一个状态项。我的问题是,当我想在状态区域中添加一些文本时,我无法将其自动调整为新文本的大小。我已经打开了自动布局,我尝试了stacky上的所有东西 我认为最接近正确的是:Ios UITextView使用自动布局自动调整大小,ios,objective-c,autolayout,Ios,Objective C,Autolayout,我正在为UICollectionView构建一个状态项。我的问题是,当我想在状态区域中添加一些文本时,我无法将其自动调整为新文本的大小。我已经打开了自动布局,我尝试了stacky上的所有东西 我认为最接近正确的是: -(UICollectionViewCell*)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(NSIndexPath*)indexPath { StatusItemModel*stat
-(UICollectionViewCell*)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(NSIndexPath*)indexPath
{
StatusItemModel*statusModel=[self.items objectAtIndex[indexPath indexPosition:0]];
StatusItemEventCell*statusCell=[collectionView dequeueResusableCellwithReuseIdentifier:@“EventStatusItem”forIndexPath:indexPath];
statusCell.statusTitleLabel.text=[statusModel.statusDetails valueForKey:@“title]”;
statusCell.statusContentTextView.text=[statuaModel.statusDetails valueForKey:@“内容”];
[状态单元格布局需要];
返回状态单元;
}
//在这之后,我相信我们必须在这方面施展魔法,但是什么呢?
-(CGSize)collectionView:(UiCollectionView*)collectionView布局:(UICollectionViewLayout*)collectionViewLayout大小FormiteIndeXPath:(NSIndexPath*)indexPath
{
//如何获取textview状态ContentTextView.text的大小?
//有了这些,我就能知道需要归还什么了。
返回CGSizeMake(299.f,200.f);
}
“自动布局”是使用单元格中所有元素的约束设置的。我甚至还玩过内部大小和占位符,不过现在仍然很幸运。请有人给我指出正确的方向。因此,在绕圈思考有更好的方法之后,不,我们需要知道大小,然后才能为集合视图设置单元格的大小。相当适得其反,因为有时我们在运行时不知道它的大小。我解决这个问题的方法是创建一个模拟UITextView对象,然后调用
sizehattfits
下面是我对代码所做的操作:
- (CGSize) collectionView:(UICollectionView *) collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
StatusItemModel *statusModel = [self.items objectAtIndex:[indexPath indexAtPosition:0]];
UITextView *temporaryTextView = [[UITextView alloc] init];
temporaryTextView.text = [statusModel.statusDetails valueForKey:@"content"];
CGSize textBoxSize = [temporaryTextView sizeThatFits:CGSizeMake(299.0f, MAXFLOAT)];
// Can now use the height of the text box to work out the size of the cell and
// the other components that make up the cell
return textBoxSize;
}
看看这个。希望对你有帮助