iPhone开发:类似iChat的聊天泡泡示例
很多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];
}
我们可以这样使用
- (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;
}
iPhone开发:类似iChat的聊天泡泡示例相关推荐
- 类似9158视频聊天室源码开发方案
类似新浪聊天室和呱呱聊天室,99CU等表现形式,聊天大厅和聊天房间可以自由切换. 2. 数据库最好使用MS SQL,程序代码模块化开发,有充分的扩展性以及接口,二次开发方便. 3. 通过IE浏览器,即 ...
- iphone开发笔记和技巧总结
在iphone程序中实现截屏的一种方法: //导入头文件 #importQuartzCore/QuartzCore.h //将整个self.view大小的图层形式创建一张图片imageUIGrap ...
- iphone开发相关的网站
首先声明一下,apple的官方文档是最重要的,一下网站只是本人学习过程中存在问题时,所寻找的相关帮助网页,有时间的话,对每一个网页写一些注释吧 sqllite相关 http://iwins.blog. ...
- iphone开发一些好的网站推荐
原文:http://blog.csdn.net/linkai5696/article/details/6656031 1.http://developer.apple.com/iphone/libra ...
- 分享iphone开发的好网站,希望大家也能提供一些分享下
1.http://developer.apple.com/iphone/library 这个是官方的代码实例 2.www.cocoachina.com 这个网站比较适合初期开发者,上面的版主之类的也比 ...
- 分享iphone开发的好网站
1.http://developer.apple.com/iphone/library 这个是官方的代码实例 2.www.cocoachina.com 这个网站比较适合初期开发者,上面的版主之类的也比 ...
- 收集各种 iOS App 开发可以用到的代码示例
code4app.com 这网站不错,收集各种 iOS App 开发可以用到的代码示例 cocoacontrols.com/ 英文版本的lib收集 objclibs.com/ 精品lib的收集网站 ...
- iphone 开发,全区索引
小僧所知道得cocoaChinese已上線作品: 佛曰: 排名不分先後 考慮到當前國情, 以下所有iTunes鏈接全部清一色為美國地區 為省時起見, 作格式說明: 以dr大大得最新作iFighter ...
- 《iPhone开发秘籍》带你深入iPhone开发秘境
< iPhone 开发秘籍> 很少有平台能够与 iPhone 独特的开发技术相提并论. iPhone 将基于 OS X 的移动计算与创新的多点触摸屏幕.位置感知.机载加速计等结合在一起.苹 ...
最新文章
- 腾讯 QQ 就“读取浏览器历史记录”道歉!
- UITextField 详解
- chrome浏览器 控制台创建 js脚本 并执行
- Python---堆里有16颗豆子,有两个玩家依次取豆
- MATLAB实战系列(二)- 如何使用YALMIP检验数学模型的正确性?
- Nomad技术手册:调度(Scheduling)
- (74)分析 APC 插入过程 —— KeInsertQueueApc , KiInsertQueueApc
- nio的epoll和selector实现流程分析
- Android 使用XmlPullParser解析xml
- 马蓉微博对呛王宝强,结果坑了新浪肥了阿里云
- 所有浏览器主页惨遭篡改:http://wangzhi6.dhtz444.top/或https://www.2345.com/
- 【软考】2020年下半年计算机技术与软件资格考试划水贴
- jQuery CSS 添加/删除类名
- 桌面窗口管理器和csrss导致Windows的GPU和内存占用过高而卡顿
- Solr全文检索学习笔记·记录
- iOS 一种连线题效果的实现
- python之词频统计
- Spring Guide:Securing a Web Application(中文大概意思)
- 并发队列中迭代器弱一致性原理探究
- SpringBoot实现Excel导出并设置表格属性(easyExcel)
热门文章
- 计算机毕业论文过程管理手册,毕业论文过程管理手册(修改)-陈亚琴.doc
- java jquery的定义方法_jQuery--基本语法
- 违反计算机网络安全如何处罚,网络信息安全知识:违反治安管理行为的处罚包括()。...
- k8s 使用Nginx Ingress实现灰度发布和蓝绿发布
- switch case穿透Java_Switch语句的case穿透
- 刚入行的软件测试工程师如何自学软件测试?0 基础该怎么学习软件测试
- 松江主机点位怎么预留_智能家居设计篇:智能化布线怎么做到有备无患
- c++ string 拼接_String类5个常见面试题的解答过程和原理
- mediarecorder添加时间戳_[Paper] 自动驾驶中相机和激光雷达的时间戳误差标定
- python行与行之间必须对齐_Python行和缩进