你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> 氣泡聊天

氣泡聊天

編輯:IOS開發綜合
- (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];
}
 
效果圖:
 
\

http://up.2cto.com/2012/0201/20120201064215166.rar



摘自 jsntghf
  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved