title: Problems with AutoLayout and adaptive labels
date: 2016-02-02 17:49
tags: Technology#
I have been encountering a problem when using AutoLayout to adapt labels:#
This has been really frustrating for me, and it only occurs on iPhone 6 and 6 Plus. At first, I thought it was an issue with my adaptive approach, but after rewriting it several times, I realized that I didn't make any mistakes...
I can't seem to find the problem.
- Today, I saw a code from a friend in a group chat about adaptive labels, so I asked for help. I found this VerticallyAlignedLabel, and I was excited to try it out, thinking that I finally found a solution. However, I discovered that although it aligned the label vertically, the excess part was still there.
- So, what is the problem? I kept searching and then I found this Multiline UILabel height with Autolayout higher in landscape on Stack Overflow. I had been searching in Chinese before, but couldn't find anything. Finally, when I searched in English, I found similar problems. I thought to myself, surely I'm not the only one facing this issue (how foolish 😢). Then I also found this article from objc.io Intrinsic Content Size of Multi-Line Text, and I decided to modify my approach based on their methods.
- Damn it, I couldn't directly use their methods because I had extracted the view, and it was a label on a table view cell. Then, I had a brilliant idea and thought, it must be because of the width issue. I couldn't make it automatically adjust with the super view's frame, and I couldn't directly calculate the correct position. I really admired myself. Then I ran the code...
- It still didn't work, damn it! Can't I just enjoy playing around? I don't want to live anymore.
-
Just at that moment, I suddenly realized that below the line where I assigned a value to the label, I had added another line of code to increase the spacing between labels. I commented it out and tried again... Yes, it worked! Don't get too excited, let's try it on iPhone 6 Plus. The result... it really worked. At that moment, I felt like this:
-
There was just one more thing left. After making so many changes, what actually worked? Did everything work together? So, I deleted the custom label from the first step, and it didn't cause any problems. Then I deleted the code for calculating the frame, shit, it didn't work. This line was the key, and there was no more after that. After spending the whole afternoon, I only did two things: a. added a line of code; b. deleted a line of code.
This is the initial code:#
This is the final code:#
Conclusion#
Actually, I should have figured out the reason for this problem a long time ago, because it only occurs on iPhone 6 and 6 Plus, and the condition in the code specifically targets devices with a wider screen than the iPhone 5s. But I didn't think about it. Ps (If you encounter the problem of extra height in the calculated height, you can try the method provided in link 2; if the height is too short, check if you forgot to add 0.1).
Also, I did miss a line of code, which is the one that sets the width of the label. This is because I added the view to the controller in viewDidLoad, and at that time, the auto layout hadn't finished yet. (Ps: Normally, setting the frame should be done in viewDidLayoutSubviews) But I usually create it in viewDidLoad and directly assign the frame, so I finally faced the consequences.