我有一个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.whiteChat
和cell.blueChat
是UIImageViews,cell.whiteText
和cell.blueText
是UITextViews
答案 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;
同样适用于白人..