[IPhone advanced] XMPP-based IOS chat client (IOS client 3)

Source: Internet
Author: User

The first two articles introduce how to send and receive messages through XMPP. This article mainly describes how to beautify our chat program and see the final effect, of course, the source program will be released at the end.


Okay. Let's take a look at the program we wrote.

Tableviewcell is customized here.


One row shows the release date, one row shows the sent message, and the other is the background.

-(ID) initwithstyle :( uitableviewcellstyle) style reuseidentifier :( nsstring *) reuseidentifier {self = [Super initwithstyle: style reuseidentifier: reuseidentifier]; If (Self) {// Date label senderandtimelabel = [[uilabel alloc] initwithframe: cgrectmake (10, 5,300, 20)]; // display senderandtimelabel in the center. textalignment = uitextalignmentcenter; senderandtimelabel. font = [uifont systemfontofsize: 11.0]; // text color senderandtimelabel. textcolor = [uicolor lightgraycolor]; [self. contentview addsubview: senderandtimelabel]; // background image bgimageview = [[uiimageview alloc] initwithframe: cgrectzero]; [self. contentview addsubview: bgimageview]; // chat information messagecontentview = [[uitextview alloc] init]; messagecontentview. backgroundcolor = [uicolor clearcolor]; // messagecontentview cannot be edited. editable = no; messagecontentview. scrollenabled = no; [messagecontentview sizetofit]; [self. contentview addsubview: messagecontentview];} return self ;}

Define the cell. In uitableviewcell, change the cell to our own cell.

-(Uitableviewcell *) tableview :( uitableview *) tableview cellforrowatindexpath :( nsindexpath *) indexpath {static nsstring * identifier = @ "msgcell"; Objective * cell = (kkmessagecell *) [tableview details: identifier]; If (cell = nil) {Cell = [[kkmessagecell alloc] initwithstyle: Invalid reuseidentifier: identifier];} nsmutabledictionary * dict = [messages objectatindex: indexpath. row]; // The sender nsstring * sender = [dict objectforkey: @ "sender"]; // message nsstring * message = [dict objectforkey: @ "MSG"]; // time 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; // send the message if ([Sender isinclutostring: @ "you"]) {// background image 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 ;}

Set the background image of the sent message and the background image of the received message in this cell.

There is a "time" in the dictionary"

This is the time when we receive and send messages.

+(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];    }

In appdelegate. m

Adjust the content of the message we received.

-(Void) xmppstream :( xmppstream *) sender didreceivemessage :( xmppmessage *) Message {//...... nsmutabledictionary * dict = [nsmutabledictionary dictionary]; [dict setobject: MSG forkey: @ "MSG"]; [dict setobject: From forkey: @ "sender"]; // message receipt time [dict setobject: [statics getcurrenttime] forkey: @ "time"]; ......}

Finally, we can set the height of each row.

// The height of each row-(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 + = pa Dding * 2; cgfloat Height = size. height <65? 65: size. height; return height ;}

By the way, do not forget to add

-(Ibaction) sendbutton :( ID) sender {// information in the local input box ...... 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]; // refresh tableview [self. tview reloaddata] ;}}

Now, the tutorial on XMPP message sending is complete. We will introduce other content about XMPP in detail later.

Source code download




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.