Swift multi-line UIButton with each line resized to fit width

Here is my solution for a standard UIButton that has…
(1) … multiple lines …
(2) … each line has a different preferred font size …
(3) … each line’s font size is automatically sized down to fit the width if needed

I tried playing with the UIButton.titleLabel and its various properties lineBreakMode, adjustsFontSizeToFitWidth …
but could not get the desired result. So in the end I just ended up using the standard attributedTitle of the UIButton
and calculating the given attributed text in advance (given the width) so that everything fits nicely.

I wrapped everything up in an extension and some overloaded operators to make it look nice:

Swift multi-line UIButton with each line resized to fit width

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">