生活随笔
收集整理的這篇文章主要介紹了
[iPhone高级] 基于XMPP的IOS聊天客户端程序(IOS端三)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
前兩篇介紹了如何通過XMPP來發(fā)送消息和接收消息,這一篇我們主要介紹如何來美化我們的聊天程序,看一下最終效果呢,當然源程序也會在最后放出
好了,我們來看一下我們寫的程序
這里我們自定義了TableViewCell
一行是顯示發(fā)布日期,一行是顯示發(fā)送的消息,還有一個是背景
[java]?view plaincopy
-(id)initWithStyle:(UITableViewCellStyle)style?reuseIdentifier:(NSString?*)reuseIdentifier{?? ?????? ????self?=?[super?initWithStyle:style?reuseIdentifier:reuseIdentifier];?? ????if?(self)?{?? ?????????? ????????senderAndTimeLabel?=?[[UILabel?alloc]?initWithFrame:CGRectMake(10,?5,?300,?20)];?? ?????????? ????????senderAndTimeLabel.textAlignment?=?UITextAlignmentCenter;?? ????????senderAndTimeLabel.font?=?[UIFont?systemFontOfSize:11.0];?? ?????????? ????????senderAndTimeLabel.textColor?=?[UIColor?lightGrayColor];?? ????????[self.contentView?addSubview:senderAndTimeLabel];?? ?????????? ?????????? ????????bgImageView?=?[[UIImageView?alloc]?initWithFrame:CGRectZero];?? ????????[self.contentView?addSubview:bgImageView];?? ?????????? ?????????? ????????messageContentView?=?[[UITextView?alloc]?init];?? ????????messageContentView.backgroundColor?=?[UIColor?clearColor];?? ?????????? ????????messageContentView.editable?=?NO;?? ????????messageContentView.scrollEnabled?=?NO;?? ????????[messageContentView?sizeToFit];?? ????????[self.contentView?addSubview:messageContentView];?? ?? ????}?? ?????? ????return?self;?? ?????? }??
定義好,在UITableViewCell中將Cell改成我們自己定義的Cell
[java]?view plaincopy
-(UITableViewCell?*)tableView:(UITableView?*)tableView?cellForRowAtIndexPath:(NSIndexPath?*)indexPath{?? ?????? ????static?NSString?*identifier?=?@"msgCell";?? ?????? ????KKMessageCell?*cell?=(KKMessageCell?*)[tableView?dequeueReusableCellWithIdentifier:identifier];?? ?????? ????if?(cell?==?nil)?{?? ????????cell?=?[[KKMessageCell?alloc]?initWithStyle:UITableViewCellStyleValue1?reuseIdentifier:identifier];?? ????}?? ?????? ????NSMutableDictionary?*dict?=?[messages?objectAtIndex:indexPath.row];?? ?????? ?????? ????NSString?*sender?=?[dict?objectForKey:@"sender"];?? ?????? ????NSString?*message?=?[dict?objectForKey:@"msg"];?? ?????? ????NSString?*time?=?[dict?objectForKey:@"time"];?? ?????? ????CGSize?textSize?=?{260.0?,10000.0};?? ????CGSize?size?=?[message?sizeWithFont:[UIFont?boldSystemFontOfSize:13]?constrainedToSize:textSize?lineBreakMode:UILineBreakModeWordWrap];?? ?????? ????size.width?+=(padding/2);?? ?????? ????cell.messageContentView.text?=?message;?? ????cell.accessoryType?=?UITableViewCellAccessoryNone;?? ????cell.userInteractionEnabled?=?NO;?? ?????? ????UIImage?*bgImage?=?nil;?? ?????? ?????? ????if?([sender?isEqualToString:@"you"])?{?? ?????????? ????????bgImage?=?[[UIImage?imageNamed:@"BlueBubble2.png"]?stretchableImageWithLeftCapWidth:20?topCapHeight:15];?? ????????[cell.messageContentView?setFrame:CGRectMake(padding,?padding*2,?size.width,?size.height)];?? ?????????? ????????[cell.bgImageView?setFrame:CGRectMake(cell.messageContentView.frame.origin.x?-?padding/2,?cell.messageContentView.frame.origin.y?-?padding/2,?size.width?+?padding,?size.height?+?padding)];?? ????}else?{?? ?????????? ????????bgImage?=?[[UIImage?imageNamed:@"GreenBubble2.png"]?stretchableImageWithLeftCapWidth:14?topCapHeight:15];?? ?????????? ????????[cell.messageContentView?setFrame:CGRectMake(320-size.width?-?padding,?padding*2,?size.width,?size.height)];?? ????????[cell.bgImageView?setFrame:CGRectMake(cell.messageContentView.frame.origin.x?-?padding/2,?cell.messageContentView.frame.origin.y?-?padding/2,?size.width?+?padding,?size.height?+?padding)];?? ????}?? ?????? ????cell.bgImageView.image?=?bgImage;?? ????cell.senderAndTimeLabel.text?=?[NSString?stringWithFormat:@"%@?%@",?sender,?time];?? ?? ????return?cell;?? ?????? }??
在這個Cell里設(shè)置了發(fā)送的消息的背景圖和接收消息的背景圖
這里在字典里有一個"time"
這是我們接收和發(fā)送消息的時間
[java]?view plaincopy
+(NSString?*)getCurrentTime{?? ?????? ????NSDate?*nowUTC?=?[NSDate?date];?? ?????? ????NSDateFormatter?*dateFormatter?=?[[NSDateFormatter?alloc]?init];?? ????[dateFormatter?setTimeZone:[NSTimeZone?localTimeZone]];?? ????[dateFormatter?setDateStyle:NSDateFormatterMediumStyle];?? ????[dateFormatter?setTimeStyle:NSDateFormatterMediumStyle];?? ?????? ????return?[dateFormatter?stringFromDate:nowUTC];?? ?????? }??
在AppDelegate.m中
將我們收到消息的內(nèi)容也做一下調(diào)整
[java]?view plaincopy
-?(void)xmppStream:(XMPPStream?*)sender?didReceiveMessage:(XMPPMessage?*)message{?? ?????? ?? ?????? ????NSMutableDictionary?*dict?=?[NSMutableDictionary?dictionary];?? ????[dict?setObject:msg?forKey:@"msg"];?? ????[dict?setObject:from?forKey:@"sender"];?? ?????? ????[dict?setObject:[Statics?getCurrentTime]?forKey:@"time"];?? ?????? ???......?? ?????? }??
最后我們再設(shè)置一下每一行顯示的高度
[java]?view plaincopy
?? -(CGFloat)tableView:(UITableView?*)tableView?heightForRowAtIndexPath:(NSIndexPath?*)indexPath{?? ?????? ????NSMutableDictionary?*dict??=?[messages?objectAtIndex:indexPath.row];?? ????NSString?*msg?=?[dict?objectForKey:@"msg"];?? ?????? ????CGSize?textSize?=?{260.0?,?10000.0};?? ????CGSize?size?=?[msg?sizeWithFont:[UIFont?boldSystemFontOfSize:13]?constrainedToSize:textSize?lineBreakMode:UILineBreakModeWordWrap];?? ?????? ????size.height?+=?padding*2;?? ?????? ????CGFloat?height?=?size.height?<?65???65?:?size.height;?? ?????? ????return?height;?? ?????? }??
,對了,在發(fā)送消息的時候,別忘了也加上
[java]?view plaincopy
-?(IBAction)sendButton:(id)sender?{?? ?????? ?????? ????......?? ?????? ????if?(message.length?>?0)?{?? ?????????? ????????.....?? ?????????? ????????NSMutableDictionary?*dictionary?=?[NSMutableDictionary?dictionary];?? ?????????? ????????[dictionary?setObject:message?forKey:@"msg"];?? ????????[dictionary?setObject:@"you"?forKey:@"sender"];?? ????????[dictionary?setObject:[Statics?getCurrentTime]?forKey:@"time"];?? ?? ????????[messages?addObject:dictionary];?? ?????????? ?????????? ????????[self.tView?reloadData];?? ?????????? ????}?? ?????? ?????? }??
好了,這里關(guān)于XMPP發(fā)送消息的教程就結(jié)束了,以后我們會詳細介紹其他關(guān)于XMPP的內(nèi)容
源碼下載
總結(jié)
以上是生活随笔為你收集整理的[iPhone高级] 基于XMPP的IOS聊天客户端程序(IOS端三)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。