2013-11-01 23 views
1

我有一個鏈接到一個JSON字符串(「身體」)我的收藏查看小區內這就是一個UITextView,我要和AFNetworking解析內的所有內容。該字符串中的文本不同(我的每個「帖子」都有不同的信息)。我希望我的UITextView以使所有的文本顯示了基於我的字符串裏面的內容擴展。擴展UITextView的高度,使得它適合一個JSON字符串(iOS7)

集合視圖細胞

@property (weak, nonatomic, readonly) IBOutlet UITextView *body; 

視圖控制器:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *identifier = @"Cell"; 

    PostCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath]; 

    NSDictionary *newPostDictionary = [self.singlePost objectAtIndex:indexPath.row]; 

    cell.body.text = [newPostDictionary objectForKey:@"caption"]; 

    NSLog(@"@"); 

    return cell; 
} 

我嘗試添加我的視圖控制器這裏面的代碼但什麼也沒做。

- (CGFloat)textViewHeightForAttributedText: (NSAttributedString*)text andWidth: (CGFloat)width { 
    UITextView *calculationView = [[UITextView alloc] init]; 
    [calculationView setAttributedText:text]; 
    CGSize size = [calculationView sizeThatFits:CGSizeMake(width, FLT_MAX)]; 
    return size.height; 
} 

謝謝。

回答

0

要獲得任何文本的大小,您應該使用NSString的方法:sizeWithFont:constrainedToSize:您傳遞的參數的字體和最大大小可以從該文本達到。

​​
+0

根據問題,他希望的UITextView到程度,而不是內容萎縮。 –

+0

他想調整的UITextView,這個他獲得文本的大小,可以改變TextView的高度。 –

0

使用sizeWithFont:constrainedToSize:得到的字符串

的大小如果你在iOS7使用boundingRectWithSize:options:attributes:context:代替(sizeWithFont:constrainedToSize:不贊成在iOS7)

+0

我應該在哪裏添加該代碼? (im on ios7) – ChrisBedoya

+0

你可以嘗試在UICollectionViewCell的子類中手動添加你的UITextView,並在'layoutSubviews'方法中設置textView框爲'boundingRectWithSize:options:attributes:context:'方法的結果 – Axadiw

0

設置UITextView的框架是一樣它裏面的內容:

CGRect frame = _textView.frame; 
frame.size.height = _textView.contentSize.height; 
_textView.frame = frame; 
相關問題