iPhone开发 类似 qq聊天的样式

主要通过

UIlabel的sizeToFit方法自动计算文本区域大小

UIImage的- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight;方法拉伸图片

可以根据文本内容自动适应算泡泡高度

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

横向从leftCapWidth+1个像素开始,该像素被横向无限复制,作为中间部分,剩余部分又被链接到一起组成整张图

纵向topCapHeight+1个像素开始,该像素被纵向无限复制,作为中间部分,剩余部分又被链接到一起组成整张图

所有拉伸后的图片不会变模糊。


效果如下,先上图



所要用到的资源



定义一个类ChatPopView,代码日下

#import


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];

}


我们可以这样使用

 

- (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;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值