I'm currently trying to implement a chat for my application but since I'm new at Swift
development I got stuck at the part where I have to create the TableView
cells for it. I was able to find 2 png images of "chat bubbles" (one for receiving and one for sending).
I placed them in Assets
and loaded them in an ImageView
. Now my problem is how can I place a Label
inside the ImageView
and rescale it depending on how many lines of text are in the Label
. The bubble should not exceed 66%
of the screen's width.
OK, there are a few considerations:
If you haven’t already, make your asset stretchable (where the corners aren’t stretched, but only the center is) by:
Also see Xcode documentation “Add a resizable area to an image”.
In your cell, now just add this image view and the label:
For the sake of completeness, the other approach is to make an @IBDesignable
view where you draw the bubble yourself with a CAShapeLayer
whose path
is the cgPath
of a UIBezierPath
. This is what I’ve done in my chat apps. But this stretchable image approach works fine, too.
By the way, if your speaker and listener bubbles are different colors, note that you might want to have flipped renditions of these bubbles for RTL languages.