1

I have two labels in my layout. I want the font size to adjust to fit the label bounds.

I set to true the UILabel adjustFontSizeToFit.

Moreover a need the two labels to have the same font size. So I added an auto-layout constraint to make the labels have the same size.

The issue with it is the labels do not contain the same string. Sometimes the first is longer and vice-versa. The font I use doesn't have the same width for every character which will not allow me to use a space to fill to shorter string. (This solution looks a bit ugly to me).

I tried to get the label.font.size.pointSize after the label has been displayed but the value returned is always the one I set at the beginning (before being reduced to fit).

Does anyone have an idea on how I could achieve it ?

Arthur
  • 390
  • 1
  • 4
  • 13
  • There are several existing questions asking how to do this. Start by looking through the related questions to the right. I found https://stackoverflow.com/questions/20262156/autolayout-link-two-uilabels-to-have-the-same-font-size but you need to know how to read Objective-C. It's the same APIs so it should be easy to convert to Swift. – rmaddy Sep 11 '18 at 16:06
  • @rmaddy thanks for your comment. I saw this answer earlier today. I was hoping to have the opportunity to avoid looping through the possible sizes as these labels are in a UICollectionViewCell which makes me scared about performance. Another thread I found is https://stackoverflow.com/questions/48399102/set-matching-font-sizes-on-uilabels-in-a-uitableviewcell-when-one-label-adjustfo?rq=1 and I would like to avoid this technique too. – Arthur Sep 11 '18 at 16:21

0 Answers0