Tuesday, October 22, 2019

Create Rectangle Bubble Chat iOS Swift 5


Hello all, every week I try to give you a good tutorial to add your skill and knowledge too. And today you got to be suprised because It's not usually tutorial. I need to dig it deep down to present it. So if you want to create chat application, it's good for you. You can take it as reference.

So in chat application, you will face chat from bottom to up. As a screenshoot above, you can see it's not from top to bottom but otherwise. There is bubble chat on left and right. You can say when you send it will be on right and when someone send, it will be on left.

And about text height, it must be flexible height. Like app chat line or whatsapp, there is no cut in height. So it can small or so high. So we need to calculate the height. And about the width it's not full, we give space to the right or left.

I include the title header too so It will be more like chat app. The header will be on viewForFooterInSection why ? Because I reverse the UITableview, so the chat will be on bottom to top. As you can see on image above.

I create UIlabel date after message like chat app usually. So it will be complete for you to implement. And sorry if I don't give you full Source Code because it's easy to follow when you the code. So this is the code..




This is a long code to understand, but I will only give you a importance thing. First you focus on class ChatTableViewCell, you can see there are two method showOtherMessage and showMeMessage. It will implement right or left the bubble chat will be. It becames right when you use showOtherMessage method and otherwise.

You can see the implementation methods in cellForRowAt UITableview. For row 0 I use showOtherMessage and 1 I showMeMessage. And for height row I use the calucation that I found on the internet but I forgor the source. So it's basically calculating the height label and text label. So It will be flexible.

If you have any questions futher, please feel free to comment below. Thanks.

No comments:

Post a Comment