Advanced AttributedText in UIButton

Sayler8182
Sep 3, 2018 · 2 min read

The most of you know how to set AttributedText for UIButton in iOS. It’s easy to create text with defined attributes, but what if part of this text have to be clickable? For example, when you need to create standard button with “agree to terms”?

Creating this type of text is not very hard but how to check what was selected? After all you can have other link to Terms and Privacy Policy.

I had the same problem and because of that I created more advanced solution than simple AttributedText.

Usage

At the beginning we have to define our styles that we can use in entire App.

Because I needed more control in displaying text I extended UIButton. Implementation of new AttributedButton you can find here. Now I will show you some examples of how to use it.

1. Regular plain text

2. Attributed text

3. Attributed text with clickable elements

Finally with couple lines of code we reached our goal. What is very important actions for button still work fine. When we click in part without action, it will perform normal IBAction function, otherwise we will perform our custom action.


Source

Thank you for reading! If you liked this article, please clap so other people can read it too. I will also have more motivation for next article :) You can also see my other articles, maybe you will find something for you.

If you have any question or suggestion leave a comment.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade