如何根据UITextView中的内容调整UIImageVIew的大小

时间:2015-03-20 02:24:20

标签: ios objective-c uiimageview uitextview

我有一个UIImageView,它的边界内有一个UITextView,我想根据UITextView中的文本调整UIImageView的大小。我该怎么做?

这是我尝试过的:

- (void)textViewDidChange:(MSChatCell *)cell andText:(NSString *)text isBlue:(BOOL)blue
{
    NSLog(@"changing height");
    UITextView *textView;
    PFImageView *chat;

    if(blue){
    CGRect newFrame = [text boundingRectWithSize:cell.blueText.bounds.size options: NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"Arial" size:10.0],NSFontAttributeName, nil] context:nil];
    cell.blueText.frame = newFrame;
        cell.blueChat.frame = newFrame;

    }else{
        CGRect newFrame = [text boundingRectWithSize:cell.whiteText.bounds.size options: NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"Arial" size:10.0],NSFontAttributeName, nil] context:nil];

        cell.whiteText.frame = newFrame;
        cell.whiteChat.frame = newFrame;
    }

}

其中cell.whiteChatcell.blueChat是UIImageViews,cell.whiteTextcell.blueText是UITextViews

1 个答案:

答案 0 :(得分:0)

好吧,计算你的文字大小然后用它创建一个框架对我有用..

CGRect blueTextFrame = cell.blueText.frame;

CGRect blueChatFrame = cell.blueChat.frame;

CGSize textSize = [sample sizeWithFont:[UIFont fontWithName:@"Arial" size:10.0]];

cell.blueTextFrame.size = textSize;

cell.blueChatFrame.size = textSize;

同样适用于白人..