气泡聊天
- (UIView *)bubbleView:(NSString *)text from:(BOOL)fromSelf {UIView *returnView = [[UIView alloc] initWithFrame:CGRectZero];
returnView.backgroundColor = [UIColor clearColor];
UIImage *bubble = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:fromSelf ? @"bubbleSelf" : @"bubble" ofType:@"png"]];
UIImageView *bubbleImageView = [[UIImageView alloc] initWithImage:[bubble stretchableImageWithLeftCapWidth:21 topCapHeight:14]];
UIFont *font = [UIFont systemFontOfSize:12];
CGSize size = [text sizeWithFont:font constrainedToSize:CGSizeMake(150.0f, 1000.0f) lineBreakMode:UILineBreakModeCharacterWrap];
UILabel *bubbleText = [[UILabel alloc] initWithFrame:CGRectMake(21.0f, 14.0f, size.width + 10, size.height + 10)];
bubbleText.backgroundColor = [UIColor clearColor];
bubbleText.font = font;
bubbleText.numberOfLines = 0;
bubbleText.lineBreakMode = UILineBreakModeCharacterWrap;
bubbleText.text = text;
bubbleImageView.frame = CGRectMake(0.0f, 0.0f, 200.0f, size.height + 40.0f);
if(fromSelf)
returnView.frame = CGRectMake(120.0f, 10.0f, 200.0f, size.height + 50.0f);
else
returnView.frame = CGRectMake(0.0f, 10.0f, 200.0f, size.height + 50.0f);
[returnView addSubview:bubbleImageView];
[bubbleImageView release];
[returnView addSubview:bubbleText];
[bubbleText release];
return [returnView autorelease];
}
- (UIView *)bubbleView:(NSString *)text from:(BOOL)fromSelf {
UIView *returnView = [[UIView alloc] initWithFrame:CGRectZero];
returnView.backgroundColor = [UIColor clearColor];
UIImage *bubble = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:fromSelf ? @"bubbleSelf" : @"bubble" ofType:@"png"]];
UIImageView *bubbleImageView = [[UIImageView alloc] initWithImage:[bubble stretchableImageWithLeftCapWidth:21 topCapHeight:14]];
UIFont *font = [UIFont systemFontOfSize:12];
CGSize size = [text sizeWithFont:font constrainedToSize:CGSizeMake(150.0f, 1000.0f) lineBreakMode:UILineBreakModeCharacterWrap];
UILabel *bubbleText = [[UILabel alloc] initWithFrame:CGRectMake(21.0f, 14.0f, size.width + 10, size.height + 10)];
bubbleText.backgroundColor = [UIColor clearColor];
bubbleText.font = font;
bubbleText.numberOfLines = 0;
bubbleText.lineBreakMode = UILineBreakModeCharacterWrap;
bubbleText.text = text;
bubbleImageView.frame = CGRectMake(0.0f, 0.0f, 200.0f, size.height + 40.0f);
if(fromSelf)
returnView.frame = CGRectMake(120.0f, 10.0f, 200.0f, size.height + 50.0f);
else
returnView.frame = CGRectMake(0.0f, 10.0f, 200.0f, size.height + 50.0f);
[returnView addSubview:bubbleImageView];
[bubbleImageView release];
[returnView addSubview:bubbleText];
[bubbleText release];
return [returnView autorelease];
}
效果图:
/2012/0201/20120201064215166.rar
摘自 jsntghf
补充:移动开发 , IOS ,