How to Draw a Speech Bubble in Swift 2.0
03 Sep 2015
Here’s a quick example of how to create a custom
UIView to display a speech bubble in Swift 2.0 just like this:
Step 1: Subclass UIView
Firstly, you need to subclass
UIView as follows.
If you intended to use storyboard, ensure that you replace the
fatalError line with
Step 2: Override
Do not call
super.drawRect(rect) in this method!
Step 3: Add a convenience initializer
Now we just need to add a convenience initializer so we can change the default color if required.
You can now instantiate a speech bubble with a color as follows:
Of course, you can easily tweak this class to make it more flexible, such as customizing how rounded the edges are or adding a
UILabel to display the number of comments etc.
Here’s the full class for reference: