The first two describes how to send messages and receive messages through XMPP, this article mainly describes how to beautify our chat program, look at 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.
Here we customize the Tableviewcell
One line is to display the release date, a line to display the messages sent, and a 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)];
Center Display
Senderandtimelabel.textalignment = Uitextalignmentcenter;
Senderandtimelabel.font = [Uifont systemfontofsize:11.0];
Text color
Senderandtimelabel.textcolor = [Uicolor Lightgraycolor];
[Self.contentview Addsubview:senderandtimelabel];
Background map
Bgimageview = [[Uiimageview alloc] Initwithframe:cgrectzero];
[Self.contentview Addsubview:bgimageview];
Chat information
Messagecontentview = [[Uitextview alloc] init];
Messagecontentview.backgroundcolor = [Uicolor Clearcolor];
Not editable
messagecontentview.editable = NO;
messagecontentview.scrollenabled = NO;
[Messagecontentview SizeToFit];
[Self.contentview Addsubview:messagecontentview];
}
return self;
}
As defined, change the cell to our own defined cell in UITableViewCell.
-(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];
Sent by
NSString *sender = [dict objectforkey:@ "Sender"];
News
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 Message
if ([Sender isequaltostring:@ "You"]) {
Background map
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;
}
In this cell, set the background of the message sent and the background map to receive the message.
There's a "time" in the dictionary here.
This is the time 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 the APPDELEGATE.M
We will also make adjustments to the contents of the message we receive.
-(void) Xmppstream: (Xmppstream *) sender Didreceivemessage: (xmppmessage *) message{
// ......
Nsmutabledictionary *dict = [Nsmutabledictionary dictionary];
[Dict setobject:msg forkey:@ "MSG"];
[Dict setobject:from forkey:@ "Sender"];
The time that the message was received
[Dict setobject:[statics GetCurrentTime] forkey:@ "Time"];
......
}
Finally, let's set the height of each line.
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 + = padding*2;
CGFloat height = Size.Height < 65? 65:size.height;
return height;
}
, by the time you send a message, don't 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];
Re-refresh TableView
[Self.tview Reloaddata];
}
}
Okay, here's a tutorial on XMPP sending messages, which will be done in more detail later on about XMPP.
XMPP-based iOS chat client program (iOS-side III)