很多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.hopean.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

iPhone开发:类似iChat的聊天泡泡相关推荐

  1. iPhone开发:类似iChat的聊天泡泡示例

    很多iPhone聊天程序消息显示都喜欢做成iChat的泡泡样式,这样是不是很apple呢? 那么下面用一种简单的方法来实现它. 主要通过 UIlabel的sizeToFit方法自动计算文本区域大小 U ...

  2. 类似9158视频聊天室源码开发方案

    类似新浪聊天室和呱呱聊天室,99CU等表现形式,聊天大厅和聊天房间可以自由切换. 2. 数据库最好使用MS SQL,程序代码模块化开发,有充分的扩展性以及接口,二次开发方便. 3. 通过IE浏览器,即 ...

  3. iphone开发笔记和技巧总结

    在iphone程序中实现截屏的一种方法: //导入头文件   #importQuartzCore/QuartzCore.h //将整个self.view大小的图层形式创建一张图片imageUIGrap ...

  4. iphone开发相关的网站

    首先声明一下,apple的官方文档是最重要的,一下网站只是本人学习过程中存在问题时,所寻找的相关帮助网页,有时间的话,对每一个网页写一些注释吧 sqllite相关 http://iwins.blog. ...

  5. iphone 开发,全区索引

    小僧所知道得cocoaChinese已上線作品:  佛曰: 排名不分先後 考慮到當前國情, 以下所有iTunes鏈接全部清一色為美國地區 為省時起見, 作格式說明: 以dr大大得最新作iFighter ...

  6. iphone开发一些好的网站推荐

    原文:http://blog.csdn.net/linkai5696/article/details/6656031 1.http://developer.apple.com/iphone/libra ...

  7. 分享iphone开发的好网站,希望大家也能提供一些分享下

    1.http://developer.apple.com/iphone/library 这个是官方的代码实例 2.www.cocoachina.com 这个网站比较适合初期开发者,上面的版主之类的也比 ...

  8. 分享iphone开发的好网站

    1.http://developer.apple.com/iphone/library 这个是官方的代码实例 2.www.cocoachina.com 这个网站比较适合初期开发者,上面的版主之类的也比 ...

  9. 《iPhone开发秘籍》带你深入iPhone开发秘境

    < iPhone 开发秘籍> 很少有平台能够与 iPhone 独特的开发技术相提并论. iPhone 将基于 OS X 的移动计算与创新的多点触摸屏幕.位置感知.机载加速计等结合在一起.苹 ...

  10. iPhone开发资料之内存管理 ,循环引用导致的内存问题

    iPhone开发资料之内存管理 ,循环引用导致的内存问题 https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual ...

最新文章

  1. poj 2362 Square
  2. java爬虫面试题_使用Java实现网络爬虫
  3. Centos 安装 MySql
  4. 万能写入sql语句,并且防注入
  5. c语言递归最小值,递归求最大最小值算法 分治策略(c语言实现)
  6. Allocation Aizu - ALDS1_4_D
  7. ABP vNext中使用开源日志面板 LogDashboard
  8. mssql on linux 安装指导
  9. 通过syslog接收远程日志
  10. php 加密保存mysql_PHP及MYSQL中字符串加密函数
  11. android string参数最大长度,每日一问 | 我们经常用的 String类型,你知道它最大可以放多长的字符串吗?...
  12. 【Vegas原创】Can't connect to X11 window server using ':0.0' 解决方法
  13. 论文赏析[TACL18]隐式句法树模型真的能学到句子中有意义的结构吗?
  14. 罗马字符转整数(python)
  15. ecno是什么的缩写_美国的英文缩写是什么简写
  16. tomcat集群(小型项目)
  17. 【SQL】用SQL语句表示同比和环比
  18. 上海市新生婴儿户口登记(出生申报)
  19. android绘制虚线
  20. 第一章:客户端网页编程简介

热门文章

  1. 将整个网站页面变成黑白色
  2. 2021必看!java电子书合集
  3. Linux操作系统资源 大合集【鸿蒙OS Suse 红帽 BSD CentOS Arch Ubuntu】 | 寻找C站宝藏
  4. SAS学习笔记(一)如何安装SAS
  5. JAVA50道基础编程题
  6. 按键精灵定位坐标循环_关于按键精灵win10抓抓截图问题
  7. SwitchHosts修改hosts利器
  8. 用户可以通过软件对计算机,用户可以通过____软件对计算机软、硬件资源进行管理。...
  9. 5个最受欢迎的大数据可视化软件
  10. php用高德地图api坐标返回市_php 使用高德地图(一) 画多边形及编辑 获取坐标 (简单总结)...