iPhone開發:類似iChat的聊天泡泡樣本

來源:互聯網
上載者:User

很多iPhone聊天程式訊息顯示都喜歡做成iChat的泡泡樣式,這樣是不是很apple呢?

那麼下面用一種簡單的方法來實現它。

主要通過

UIlabel的sizeToFit方法自動計算文本地區大小

UIImage的- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight;方法展開圖片
可以根據常值內容自動適應算泡泡高度

- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight;的含義是

橫向從leftCapWidth+1個像素開始,該像素被橫向無限複製,作為中間部分,剩餘部分又被連結到一起組成整張圖

縱向從topCapHeight+1個像素開始,該像素被縱向無限複製,作為中間部分,剩餘部分又被連結到一起組成整張圖

所有展開後的圖片不會變模糊。


效果如下,先

 

所要用到的資源


定義一個類ChatPopView,代碼日下

#import <UIKit/UIKit.h>

typedef enum tagPopDirection
{
ePopDirectionLeft = 0,
ePopDirectionRight
}ePopDirection;

@interface ChatPopView : UIView {
UIImageView *popBackground;
UILabel     *contentLabel;
ePopDirection direction;
}

@property (nonatomic,retain) UIImageView *popBackground;
@property (nonatomic,retain) UILabel     *contentLabel;
@property (assign) ePopDirection direction;

-(id)initWithFrame:(CGRect)frame popDirection:(ePopDirection) d;
-(void)setText:(NSString *)str;

@end


#import "ChatPopView.h"

@implementation ChatPopView
@synthesize popBackground;
@synthesize contentLabel;
@synthesize direction;

-(id)initWithFrame:(CGRect)frame popDirection:(ePopDirection) d{

self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor clearColor];
self.direction = d;
UIImageView *back = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
self.popBackground = back;
[back release];
UIImage *theImage = nil;
if (ePopDirectionRight== self.direction) {
theImage = [UIImage imageNamed:@"SSMessageTableViewCellBackgroundGreen"];
}else {
theImage = [UIImage imageNamed:@"SSMessageTableViewCellBackgroundClear"];
}
popBackground.image = [theImage stretchableImageWithLeftCapWidth:21 topCapHeight:15];
[self addSubview:popBackground];
UILabel *content = [[UILabel alloc] initWithFrame:CGRectMake(15, 5, frame.size.width - 15, frame.size.height)];
self.contentLabel = content;
[content release];
contentLabel.numberOfLines = 0;
contentLabel.backgroundColor = [UIColor clearColor];
[self addSubview:contentLabel];
}
return self;
}

-(void)setText:(NSString *)str{
contentLabel.text = str;
[contentLabel sizeToFit];
[self setNeedsLayout];
}

-(void)layoutSubviews{
[super layoutSubviews];
self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, contentLabel.frame.size.width+30, contentLabel.frame.size.height+15);
popBackground.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
}

- (void)dealloc {
[popBackground release];
[contentLabel release];
    [super dealloc];
}

我們可以這樣使用 www.2cto.com


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {   
   
    // Override point for customization after application launch.
   
    [self.window makeKeyAndVisible];
ChatPopView *pop1 = [[ChatPopView alloc] initWithFrame:CGRectMake(20, 100, 200, 80) popDirection:ePopDirectionLeft];
[self.window addSubview:pop1];
[pop1 setText:@"Sent when the application is about to move from active to inactive state. "];
ChatPopView *pop2 = [[ChatPopView alloc] initWithFrame:CGRectMake(130, 220, 200, 40) popDirection:ePopDirectionRight];
[self.window addSubview:pop2];
[pop2 setText:@"This can occur for certain types of..."];
ChatPopView *pop3 = [[ChatPopView alloc] initWithFrame:CGRectMake(20, 300, 280, 300) popDirection:ePopDirectionLeft];
[self.window addSubview:pop3];
[pop3 setText:@""];
ChatPopView *pop4 = [[ChatPopView alloc] initWithFrame:CGRectMake(230, 400, 200, 40) popDirection:ePopDirectionRight];
[self.window addSubview:pop4];
[pop4 setText:@""];
[pop1 release];
    return YES;
}

摘自:Bright的iPhoneDev專欄Focus on iPhone Applications Development

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.